警示后人
  • 板块P1811 最短路
  • 楼主wxzzzz
  • 当前回复7
  • 已保存回复7
  • 发布时间2023/8/30 20:31
  • 上次更新2023/11/3 00:17:28
查看原帖
警示后人
749630
wxzzzz楼主2023/8/30 20:31

如果你用了链式前向星,边的起始编号要从 11 开始。

#include <bits/stdc++.h>
using namespace std;
int n, m, k, idx, len, tmp, v[1000005], h[1000005], ne[1000005];
int ep[1000005], dis[1000005], lst[1000005], ans[1000005];
bool vis[1000005];
struct node {
    int x, id, last;
};
queue<node> q;
map<tuple<int, int, int>, bool> flag;
inline void add(int x, int y) {
    v[++idx] = y, ne[idx] = h[x], h[x] = idx;
    //本题错误写法:v[idx] = y, ne[idx] = h[x], h[x] = idx++;
}
inline void bfs() {
    memset(dis, 0x3f, sizeof dis);
    q.push({1, 0, 0}), dis[1] = 0;

    while (!q.empty()) {
        int x = q.front().x, last = q.front().last, id = q.front().id;
        q.pop();

        for (int i = h[x]; ~i; i = ne[i]) {
            int y = v[i];

            if (vis[i] || flag[ {last, x, y}])
                continue;
            dis[y] = dis[x] + 1, vis[i] = 1, lst[i] = id, q.push({y, i, x});

            if (y == n) {
                tmp = i;
                return;
            }
        }
    }
}
int main() {
    memset(h, -1, sizeof h);
    cin >> n >> m >> k;

    while (m--) {
        int x, y;
        cin >> x >> y;
        add(x, y), ep[idx] = y, add(y, x), ep[idx] = x;
    }

    while (k--) {
        int a, b, c;
        cin >> a >> b >> c;
        flag[ {a, b, c}] = 1;
    }

    bfs();

    if (dis[n] > 20000)
        cout << "-1";
    else {
        int now = tmp;

        while (now)
            ans[++len] = ep[now], now = lst[now];

        ans[++len] = 1;
        cout << len - 1 << '\n';

        while (len)
            cout << ans[len--] << ' ';
    }

    return 0;
}
2023/8/30 20:31
加载中...