[UVA 1599] Ideal Path | 细节最短路

简介: DescriptionNew 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 .

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 (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 ≤ a i , b i ≤ n , 1 ≤ c i ≤ 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 开始,一直向终点n 走,每次只是挑选边权(颜色)最小的走,可能无法到达,也可能找到的不是字典序最小的(考虑长度)


所以说可以先从终点n 向起点1 走,求出最短路(每条路径边权为1 ),那么说到达起点1 的距离 dis[1]便是路径上经过的颜色的个数

倒着求完最短路径之后,就可以利用已经有的 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
**/


目录
相关文章
AtCoder Beginner Contest 223 D - Restricted Permutation(建图 思维 构造 拓扑排序)
AtCoder Beginner Contest 223 D - Restricted Permutation(建图 思维 构造 拓扑排序)
134 0
luoguP2205 [USACO13JAN]Painting the Fence S(差分 扫描线思想)
luoguP2205 [USACO13JAN]Painting the Fence S(差分 扫描线思想)
65 0
AtCoder Beginner Contest 216 D - Pair of Balls (思维建图 拓扑排序判断有向图是否有环)
AtCoder Beginner Contest 216 D - Pair of Balls (思维建图 拓扑排序判断有向图是否有环)
127 0
AtCoder Beginner Contest 221 E - LEQ(组合数学 树状数组)
AtCoder Beginner Contest 221 E - LEQ(组合数学 树状数组)
158 0
2019CCPC秦皇岛HDU - 6736 F - Forest Program(dfs找环 组合数学)
2019CCPC秦皇岛HDU - 6736 F - Forest Program(dfs找环 组合数学)
101 0
|
算法 Go
HDU-1548,A strange lift(Dijkstra)
HDU-1548,A strange lift(Dijkstra)
HDOJ/HDU 1372 Knight Moves(经典BFS)
HDOJ/HDU 1372 Knight Moves(经典BFS)
138 0
UVa 11292 - Dragon of Loowater(排序贪心)
UVa 11292 - Dragon of Loowater(排序贪心)
101 0
|
算法 测试技术
lecture 2.1 简单算法
(一)while循环 1. Convert the following into code that uses a while loop.
1130 0

热门文章

最新文章