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 (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.
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 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: 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
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 Output
2 1 3
Source
題意:路徑最短,顏色字典序最小
從白書上看著的,用的白書做法
倒著bfs一遍得到層次圖
然後按層次圖bfs,每次選擇當前層次向下一層次中顏色最小的邊練的點加進下一層次集合中
速度還可以了
16 | 16123402(2) | thwfhk | 7320K | 3360MS | G++ | 2083B | 2016-09-25 23:16:16 |
// // main.cpp // poj3967 // // Created by Candy on 9/25/16. // Copyright © 2016 Candy. All rights reserved. // #include <iostream> #include <cstdio> #include <algorithm> #include <cstring> using namespace std; const int N=1e5+5,M=2e5+5,INF=1e9+5; inline int read(){ char c=getchar();int x=0,f=1; while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();} while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();} return x; } int n,m,u,v,w; struct edge{ int v,w,ne; }e[M<<1]; int h[N],cnt=0; inline void ins(int u,int v,int w){ cnt++; e[cnt].v=v;e[cnt].w=w;e[cnt].ne=h[u];h[u]=cnt; cnt++; e[cnt].v=u;e[cnt].w=w;e[cnt].ne=h[v];h[v]=cnt; } int vis[N],q[N],head=1,tail=0; int d[N]; void bfs1(){ q[++tail]=n;vis[n]=1; d[n]=1; while(head<=tail){ int u=q[head++]; for(int i=h[u];i;i=e[i].ne){ int v=e[i].v; if(vis[v]) continue; vis[v]=1; d[v]=d[u]+1; q[++tail]=v; } } } int ans[N],lst[N],num=0; void bfs2(){ memset(ans,127,sizeof(ans)); head=1;tail=0; memset(q,0,sizeof(q)); memset(vis,0,sizeof(vis)); q[++tail]=1; while(head<=tail||num>=1){ int mn=INF,dis=0;num=0; while(head<=tail){ int u=q[head++];dis=d[u]; //printf("u %d\n",u); for(int i=h[u];i;i=e[i].ne){ int v=e[i].v,c=e[i].w; if(d[v]!=d[u]-1) continue; if(c>mn) continue; if(c<mn){ num=0; mn=c; lst[++num]=v; }else lst[++num]=v; } } ans[dis]=mn; for(int i=1;i<=num;i++) if(!vis[lst[i]]){vis[lst[i]]=1;q[++tail]=lst[i];} } } int main(int argc, const char * argv[]) { n=read();m=read(); for(int i=1;i<=m;i++){ u=read();v=read();w=read(); if(u!=v) ins(u,v,w); } bfs1(); bfs2(); printf("%d\n",d[1]-1); for(int i=d[1];i>1;i--) printf("%d ",ans[i]); // cout<<"\n\n"; // for(int i=1;i<=n;i++) printf("%d ",d[i]); return 0; }