POJ 3967 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 (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 first line of the input file contains integers n and m —the number of rooms and passages, respectively (2 <= n <= 100 000, 1 <= m <= 200 000). 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

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 1
1 3 2
3 4 3
2 3 1
2 4 4
3 1 1
Sample Output

2
1 3

题意:

一张图,路径有颜色。求最短路,并使最短路颜色字典序最小。

思路:

先跑一遍bfs分层,然后对于每一层,找出颜色最小的连向下一层的边,再把这些边连接的点入队。
思路挺简单的(据说可以一遍bfs扫完,没去想),但是实现有点烦,细节比较多。

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int INF = 1e9+10;
const int N = 1e5+10;
const int M = 4e5+10;
int n, m;
struct EDGE{
    int nxt, v, w;
}edge[M];
int point[N], e, dis[N];
int q[N], h, t, ans[N];
bool vis[N];
int a[N], num;

void add_edge(int u, int v, int w)
{
    edge[++e] = (EDGE){point[u], v, w};
    point[u] = e;
}

void Bfs1()
{
    memset(vis, 0, sizeof(vis));
    q[h = t = 1] = n; dis[n] = 0; vis[n] = 1;
    while (h <= t){
        int u = q[h++];
        for (int i = point[u]; i != -1; i = edge[i].nxt){
            int v = edge[i].v;
            if (!vis[v]){
                vis[v] = 1;
                dis[v] = dis[u]+1;
                q[++t] = v;
            }
        }
    }
}

void Bfs2()
{
    memset(ans, 0x7f, sizeof(ans));
    memset(vis, 0, sizeof(vis));
    q[h = t = 1] = 1;
    while (h <= t){
        int d = dis[q[h]], minw = INF; num = 0;
        while (h <= t){
            int u = q[h++];
            for (int i = point[u]; i != -1; i = edge[i].nxt){
                int v = edge[i].v;
                int w = edge[i].w;
                if (dis[v] == d-1){
                    //如果下面的vis放到这里来判断,就会WA
                    //因为连向这个点的边可能有很多条,要被多次更新,不能判掉
                    //vis放在下面是为了防止重复入队
                    if (w < minw){
                        minw = w;
                        a[num = 1] = v;
                    }
                    else if (w == minw)
                        a[++num] = v;
                }
            }
        }
        ans[d] = minw;
        h = t = 1;
        for (int i = 1; i <= num; i++){
            if (vis[a[i]]) continue;
            vis[a[i]] = 1;
            q[++t] = a[i];
        }
    }
}

int main()
{
    scanf("%d%d", &n, &m);
    memset(point, -1, sizeof(point)); e = -1;
    for (int i = 1; i <= m; i++){
        int x, y, z;
        scanf("%d%d%d", &x, &y, &z);
        add_edge(x, y, z);
        add_edge(y, x, z);
    }
    Bfs1();
    Bfs2();
    printf("%d\n", dis[1]);
    for (int i = dis[1]; i >= 1; i--){
        printf("%d", ans[i]);
        if (i != 1) putchar(' ');
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/xyyxyyx/article/details/82154245