[UVA 1599] Ideal Path | 细节最短路
Posted PushyTao
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[UVA 1599] Ideal Path | 细节最短路相关的知识,希望对你有一定的参考价值。
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 ( a 1 , a 2 , … , a k ) (a1,a2,…,ak) (a1,a2,…,ak) is lexicographically smaller than a sequence ( b 1 , b 2 , … , b k ) (b1,b2,…,bk) (b1,b2,…,bk) if there exists i such that a i < b i ai<bi ai<bi , and a j = b j aj=bj aj=bj for all j < i j<i 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 2≤n≤100000 2≤n≤100000, 1 ≤ m ≤ 200000 1≤m≤200000 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 ≤ a i , b i ≤ n 1≤ai,bi≤n 1≤ai,bi≤n, 1 ≤ c i ≤ 1 0 9 1≤ci≤10^9 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.
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
如果说从起点
1
1
1开始,一直向终点
n
n
n走,每次只是挑选边权(颜色)最小的走,可能无法到达,也可能找到的不是字典序最小的(考虑长度)
所以说可以先从终点
n
n
n向起点
1
1
1走,求出最短路(每条路径边权为
1
1
1),那么说到达起点
1
1
1的距离
d
i
s
[
1
]
dis[1]
dis[1]便是路径上经过的颜色的个数
倒着求完最短路径之后,就可以利用已经有的
d
i
s
[
]
dis[]
dis[]来正向寻找答案
细节的地方在于要先倒序求完最短路,然后正向获取答案
#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
**/
以上是关于[UVA 1599] Ideal Path | 细节最短路的主要内容,如果未能解决你的问题,请参考以下文章
每日一题 UVA - 1599 Ideal Path 字典序最短路