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

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!

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <cstdlib>
  5 #include <queue>
  6 #include <vector>
  7 #define INF 0x3f3f3f3f
  8 using namespace std;
  9 
 10 const int maxn = 100000+10,maxe = 200000+10;
 11 
 12 struct Edge{
 13     int to,next,color;
 14     Edge(int to = 0,int next = 0,int color = 0) :
 15         to(to),next(next),color(color) {}
 16 }edge[maxe<<1];
 17 
 18 int tot,head[maxn];
 19 int dist[maxn];
 20 int n,m;
 21 
 22 void AddEdge(int u,int v,int color){
 23     edge[tot] = Edge(v,head[u],color);
 24     head[u] = tot++;
 25     edge[tot] = Edge(u,head[v],color);
 26     head[v] = tot++;
 27 }
 28 
 29 void rev_bfs(){
 30     memset(dist,-1,sizeof(dist));
 31     queue<int> que;
 32     que.push(n);
 33     dist[n] = 0;
 34     while(!que.empty()){
 35         int v = que.front();que.pop();
 36         for(int i = head[v];i != -1;i = edge[i].next){
 37             int u = edge[i].to;
 38             if(dist[u] < 0){
 39                 dist[u] = dist[v]+1;
 40                 que.push(u);
 41             }
 42         }
 43     }
 44 }
 45 
 46 bool vis[maxn];
 47 vector<int> ans;
 48 
 49 void bfs(){
 50     memset(vis,false,sizeof(vis));
 51     ans.clear();
 52     vector<int> Next;
 53     Next.push_back(1);
 54     vis[1] = true;
 55     for(int i = 0;i < dist[1];i++){
 56         int Min_color = INF;
 57         for(int j = 0;j < (int)Next.size();j++){
 58             int u = Next[j];
 59             for(int i = head[u];i != -1;i = edge[i].next){
 60                 int v = edge[i].to;
 61                 if(dist[u] == dist[v]+1){
 62                     Min_color = Min_color < edge[i].color ? Min_color : edge[i].color;
 63                 }
 64             }
 65         }
 66         ans.push_back(Min_color);
 67         vector<int> Next2;
 68         for(int j = 0;j < (int)Next.size();j++){
 69             int u = Next[j];
 70             for(int i = head[u];i != -1;i = edge[i].next){
 71                 int v = edge[i].to;
 72                 if(dist[u]==dist[v]+1 && !vis[v] && edge[i].color==Min_color){
 73                     vis[v] = true;
 74                     Next2.push_back(v);
 75                 }
 76             }
 77         }
 78         Next = Next2;
 79     }
 80     printf("%d
",ans.size());
 81     printf("%d",ans[0]);
 82     for(int i = 1;i < (int)ans.size();i++){
 83         printf(" %d",ans[i]);
 84     }
 85     printf("
");
 86 }
 87 
 88 void init(){
 89     tot = 0;
 90     memset(head,-1,sizeof(head));
 91 }
 92 
 93 int main()
 94 {
 95     //freopen("input.txt","r",stdin);
 96     while(scanf("%d%d",&n,&m) == 2){
 97         init();
 98         int u,v,color;
 99         for(int i = 0;i < m;i++){
100             scanf("%d%d%d",&u,&v,&color);
101             AddEdge(u,v,color);
102         }
103         rev_bfs();
104         bfs();
105     }
106     return 0;
107 }
原文地址:https://www.cnblogs.com/npugen/p/9515419.html