Problem UVA1599-Ideal Path

Time Limit: 3000 mSec

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.

 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.

 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 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+字典序+非简单图的最短路+队列判重)

    题目大意: 对于一个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,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+字典序+非简单图的最短路+队列判重)

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

  6. UVA 1599 Ideal Path(bfs1+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 理想路径 (逆向BFS跑层次图)

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

随机推荐

  1. Angular2入门:TypeScript的类型 - 类型、null、undefined

  2. IntelliJ IDEA 2018.3 for Mac 注册码激活

    一.前往 jetbrains 官网下载 IDEA Ultimate版本,地址: https://www.jetbrains.com/idea/download/#section=mac 二.安装 ID ...

  3. 页面滚动显示或隐藏元素Headroom.js插件帮助你实现滚动效果

    Headroom.js 是什么? Headroom.js 是一个轻量级.高性能的JS小工具(不依赖任何工具库!),它能在页面滚动时做出响应.此页面顶部的导航条就是一个鲜活的案例,当页面向下滚动时,导航 ...

  4. SpringBoot之Mybatis操作中使用Redis做缓存

    上一博客学习了SpringBoot集成Redis,今天这篇博客学习下Mybatis操作中使用Redis做缓存.这里其实主要学习几个注解:@CachePut.@Cacheable.@CacheEvict ...

  5. 运行 svgatest 显示 mmap /dev/zero Permission denied 解决办法

    答案是我在这个网站上找到的: 执行 xset dpms force off 命令就可以解决掉这个问题. 再次运行 svgatest 程序,得到了预期的结果,perfect!

  6. Vue 动态加载组件

    为什么要动态加载呢?而不是一次性加载呢? 一次性?你能保证你拿的内容不多,那从性能方面说还是OK的.否则,就该什么时候用,就什么时候取. 得出这想法,源于前几天上班赶产品的故事: A组件是父亲,B组件 ...

  7. IdentityServer4-Resource定义-翻译

    资源定义(Defining Resource) 通常,第一件事是定义那些你想保护的资源.这些资源可能是你的用户信息,比如个人数据,电子邮件或者对Api的访问. Note: 你可以用C#实体类来定义资源 ...

  8. (3)Microsoft office Word 2013版本操作入门_段落设定

    1.查看文件: 打开word查看左下角 会显示 word一共有多少页,当前第几页,共多少字等,如下图所示 2.word快速翻页: Ctrl+PageDown  向下翻页, Ctrl+PageUp 向上 ...

  9. What is The Rule of Three?

    Question: What does copying an object mean? What are the copy constructor and the copy assignment op ...

  10. JDK动态代理与CGLib动态代理相关问题

    导读: 1.JDK动态代理原理是什么?为什么不支持类的代理? 2.JDK动态代理实例 3.CGLib代理原理是什么? 4.CGLib代理实例 5.JDK动态代理与CGLib代理的区别是什么? 6.总结 ...