首页 > 代码库 > POJ3967Ideal Path[反向bfs 层次图]
POJ3967Ideal Path[反向bfs 层次图]
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 61 2 11 3 23 4 32 3 12 4 43 1 1
Sample Output
21 3
Source
Northeastern Europe 2010
题意:路径最短,颜色字典序最小
从白书上看着的,用的白书做法
倒着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;}
POJ3967Ideal Path[反向bfs 层次图]
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。