UVA1599-Ideal Path(BFS进阶)
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
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进阶)的更多相关文章
- UVa1599 Ideal Path(双向bfs+字典序+非简单图的最短路+队列判重)
题目大意: 对于一个n个房间m条路径的迷宫(Labyrinth)(2<=n<=100000, 1<=m<=200000),每条路径上都涂有颜色,颜色取值范围为1<=c&l ...
- UVA-1599 Ideal Path(双向BFS)
题目: 给一个n个点m条边(2≤m≤100000, 1≤m≤200000)的无向图,每条边上都涂有一种颜色(用1到1000000000表示).求从结点1到结点n的一条路径, 使得经过的边数尽量少,在此 ...
- UVa1599,Ideal Path
说实话,这题参考的: http://blog.csdn.net/u013382399/article/details/38227917 倒着BFS就把我难住了T T,原来这样倒着BFS一遍,遍历完 ...
- 6-20 Ideal Path uva1599
第一个bfs很快 但是我第一次做还用了结构体 这题完全不需要 反而导致了代码非常乱 输入: 一开始我是用m二维数组储存颜色 vector path来储存路径 但是二维数组的下标是不够用的 ...
- UVA 1599 Ideal Path(双向bfs+字典序+非简单图的最短路+队列判重)
https://vjudge.net/problem/UVA-1599 给一个n个点m条边(2<=n<=100000,1<=m<=200000)的无向图,每条边上都涂有一种颜色 ...
- UVA 1599 Ideal Path(bfs1+bfs2,双向bfs)
给一个n个点m条边(<=n<=,<=m<=)的无向图,每条边上都涂有一种颜色.求从结点1到结点n的一条路径,使得经过的边数尽量少,在此前提下,经过边的颜色序列的字典序最小.一对 ...
- Uva 1599 Ideal Path - 双向BFS
题目连接和描述以后再补 这题思路很简单但还真没少折腾,前后修改提交了七八次才AC...(也说明自己有多菜了).. 注意问题: 1.看清楚原题的输入输出要求,刚了书上的中文题目直接开撸,以为输入输出都是 ...
- UVa 1599 Ideal Path (两次BFS)
题意:给出n个点,m条边的无向图,每条边有一种颜色,求从结点1到结点n颜色字典序最小的最短路径. 析:首先这是一个最短路径问题,应该是BFS,因为要保证是路径最短,还要考虑字典序,感觉挺麻烦的,并不好 ...
- UVA 1599, POJ 3092 Ideal Path 理想路径 (逆向BFS跑层次图)
大体思路是从终点反向做一次BFS得到一个层次图,然后从起点开始依次向更小的层跑,跑的时候选则字典序最小的,由于可能有多个满足条件的点,所以要把这层满足条件的点保存起来,在跑下一层.跑完一层就会得到这层 ...
随机推荐
- .Net敏捷开发框架6.1.6.2版本,联系QQ:6539471
演示地址:www.fishcmonkey.com .NET敏捷开发框架 6.1.6.2 版本发布 新增手机流程-我的流程(可查看流程进度和表单内容) 新增手机流程-待办任务(可查看流程进度和表单内容, ...
- LeetCode 键盘行-Python3.7<四>
500. 键盘行 题目网址:https://leetcode-cn.com/problems/keyboard-row/hints/ 给定一个单词列表,只返回可以使用在键盘同一行的字母打印出来的单词. ...
- 【RabbitMQ】6、rabbitmq生产者的消息确认
通过Publisher Confirms and Returns机制,生产者可以判断消息是否发送到了exchange及queue,而通过消费者确认机制,Rabbitmq可以决定是否重发消息给消费者,以 ...
- 8.并发容器ConcurrentHashMap#put方法解析
jdk1.7.0_79 HashMap可以说是每个Java程序员用的最多的数据结构之一了,无处不见它的身影.关于HashMap,通常也能说出它不是线程安全的.这篇文章要提到的是在多线程并发环境下的Ha ...
- GDB使用技巧
最近使用GDB比较多,发现除了最常用的run.break.continue.next等命令的基本用法外,还有一些非常有用的命令和用法,能让你更加得心应手地使用GDB,在这里做了一下简单的总结. 1. ...
- git常用命令以及如何与fork别人的仓库保持同步
简单常用命令1.git status查看当前仓库是否有文件改动a:提示Your branch is up-to-date with 'origin/master'.nothing to commit, ...
- JS的DOM操作 - 你真的了解吗?
摘要 想稍微系统的说说对于DOM的操作,把Javascript和jQuery常用操作DOM的内容归纳成思维导图方便阅读,同时加入性能上的一些问题. 前言 在前端开发的过程中,javascript极为重 ...
- 【代码笔记】Web-HTML-段落
一,效果图. 二,代码. <!DOCTYPE html> <html> <head> <meta charset="utf-8"> ...
- 【图解】Web前端实现类似Excel的电子表格
本文将通过图解的方式,使用纯前端表格控件 SpreadJS 来一步一步实现在线的电子表格产品(例如可构建Office 365 Excel产品.Google的在线SpreadSheet). 工具简介: ...
- Java并发编程(十三)线程间协作的两种方式:wait、notify、notifyAll和Condition
在现实中,需要线程之间的协作.比如说最经典的生产者-消费者模型:当队列满时,生产者需要等待队列有空间才能继续往里面放入商品,而在等待的期间内,生产者必须释放对临界资源(即队列)的占用权.因为生产者如果 ...