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. [POI2004] SZN

    Description 给定\(N(N\leq 10000)\)个点的树,要求用最少的路径覆盖树边.路径之间可以有交点,不能有交边.问最少需要几条路径以及在第一问的基础上最长的路径最短是多少? Sol ...

  2. 如何查看oracle用户具有的权限和角色

    .查看所有用户: select * from dba_users; select * from all_users; select * from user_users; .查看用户或角色系统权限(直接 ...

  3. 基于Visual Studio .NET2015的单元测试

    基于Visual Studio .NET2015的单元测试 1.    在Visual Studio .NET2015中创建任意项目. 2.    在某个公共类的公共方法的名称上面点击右键,选择“创建 ...

  4. Jjava8 Lambda 神操作

    public class Lambda { @FunctionalInterface public interface AddInter { void add(int x, long y); } pu ...

  5. react-router与react-router-dom使用时的区别

    1.React-router与React-router-dom的API对比 React-router:提供了router的核心api.如Router.Route.Switch等,但没有提供有关dom操 ...

  6. Tomcat 8默认工具manager管理页面访问配置

    Tomcat 8默认工具manager管理页面访问配置 1. 分配相关的角色权限 需要配置的配置文件是${catalina.home}/conf/tomcat-users.xml先给Tomcat访问相 ...

  7. 常用Linux 服务器命令--各种性能指标命令

    如果你想知道你的服务器正在做干什么,你就需要了解一些基本的命令,一旦你精通了这些命令,那你就是一个专业的 Linux 系统管理员. 监控命令## iostat### iostat命令用来显示存储系统的 ...

  8. scrapy爬取校花网男神图片保存到本地

    爬虫四部曲,本人按自己的步骤来写,可能有很多漏洞,望各位大神指点指点 1.创建项目 scrapy startproject xiaohuawang scrapy.cfg: 项目的配置文件xiaohua ...

  9. Python3 下实现 腾讯人工智能API 调用

    1.背景 a.鹅厂近期发布了自己的人工智能 api,包括身份证ocr.名片ocr.文本分析等一堆API,因为前期项目用到图形OCR,遂实现试用了一下,发现准确率还不错,放出来给大家共享一下. b.基于 ...

  10. 字符串通过在配置文件配置三个key来进行加密解密

    在这里和大家分享一个加密util,相对于md5加密相信大家都已经很熟悉了吧,md5是不可逆的一种加密方式,虽说不可逆但是网上已经有了破解的方法,我这边分享一个免费的破解 网址给大家:https://w ...