UVA - 1599 Ideal Path
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$ \le$n$ \le$100000, 1$ \le$m$ \le$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$ \le$ai, bi$ \le$n, 1$ \le$ci$ \le$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 nfrom 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 Output
2
1 3
紫书上的原题。
思路:
先通过反着走一遍bfs,求出每个顶点到终点的最短长度。
然后正着走一遍bfs,求出符合d[u]=d[v]+1顶点中的最短距离,并将最短距离的顶点压入队列。
通过ans数组来记录字典序。
代码如下:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;
const int maxn=1e5+5;
const int INF=0x3f3f3f3f;
//邻接表
int head[maxn];
int n,m;
//到终点的最短距离
int d[maxn];
//是否被访问过
int vis[maxn];
//最短字典序数组
int ans[maxn];
struct edge
{
int to;
int next;
int col;
}edge[maxn<<2];
void add (int id,int u,int v,int col)
{
edge[id].to=v;
edge[id].col=col;
edge[id].next=head[u];
head[u]=id;
}
//正向bfs
void zbfs()
{
queue<int>q;
int now,next;
memset (vis,0,sizeof(vis));
q.push(1);
vis[1]=1;
for (int i=0;i<maxn;i++) ans[i]=INF;
while (!q.empty())
{
now=q.front();q.pop();
if(now==n) continue;
int Min=INF;
//求出符合要求的最小距离
for (int i=head[now];i!=-1;i=edge[i].next)
{
next=edge[i].to;
if(d[next]+1==d[now])
{
Min=min(Min,edge[i].col);
}
}
//将最小距离的顶点压入队列
for (int i=head[now];i!=-1;i=edge[i].next)
{
next=edge[i].to;
if(!vis[next]&&d[next]+1==d[now]&&Min==edge[i].col)
{
q.push(next);
vis[next]=1;
}
}
//求出位于第几个位置
int step=d[1]-d[now];
//更新
ans[step]=min(Min,ans[step]);
//printf("step=%d Min=%d ans=%d\n",step,Min,ans[step]);
}
}
//反向bfs
void fbfs()
{
queue<int>q;
memset (vis,0,sizeof(vis));
d[n]=0;
q.push(n);
vis[n]=1;
int now,next;
while (!q.empty())
{
now=q.front();q.pop();
for(int i=head[now];i!=-1;i=edge[i].next)
{
next=edge[i].to;
if(!vis[next])
{
d[next]=d[now]+1;
q.push(next);
vis[next]=1;
}
}
}
/*for (int i=1;i<=n;i++)
{
printf("%d ",d[i]);
}
printf("\n-------\n");*/
}
int main()
{
while (scanf("%d%d",&n,&m)!=EOF)
{
memset (head,-1,sizeof(head));
for (int i=0,id=0;i<m;i++)
{
int x,y,col;
scanf("%d%d%d",&x,&y,&col);
if(x!=y)
{
add(++id,x,y,col);
add(++id,y,x,col);
}
}
fbfs();
zbfs();
printf("%d\n",d[1]);
for (int i=0;i<d[1];i++)
{
printf("%d%c",ans[i],i==d[1]-1?'\n':' ');
}
}
return 0;
}