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 [UVA 1599] Ideal Path | 细节最短路_ide is lexicographically smaller than a sequence [UVA 1599] Ideal Path | 细节最短路_广度搜索_02 if there exists i such that [UVA 1599] Ideal Path | 细节最短路_ico_03 , and [UVA 1599] Ideal Path | 细节最短路_c++_04 for all [UVA 1599] Ideal Path | 细节最短路_ico_05.

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 ([UVA 1599] Ideal Path | 细节最短路_广度搜索_06, [UVA 1599] Ideal Path | 细节最短路_c++_07). 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 ([UVA 1599] Ideal Path | 细节最短路_广度搜索_08, [UVA 1599] Ideal Path | 细节最短路_c++_09

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.

Samples
Input Copy

4 6
1 2 1
1 3 2
3 4 3
2 3 1
2 4 4
3 1 1

Output

2
1 3

Source
UVA 1599
如果说从起点[UVA 1599] Ideal Path | 细节最短路_c++_10开始,一直向终点[UVA 1599] Ideal Path | 细节最短路_ide_11走,每次只是挑选边权(颜色)最小的走,可能无法到达,也可能找到的不是字典序最小的(考虑长度)

所以说可以先从终点[UVA 1599] Ideal Path | 细节最短路_ide_11向起点[UVA 1599] Ideal Path | 细节最短路_c++_10走,求出最短路(每条路径边权为[UVA 1599] Ideal Path | 细节最短路_c++_10),那么说到达起点[UVA 1599] Ideal Path | 细节最短路_c++_10的距离[UVA 1599] Ideal Path | 细节最短路_广度搜索_16便是路径上经过的颜色的个数
倒着求完最短路径之后,就可以利用已经有的[UVA 1599] Ideal Path | 细节最短路_广度搜索_17来正向寻找答案

细节的地方在于要先倒序求完最短路,然后正向获取答案

#define Clear(x,val) memset(x,val,sizeof x)
struct node {
int u, v, nex;
ll w;
}e[maxn << 2];
int n, m, cnt, head[maxn];
vector<int> ans;
void init(int x) {
cnt = 0;
for (int i = 0; i <= x; i++) {
head[i] = -1;
ans[i] = 0x3f3f3f3f;
}
}
void add(int u, int v, int w) {
e[cnt].u = u;
e[cnt].v = v;
e[cnt].w = w;
e[cnt].nex = head[u];
head[u] = cnt++;
}
ll dis[maxn];
bool vis[maxn];
void bfs() {
queue<int> que;
Clear(vis, 0);
Clear(dis, -1);
que.push(n);
dis[n] = 0, vis[n] = 1;
while (que.size()) {
int u = que.front();
que.pop();
for (int i = head[u]; ~i; i = e[i].nex) {
int to = e[i].v;
if (vis[to]) continue;
vis[to] = 1;
dis[to] = dis[u] + 1;
que.push(to);
}
}
}
bool in[maxn];
void getAns() {
Clear(in, 0);
Clear(vis, 0);
queue<int> que;
que.push(1);
//vis[1] = 1;
while (que.size()) {
int u = que.front();
que.pop();
vis[u] = 1;
if (u == n) return;
int minCol = 0x3f3f3f3f;
for (int i = head[u]; ~i; i = e[i].nex) {
int col = e[i].w;
int to = e[i].v;
//debug(col);
if (!vis[to] && col < minCol && dis[to] == dis[u] - 1) minCol = col;
}
//debug(minCol);
for (int i = head[u]; ~i; i = e[i].nex) {
int col = e[i].w;
int to = e[i].v;
if (!vis[to] && dis[to] == dis[u] - 1 && col == minCol && !in[to]) {
que.push(to);
in[to] = 1;
}
}
//cout << dis[u] << " " << minCol << endl;
int pos = dis[1] - dis[u];
ans[pos] = min(minCol, ans[pos]);
}
}
int main() {
while (cin >> n >> m) {
ans = vector<int>(n + 10);
//cout << ans.size() << endl;
init(n);
//puts("ok");
for (int i = 1; i <= m; i++) {
int u = read, v = read, w = read;
add(u, v, w);
add(v, u, w);
}
//puts("ok");
ans.clear();
bfs();
//puts("ok");
//cout << dis[1] << endl;
getAns();
printf("%d\n", dis[1]);
for (int i = 0; i < dis[1]; i++) {
printf("%d%c", ans[i], (i == dis[1] - 1 ? '\n' : ' '));
}
}
return 0;
}
/**
4 6
1 2 1
1 3 2
3 4 3
2 3 1
2 4 4
3 1 1

2
1 3

26862488
**/