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得到一个层次图,然后从起点开始依次向更小的层跑,跑的时候选则字典序最小的,由于可能有多个满足条件的点,所以要把这层满足条件的点保存起来,在跑下一层.跑完一层就会得到这层 ...
随机推荐
- net4log 添加自定义变量
在log4net.config中 <parameter> <parameterName value="@czyid" /> <dbType value ...
- Visual Studio Ultimate 2013
简体中文版 SHA-1: 07313542D36ED8BEEF18520AA4F15E33E32C7F77 http://download.microsoft.com/download/0/7/5/0 ...
- STM32F4 MDK5软件仿真 error : no 'read' permission
问题描述 CPU:STM32F407 MDK5软件模拟提示没有读写权限,只能一步一步运行.提示代码如下: *** error 65: access violation at 0x40023800 : ...
- 【RabbitMQ】2、心得总结,资料汇总
Spring AMQP中文文档 http://ju.outofmemory.cn/entry/320538 云栖社区 https://yq.aliyun.com/search?q=rabbitm ...
- java数据写入Excel
正好最近公司要写一个对账的功能,后台用java从银行获得对账信息,数据是json类型的,然后写入excel中发送给一卡通中心的服务器上,网上找了很多代码,然后整合和改正,代码如下. import ja ...
- Vue 系列之 样式相关
Class 与 Style 绑定 动态修改元素样式 <head> <meta charset="utf-8" /> <meta http-equiv= ...
- JavaScript复杂判断的更优雅写法
摘要: 写代码是一门艺术. 原文:JavaScript 复杂判断的更优雅写法 作者:Think. 公众号:大转转fe Fundebug经授权转载,版权归原作者所有. 前提 我们编写js代码时经常遇到复 ...
- 9种网页Flash焦点图和jQuery焦点图幻灯片
jQuery图标放大轮播焦点图 Flash图片焦点图滑动切换 Flash右侧焦点图上下滑动切换 左右按钮滑动切换的网页幻灯片 双图同时滑动切换的焦点图 含有上下按钮的双图同时滑动切换的焦点图 常见的j ...
- “一切都是消息”--iMSF(即时消息服务框架)之【发布-订阅】模式
MSF的名字是 Message Service Framework 的简称,由于目前框架主要功能在于处理即时(immediately)消息,所以iMSF就是 immediately Message S ...
- 一个Web页面的问题分析
几个月之前我接到一个新的开发任务,要在一个旧的Web页面上面增添一些新的功能.在开发的过程中发现旧的代码中有很多常见的不合适的写法,结合这些问题,如何写出更好的,更规范的,更可维护的代码,就是这篇文章 ...