structEdge { int to, w; }; vector<Edge> g[N]; int dist[N], cnt[N]; bool st[N]; // 记录是否在队列中
// 对于边权为正的图,任意两个结点之间的最短路,任意一条的结点数不会超过 $n$,边数不会超过 $n-1$。 boolspfa(int n){ memset(dist, 0x3f, sizeof(dist)); memset(st, 0, sizeof(st)); memset(cnt, 0, sizeof(cnt)); dist[1] = 0; queue<int> q; q.push(1); while (q.size()) { int u = q.front(); q.pop(); st[u] = false; for (auto e : g[u]) { int v = e.to, w = e.w; if (dist[v] > dist[u] + w) { dist[v] = dist[u] + w; cnt[v] = cnt[u] + 1; // 根据负环性质,判定为存在负环,直接返回 if (cnt[v] >= n) returntrue; if (!st[v]) { q.push(v); st[v] = true; } } } }
returnfalse; }
voidsolve(){ int n, m; cin >> n >> m; for (int i = 1; i <= n; i ++ ) g[i].clear(); while (m --) { int a, b, c; cin >> a >> b >> c; if (c >= 0) { g[a].push_back({b, c}); g[b].push_back({a, c}); } else { g[a].push_back({b, c}); } }
bool t = spfa(n); if (t) cout << "YES\n"; else cout << "NO\n"; }
intmain(){ std::ios::sync_with_stdio(false); cin.tie(0);cout.tie(0); int T; cin >> T; while (T --) { solve(); } return0; }
voidfloyd(){ for (int k = 1; k <= n; k ++ ) for (int i = 1; i <= n; i ++ ) for (int j = 1; j <= n; j ++ ) d[i][j] = min(d[i][j], d[i][k] + d[k][j]); }
intmain(){ cin >> n >> m >> k; // 初始化,当i和j相等表示自己走到自己,路径长度为0,其余均视作无穷大 for (int i = 1; i <= n; i ++ ) for (int j = 1; j <= n; j ++ ) if (i == j) d[i][j] = 0; else d[i][j] = INF;
for (int i = 1; i <= m; i ++ ) { int x, y, z; scanf("%d%d%d", &x, &y, &z); d[x][y] = min(d[x][y], z); // 由于求最短路,只需要保留最短边即可 }
floyd();
// 处理输出结果 while (k -- ) { int x, y; scanf("%d%d", &x, &y); if (d[x][y] >= INF / 2) printf("impossible\n"); elseprintf("%d\n", d[x][y]); }