hdu 6386 Age of Moyu (重边判断)

时间:2024-12-23 08:04:01

本来用一个map判重边结果T了, 实际上可以直接给边上打标记即可

int n, m;
struct _ {int to,w,vis;};
vector<_> g[N];
int dis[N];
queue<int> q; void dfs(int x, int c, int d) {
dis[x] = d, q.push(x);
for (auto &e:g[x]) if (!e.vis&&e.w==c) {
e.vis = 1;
if (dis[e.to]>=d) dfs(e.to,c,d);
}
} void work() {
REP(i,1,m) {
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
g[u].pb({v,w,0}),g[v].pb({u,w,0});
}
q.push(1);
dis[1]=0;
while (q.size()) {
int x = q.front(); q.pop();
for (auto &e:g[x]) if (!e.vis) {
e.vis = 1;
if (dis[e.to]>=dis[x]+1) dfs(e.to,e.w,dis[x]+1);
}
}
printf("%d\n", dis[n]==INF?-1:dis[n]);
} int main() {
for (; ~scanf("%d%d", &n, &m); ) {
REP(i,0,n) g[i].clear(),dis[i]=INF;
work();
}
}