如果你用了链式前向星,边的起始编号要从 1 开始。
#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;
}