UVA1599-Ideal Path(BFS进阶)

时间:2021-04-12 13:48:06

Problem UVA1599-Ideal Path

Time Limit: 3000 mSec

UVA1599-Ideal Path(BFS进阶) Problem Description

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.

UVA1599-Ideal Path(BFS进阶) 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 ≤ n ≤ 100000,1 ≤ m ≤ 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 ≤ ai,bi ≤ n,1 ≤ ci ≤ 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 n from the room number 1.

UVA1599-Ideal Path(BFS进阶) 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.

UVA1599-Ideal Path(BFS进阶) Sample Input

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

UVA1599-Ideal Path(BFS进阶) Sample Ouput

2

1 3

题解:这个题真是太有价值了,学到了一个结论,一个技巧。

结论:正向反向分别BFS可以确定出最短路上的点,也就是说,在正向和反向BFS中一个点的时间戳互补,或者说反过来相等的点在最短路上。

技巧:如何分阶段BFS,用vector!

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <queue>
#include <vector>
#define INF 0x3f3f3f3f
using namespace std; const int maxn = +,maxe = +; struct Edge{
int to,next,color;
Edge(int to = ,int next = ,int color = ) :
to(to),next(next),color(color) {}
}edge[maxe<<]; int tot,head[maxn];
int dist[maxn];
int n,m; void AddEdge(int u,int v,int color){
edge[tot] = Edge(v,head[u],color);
head[u] = tot++;
edge[tot] = Edge(u,head[v],color);
head[v] = tot++;
} void rev_bfs(){
memset(dist,-,sizeof(dist));
queue<int> que;
que.push(n);
dist[n] = ;
while(!que.empty()){
int v = que.front();que.pop();
for(int i = head[v];i != -;i = edge[i].next){
int u = edge[i].to;
if(dist[u] < ){
dist[u] = dist[v]+;
que.push(u);
}
}
}
} bool vis[maxn];
vector<int> ans; void bfs(){
memset(vis,false,sizeof(vis));
ans.clear();
vector<int> Next;
Next.push_back();
vis[] = true;
for(int i = ;i < dist[];i++){
int Min_color = INF;
for(int j = ;j < (int)Next.size();j++){
int u = Next[j];
for(int i = head[u];i != -;i = edge[i].next){
int v = edge[i].to;
if(dist[u] == dist[v]+){
Min_color = Min_color < edge[i].color ? Min_color : edge[i].color;
}
}
}
ans.push_back(Min_color);
vector<int> Next2;
for(int j = ;j < (int)Next.size();j++){
int u = Next[j];
for(int i = head[u];i != -;i = edge[i].next){
int v = edge[i].to;
if(dist[u]==dist[v]+ && !vis[v] && edge[i].color==Min_color){
vis[v] = true;
Next2.push_back(v);
}
}
}
Next = Next2;
}
printf("%d\n",ans.size());
printf("%d",ans[]);
for(int i = ;i < (int)ans.size();i++){
printf(" %d",ans[i]);
}
printf("\n");
} void init(){
tot = ;
memset(head,-,sizeof(head));
} int main()
{
//freopen("input.txt","r",stdin);
while(scanf("%d%d",&n,&m) == ){
init();
int u,v,color;
for(int i = ;i < m;i++){
scanf("%d%d%d",&u,&v,&color);
AddEdge(u,v,color);
}
rev_bfs();
bfs();
}
return ;
}

UVA1599-Ideal Path(BFS进阶)的更多相关文章

  1. UVa1599 Ideal Path(双向bfs&plus;字典序&plus;非简单图的最短路&plus;队列判重)

    题目大意: 对于一个n个房间m条路径的迷宫(Labyrinth)(2<=n<=100000, 1<=m<=200000),每条路径上都涂有颜色,颜色取值范围为1<=c&l ...

  2. UVA-1599 Ideal Path(双向BFS)

    题目: 给一个n个点m条边(2≤m≤100000, 1≤m≤200000)的无向图,每条边上都涂有一种颜色(用1到1000000000表示).求从结点1到结点n的一条路径, 使得经过的边数尽量少,在此 ...

  3. UVa1599&comma;Ideal Path

      说实话,这题参考的: http://blog.csdn.net/u013382399/article/details/38227917 倒着BFS就把我难住了T T,原来这样倒着BFS一遍,遍历完 ...

  4. 6-20 Ideal Path uva1599

    第一个bfs很快  但是我第一次做还用了结构体  这题完全不需要  反而导致了代码非常乱 输入: 一开始我是用m二维数组储存颜色  vector path来储存路径 但是二维数组的下标是不够用的   ...

  5. UVA 1599 Ideal Path(双向bfs&plus;字典序&plus;非简单图的最短路&plus;队列判重)

    https://vjudge.net/problem/UVA-1599 给一个n个点m条边(2<=n<=100000,1<=m<=200000)的无向图,每条边上都涂有一种颜色 ...

  6. UVA 1599 Ideal Path(bfs1&plus;bfs2,双向bfs)

    给一个n个点m条边(<=n<=,<=m<=)的无向图,每条边上都涂有一种颜色.求从结点1到结点n的一条路径,使得经过的边数尽量少,在此前提下,经过边的颜色序列的字典序最小.一对 ...

  7. Uva 1599 Ideal Path - 双向BFS

    题目连接和描述以后再补 这题思路很简单但还真没少折腾,前后修改提交了七八次才AC...(也说明自己有多菜了).. 注意问题: 1.看清楚原题的输入输出要求,刚了书上的中文题目直接开撸,以为输入输出都是 ...

  8. UVa 1599 Ideal Path (两次BFS)

    题意:给出n个点,m条边的无向图,每条边有一种颜色,求从结点1到结点n颜色字典序最小的最短路径. 析:首先这是一个最短路径问题,应该是BFS,因为要保证是路径最短,还要考虑字典序,感觉挺麻烦的,并不好 ...

  9. UVA 1599, POJ 3092 Ideal Path 理想路径 &lpar;逆向BFS跑层次图&rpar;

    大体思路是从终点反向做一次BFS得到一个层次图,然后从起点开始依次向更小的层跑,跑的时候选则字典序最小的,由于可能有多个满足条件的点,所以要把这层满足条件的点保存起来,在跑下一层.跑完一层就会得到这层 ...

随机推荐

  1. iOS 杂笔-25&lpar;不要用copy修饰NSMutableString)

    iOS 杂笔-25(不要用copy修饰NSMutableString) 首先对题目进行简单的解释,我所说的不要用copy修饰NSMutableString不是说完全不可以用.但是要清楚一点,既然使用N ...

  2. owin要跑起来

    必须安装 Microsoft.Owin.Host.SystemWeb

  3. 通讯录改造——MVC设计模式

    将之前用servlet写的程序转化为jsp+servlet的简单的MVC的三层结构.项目中程序的包如图 首先是实体对象: package com.contactSystem.entiey; publi ...

  4. 不同分辨率下获取不同js文件

    获取当前网站的目录  //js获取网站根路径(站点及虚拟目录),获得网站的根目录或虚拟目录的根地址 function getRootPath(){ //整个域名(如:http://vc3.cn/ind ...

  5. Oracle用户密码过期和用户被锁解决方法【转】

    [原因/触发因素] 确定是由于Oracle11g中默认在default概要文件中设置了“PASSWORD_LIFE_TIME=180天”所导致. [影响和风险] 影响 密码过期后,业务进程连接数据库异 ...

  6. JS实现鼠标移上去图片停止滚动移开恢复滚动效果

    这是在做个人站的时候展示项目成果,因为不光需要展示,还需要介绍详细内容,就在滚动展示的地方做了这个效果以便于点开想要看的项目. 首先,要做的是一个需要滚动的区域.我前边写过一个关于图片循环滚动的示例, ...

  7. luogu 3045 优先队列反悔&sol;bzoj 2590

    N头奶牛,价格Pi,K张优惠券,优惠券购买降为Ci,不超过M的钱最多可买多少奶牛 先将c值k小的加入,将它们省下的钱加入优先队列(省下的钱由少到多),在将k+1-n用p排序,再逐个与优先队列中弹出的比 ...

  8. G&colon; Dave的时空迷阵(next数组)

    G: Dave的时空迷阵 Time Limit: 1 s      Memory Limit: 128 MB Submit My Status Problem Description 皇家理工本部隐藏 ...

  9. linux 的常用命令---------第七阶段

       LVM 逻辑卷管理器  -----其作用为 :在线扩容 卷组 vG  (也叫LVM卷组) ------------------→     在此卷组vG上建立  :       逻辑卷组 LV ( ...

  10. 【文档】七、Mysql Binlog不同事件类型的事件内容

    下面主要讲述了每个类型的事件中的固定和可变部分的数据. Start_log_event_v3/START_EVENT_V3 这个事件出现在v1或v3的binlog文件的开头部分.对于4.0和4.1版本 ...