Ideal Path
Time Limit: 10000MS   Memory Limit: 65536K
Total Submissions: 1754   Accepted: 240

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 (a1a2, . . . , ak) is lexicographically smaller than a sequence (b1b2, . . . , bk) if there exists i such that ai < bi, and aj = bj for all j < i.

Input

The first line of the input file contains integers n and m —the number of rooms and passages, respectively (2 <= n <= 100 000, 1 <= m <= 200 000). The following m lines describe passages, each passage is described with three integer numbers: aibi, and ci — the numbers of rooms it connects and its color (1 <= aibi <= 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

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. 4 6
  2. 1 2 1
  3. 1 3 2
  4. 3 4 3
  5. 2 3 1
  6. 2 4 4
  7. 3 1 1

Sample Output

  1. 2
  2. 1 3

Source


题意:路径最短,颜色字典序最小

从白书上看着的,用的白书做法
倒着bfs一遍得到层次图
然后按层次图bfs,每次选择当前层次向下一层次中颜色最小的边练的点加进下一层次集合中
 
速度还可以了

16 16123402(2) thwfhk 7320K 3360MS G++ 2083B 2016-09-25 23:16:16
  1. //
  2. // main.cpp
  3. // poj3967
  4. //
  5. // Created by Candy on 9/25/16.
  6. // Copyright © 2016 Candy. All rights reserved.
  7. //
  8.  
  9. #include <iostream>
  10. #include <cstdio>
  11. #include <algorithm>
  12. #include <cstring>
  13. using namespace std;
  14. const int N=1e5+,M=2e5+,INF=1e9+;
  15. inline int read(){
  16. char c=getchar();int x=,f=;
  17. while(c<''||c>''){if(c=='-')f=-;c=getchar();}
  18. while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
  19. return x;
  20. }
  21. int n,m,u,v,w;
  22. struct edge{
  23. int v,w,ne;
  24. }e[M<<];
  25. int h[N],cnt=;
  26. inline void ins(int u,int v,int w){
  27. cnt++;
  28. e[cnt].v=v;e[cnt].w=w;e[cnt].ne=h[u];h[u]=cnt;
  29. cnt++;
  30. e[cnt].v=u;e[cnt].w=w;e[cnt].ne=h[v];h[v]=cnt;
  31. }
  32. int vis[N],q[N],head=,tail=;
  33. int d[N];
  34. void bfs1(){
  35. q[++tail]=n;vis[n]=;
  36. d[n]=;
  37. while(head<=tail){
  38. int u=q[head++];
  39. for(int i=h[u];i;i=e[i].ne){
  40. int v=e[i].v;
  41. if(vis[v]) continue;
  42. vis[v]=;
  43. d[v]=d[u]+;
  44. q[++tail]=v;
  45. }
  46. }
  47. }
  48. int ans[N],lst[N],num=;
  49. void bfs2(){
  50. memset(ans,,sizeof(ans));
  51. head=;tail=;
  52. memset(q,,sizeof(q));
  53. memset(vis,,sizeof(vis));
  54. q[++tail]=;
  55. while(head<=tail||num>=){
  56. int mn=INF,dis=;num=;
  57. while(head<=tail){
  58. int u=q[head++];dis=d[u]; //printf("u %d\n",u);
  59. for(int i=h[u];i;i=e[i].ne){
  60. int v=e[i].v,c=e[i].w;
  61. if(d[v]!=d[u]-) continue;
  62. if(c>mn) continue;
  63. if(c<mn){
  64. num=; mn=c;
  65. lst[++num]=v;
  66. }else lst[++num]=v;
  67. }
  68. }
  69. ans[dis]=mn;
  70. for(int i=;i<=num;i++)
  71. if(!vis[lst[i]]){vis[lst[i]]=;q[++tail]=lst[i];}
  72. }
  73. }
  74. int main(int argc, const char * argv[]) {
  75. n=read();m=read();
  76. for(int i=;i<=m;i++){
  77. u=read();v=read();w=read();
  78. if(u!=v) ins(u,v,w);
  79. }
  80. bfs1();
  81. bfs2();
  82. printf("%d\n",d[]-);
  83. for(int i=d[];i>;i--) printf("%d ",ans[i]);
  84. // cout<<"\n\n";
  85. // for(int i=1;i<=n;i++) printf("%d ",d[i]);
  86. return ;
  87. }
 

POJ3967Ideal Path[反向bfs 层次图]的更多相关文章

  1. hdu 1689 Alien’s Necklace (bfs层次图剪枝)

    Alien's Necklace Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  2. UVA 1599, POJ 3092 Ideal Path 理想路径 (逆向BFS跑层次图)

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

  3. 【HDU - 1043】Eight(反向bfs+康托展开)

    Eight Descriptions: 简单介绍一下八数码问题:在一个3×3的九宫格上,填有1~8八个数字,空余一个位置,例如下图: 1 2 3 4 5 6 7 8   在上图中,由于右下角位置是空的 ...

  4. UVA1599-Ideal Path(BFS进阶)

    Problem UVA1599-Ideal Path Time Limit: 3000 mSec Problem Description New labyrinth attraction is ope ...

  5. HDU 1043 Eight(反向BFS+打表+康托展开)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1043 题目大意:传统八数码问题 解题思路:就是从“12345678x”这个终点状态开始反向BFS,将各 ...

  6. POJ1077 Eight —— 反向BFS

    主页面:http://www.cnblogs.com/DOLFAMINGO/p/7538588.html 代码一:以数组充当队列,利用结构体中的pre追溯上一个状态在数组(队列)中的下标: #incl ...

  7. BZOJ_1195_[HNOI2006]最短母串_AC自动机+BFS+分层图

    BZOJ_1195_[HNOI2006]最短母串_AC自动机+BFS+分层图 Description 给定n个字符串(S1,S2,„,Sn),要求找到一个最短的字符串T,使得这n个字符串(S1,S2, ...

  8. UVa 1599 理想路径(反向BFS 求最短路径 )

    题意: 给定一个有重边有自环的无向图,n个点(2 <= n <= 100000), m条边(1 <= m <= 200000), 每条边有一个权值, 求从第一个点到n的最少步数 ...

  9. 使用Architecture Explorer分析应用程序及使用层次图

    使用Architecture Explorer分析应用程序 Architecture Explorer和依赖图可以帮助我们了解所有的项目,包括小项目和大项目.Architecture Explorer ...

随机推荐

  1. requirejs:杏仁的优化(almond)

    这里只是调侃一下,“杏仁”其实指的是almond,requirejs作者的另一个开源项目,它的定位是作为requirejs的一个替代品. 本文概要: 1. 使用场景 2. 打包例子:未使用almond ...

  2. Flex布局窥探(一)

    一.Flex布局是神马? Flex是Flexible Box的缩写,意为‘弹性布局’,用来为盒模型提供最大的灵活性. 任何容器都能被指定为Flex布局: .box{ display: flex; } ...

  3. HTML基础知识总结

    经过这段时间的学习,对于html的一些基础知识有了一定的了解.所谓好记性不如烂笔头,唯有一点点累积,才能汇聚成知识的海洋.现在,我对这段时间的学习做一个总结. 一.HTML的定义 HTML,超文本标记 ...

  4. 读jQuery源码 - Deferred

    Deferred首次出现在jQuery 1.5中,在jQuery 1.8之后被改写,它的出现抹平了javascript中的大量回调产生的金字塔,提供了异步编程的能力,它主要服役于jQuery.ajax ...

  5. AWS EC2 复制实例后,自定义指标无法显示数据

    从一个实例创建了一个AMI,然后通过这个AMI创建新的EC2实例,结果发票自定义指标不会显示: 系统一直在邮件中提示: print() on closed filehandle MDATA at Cl ...

  6. 极其简单的搭建eclipse的android开发环境

    这篇博客是关于如何搭建eclipse的android开发环境, 与网上的其他博客不同,我的方法比他们简单的多,所 以推荐给大家. 搭建eclipse的android开发环境步骤: 1.配置JDK(Ja ...

  7. 查看Android系统给APP分配的最大堆栈

    命令方式: cat /system/build.prop dalvik.vm.heapgrowthlimit=48m dalvik.vm.heapsize=128m 代码方式: Runtime rt= ...

  8. 从Eclipse迁移到Android Studio

    Google正式推出了Android Studio 1.0,Android默认的开发工具也由Eclipse变成了intellij,对Eclipse的支持肯定会越来越少了,对于Android开发者来说, ...

  9. NSFileManeger

    #define PATH @"/Users/wenhua/testdir" // 删除, 复制 剪切  这些行为都是管理文件的行为 //创建文件 void createFile(v ...

  10. 后台管理UI皮肤的选择

    后台管理UI的选择 目录 一.EasyUI 二.DWZ JUI 三.HUI 四.BUI 五.Ace Admin 六.Metronic 七.H+ UI 八.Admin LTE 九.INSPINIA 十. ...