1599 - Ideal Path

来源:互联网 发布:java外包 编辑:程序博客网 时间:2024/04/30 18:49


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 isuch 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 numbern 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 11 3 23 4 32 3 12 4 43 1 1

Sample Output 

2 1 3
真心不错的的一题,从暑假就开始看到这一题了,直到今天才搞定;唯一比较可惜的就是看着题解搞定的。
首先从终点倒着BFS,记录每一点到终点的最短路径d[i],然后在从起点开始走,每到达一个新节点保证d值恰好减少1,直到终点;
虽说是这样但也要注意好多的细节
#include <iostream>#include<cstdio>#include<cstring>#include<vector>#include<algorithm>#include<queue>#include<vector>using namespace std;#define MAXN 100000+5#define MAXN2 1000000000+5;vector<int> g[MAXN];vector<int> c[MAXN];int d[MAXN],ans[MAXN];bool vis[MAXN];void bfs(int x){    queue<int> q;    q.push(x);    while(q.size())    {        int u=q.front();        q.pop();        int len=g[u].size();        for(int i=0;i<len;i++)        {            int v=g[u][i];            if(v==1)            {                d[v]=d[u]+1;                printf("%d\n",d[1]);                return ;            }            else if(d[v]>d[u]+1)            {                d[v]=d[u]+1;                q.push(v);            }        }    }}void bfs2(){    queue<int> q;    q.push(1);    while(q.size())    {        int u=q.front();        q.pop();        if(d[u]==0) return;        int len=c[u].size();        int min_color=MAXN2;        for(int i=0;i<len;i++)        {            int v=g[u][i];            if(d[v]==d[u]-1)            {                min_color=min(min_color,c[u][i]);            }        }        int t=d[1]-d[u];     //这个处理的好,本来想设个变量逐步累加        ans[t]=min(ans[t],min_color); //更新        for(int i=0;i<len;i++)              {            int v=g[u][i];            if(vis[v]==false && d[v]==d[u]-1 && c[u][i]==min_color) //这个也是细节            {                q.push(v);                vis[v]=true;            }        }    }    return ;}int main(){    int n,m;    while(scanf("%d%d",&n,&m)!=EOF)    {        for(int i=0;i<MAXN;i++)        {            g[i].clear();            c[i].clear();        }        for(int i=1;i<=m;i++)        {            int a,b,c1;            scanf("%d%d%d",&a,&b,&c1);        //    if(a==b) continue;            g[a].push_back(b);            g[b].push_back(a);            c[a].push_back(c1);   //从a出发颜色可以是c1,从b出发的颜色也可以是c1            c[b].push_back(c1);        }        for(int i=0;i<=n;i++)        {            d[i]=MAXN;            ans[i]=MAXN2;            vis[i]=false;        }        d[n]=0;        bfs(n);        bfs2();        printf("%d",ans[0]);        for(int i=1;i<d[1];i++)        {            printf(" %d",ans[i]);        }        printf("\n");    }    return 0;}

0 0
原创粉丝点击