Ideal Path UVA

来源:互联网 发布:淘宝双12怎么报名 编辑:程序博客网 时间:2024/04/29 15:30

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.


题目简单来说就是找出从点1到点n最短路中边集合字典序最小的一个,用到了多次bfs,前面的两面bfs用来寻找所有的最短路,最后一次bfs用来在这些最短路中寻找字典序最小的。前面两边bfs分别求出了所有点离点1和点n的距离,所以对于点x,若disto1[x]+diston[x]==disto1[n],说明这个点出现在一条最短路上。剩下的问题就是找出字典序最小的最短路了,从点1开始找与其相连且是最短路的边,找出最小的边,把所有边为这个最小值的相邻的点入队,再去寻找下一条,必须把所有边遍历一边后才能确定到底哪个才是字典序最小的,所以我用了两个队列保存,具体见代码bfs1()

(忘了清理存边的vector导致坑了好久= =)

#include<iostream>#include<algorithm>#include<string.h>#include<stdio.h>#include<vector>#include<queue>using namespace std;#define ll long long#define inf 0x3f3f3f3fint n, m;queue<int> que;queue<int> que1;int ans_step[100005];vector<pair<int, int> > e[100005];int frombeg[100005], fromend[100005];void bfs(int* node){    while (!que.empty()){        int cur = que.front(); que.pop();        for (int i = 0; i< e[cur].size(); i++){            int v = e[cur][i].first;            if (node[v]>node[cur] + 1){                node[v] = node[cur] + 1;                que.push(v);            }        }    }}bool vis[100005];void bfs1(int kk){    if (kk == frombeg[n] + 1)return;    while (!que.empty()){        int cur = que.front(); que.pop();        que1.push(cur);        for (int i = 0; i<e[cur].size(); i++){            int v = e[cur][i].first;            if (frombeg[v] != kk)continue;            if (frombeg[v] + fromend[v] == frombeg[n]){                ans_step[kk] = min(ans_step[kk], e[cur][i].second);            }        }    }    while (!que1.empty()){        int cur = que1.front(); que1.pop();        for (int i = 0; i<e[cur].size(); i++){            if (e[cur][i].second == ans_step[kk] && frombeg[e[cur][i].first] == kk&&!vis[e[cur][i].first]){                vis[e[cur][i].first] = 1;                que.push(e[cur][i].first);            }        }    }    bfs1(kk + 1);}int main(){    int x, y, z;    while (~scanf("%d%d", &n, &m)){        memset(frombeg, 0x3f, sizeof(frombeg));        memset(fromend, 0x3f, sizeof(fromend));        memset(vis, 0, sizeof(vis));        memset(ans_step, 0x3f, sizeof(ans_step));        while (!que.empty())que.pop();        for (int i = 1; i <= n; i++)e[i].clear();        for (int i = 0; i<m; i++){            scanf("%d%d%d", &x, &y, &z);            e[x].push_back(make_pair(y, z));            e[y].push_back(make_pair(x, z));        }        que.push(1);        frombeg[1] = 0;        bfs(frombeg);        fromend[n] = 0;        que.push(n);        bfs(fromend);        int ans = frombeg[n];        printf("%d\n", ans);        que.push(1);        bfs1(1);        for (int i = 1; i <= ans; i++){            printf("%d", ans_step[i]);            if (i<ans)printf(" ");        }        printf("\n");    }    return 0;}
0 0