博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
UVa 1599 理想路径 正向bfs+反向bfs
阅读量:3904 次
发布时间:2019-05-23

本文共 3430 字,大约阅读时间需要 11 分钟。

 UVA - 1599  Ideal Path

New labyrinth attraction is open in New Lostland amusement park. The labyrinth consists of n rooms connected by m passages. Each passage is colored into some color ci. Visitors of the labyrinth are dropped from the helicopter to the room number 1 and their goal is to get to the labyrinth exit located in the room number n.

Labyrinth owners are planning to run a contest tomorrow. Several runners will be dropped to the room number 1. They will run to the room number n writing down colors of passages as they run through them. The contestant with the shortest sequence of colors is the winner of the contest. If there are several contestants with the same sequence length, the one with the ideal path is the winner. The path is the ideal path if its color sequence is the lexicographically smallest among shortest paths.

Andrew is preparing for the contest. He took a helicopter tour above New Lostland and made a picture of the labyrinth. Your task is to help him find the ideal path from the room number 1 to the room number n that would allow him to win the contest.

 

Note:

A sequence (a1, a2,..., ak) is lexicographically smaller than a sequence (b1, b2,..., bk) if there exists i such that ai < bi, and aj = bj for all j< i.

 

Input

The input file contains several test cases, each of them as described below.

The first line of the input file contains integers n and m -- the number of rooms and passages, respectively (2$ \le$n$ \le$100000, 1$ \le$m$ \le$200000). The following m lines describe passages, each passage is described with three integer numbers: ai, bi, and ci -- the numbers of rooms it connects and its color (1$ \le$ai, bi$ \le$n, 1$ \le$ci$ \le$109). Each passage can be passed in either direction. Two rooms can be connected with more than one passage, there can be a passage from a room to itself. It is guaranteed that it is possible to reach the room number nfrom the room number 1.

 

Output

For each test case, the output must follow the description below.

The first line of the output file must contain k -- the length of the shortest path from the room number 1 to the room number n. The second line must contain k numbers -- the colors of passages in the order they must be passed in the ideal path.

 

Sample Input

 

4 6

1 2 1
1 3 2
3 4 3
2 3 1
2 4 4
3 1 1

 

Sample Output

 

2

1 3
紫书上的原题。

思路:

先通过反着走一遍bfs,求出每个顶点到终点的最短长度。

然后正着走一遍bfs,求出符合d[u]=d[v]+1顶点中的最短距离,并将最短距离的顶点压入队列。

通过ans数组来记录字典序。

代码如下:

#include 
#include
#include
#include
#include
using namespace std;const int maxn=1e5+5;const int INF=0x3f3f3f3f;//邻接表int head[maxn];int n,m;//到终点的最短距离int d[maxn];//是否被访问过int vis[maxn];//最短字典序数组int ans[maxn];struct edge{ int to; int next; int col;}edge[maxn<<2];void add (int id,int u,int v,int col){ edge[id].to=v; edge[id].col=col; edge[id].next=head[u]; head[u]=id;}//正向bfsvoid zbfs(){ queue
q; int now,next; memset (vis,0,sizeof(vis)); q.push(1); vis[1]=1; for (int i=0;i
q; memset (vis,0,sizeof(vis)); d[n]=0; q.push(n); vis[n]=1; int now,next; while (!q.empty()) { now=q.front();q.pop(); for(int i=head[now];i!=-1;i=edge[i].next) { next=edge[i].to; if(!vis[next]) { d[next]=d[now]+1; q.push(next); vis[next]=1; } } } /*for (int i=1;i<=n;i++) { printf("%d ",d[i]); } printf("\n-------\n");*/}int main(){ while (scanf("%d%d",&n,&m)!=EOF) { memset (head,-1,sizeof(head)); for (int i=0,id=0;i

 

转载地址:http://zwoen.baihongyu.com/

你可能感兴趣的文章
网页乱码分析
查看>>
几个名词解释:大数据、Hadoop、云计算、机器学习、NLP、数据挖掘
查看>>
.net平台个人理解
查看>>
yii2.0学习笔记
查看>>
解决使用composer出现的错误
查看>>
java 线程:sleep join yield | wait notify notifyAll
查看>>
Python 包、模块 概念 from 、import 关键字
查看>>
世界各国的手机号码
查看>>
通配符与正则表达式
查看>>
c++ 与 Java 之 红黑树 哈希表 辨析
查看>>
open GL 、DirectX、open CV、 open Inventor 、cocos2dx、unity3d、3dmax辨析
查看>>
理解矩阵
查看>>
彩虹七色的RGB值
查看>>
常用正则表达式实例
查看>>
java之面向对象——继承、封装、多态
查看>>
web网站架构演变过程
查看>>
c、 c++、 java 基本数据类型 对比辨析
查看>>
文件系统之 簇&块
查看>>
Android 音乐播放器 源码 下载 高仿魅族系统音乐播放器
查看>>
ERP生产线管理系统 with WAMP
查看>>