int dis[N]; bool vis[N]; priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> Q; inlinevoidDijkstra(int s) { memset(vis, false, sizeof(vis)); memset(dis, 0x3f, sizeof(dis)); dis[s] = 0; Q.push(make_pair(0, s)); while (!Q.empty()) { int u = Q.top().second; Q.pop(); if (vis[u]) continue; vis[u] = true; for (auto nxt : E[u]) { int v = nxt.first, w = nxt.second; if (dis[v] > dis[u] + w) { dis[v] = dis[u] + w; Q.push(make_pair(dis[v], v)); } } } }
int n, m, s;
intmain() { scanf("%d%d%d", &n, &m, &s); for (int i = 1; i <= m; i++) { int u, v, w; scanf("%d%d%d", &u, &v, &w); E[u].push_back(make_pair(v, w)); } Dijkstra(s); for (int i = 1; i <= n; i++) printf("%d%c", dis[i], i == n ? '\n' : ' '); return0; }
int dis[N]; __gnu_pbds::priority_queue<pair<int, int>, greater<pair<int, int>>, __gnu_pbds::pairing_heap_tag> Q; __gnu_pbds::priority_queue<pair<int, int>, greater<pair<int, int>>, __gnu_pbds::pairing_heap_tag>::point_iterator idx[N]; inlinevoidDijkstra(int s) { memset(dis, 0x3f, sizeof(dis)); dis[s] = 0; idx[s] = Q.push(make_pair(0, s)); while (!Q.empty()) { int u = Q.top().second; Q.pop(); for (auto nxt : E[u]) { int v = nxt.first, w = nxt.second; if (dis[v] > dis[u] + w) { dis[v] = dis[u] + w; if (idx[v] != 0) Q.modify(idx[v], make_pair(dis[v], v)); else idx[v] = Q.push(make_pair(dis[v], v)); } } } }
int n, m, s;
intmain() { scanf("%d%d%d", &n, &m, &s); for (int i = 1; i <= m; i++) { int u, v, w; scanf("%d%d%d", &u, &v, &w); E[u].push_back(make_pair(v, w)); } Dijkstra(s); for (int i = 1; i <= n; i++) printf("%d%c", dis[i], i == n ? '\n' : ' '); return0; }
int dis[N]; bool vis[N]; queue<int> Q; voidSPFA() { for (int i = 1; i <= n; i++) dis[i] = 0x7fffffff; dis[s] = 0; vis[s] = true; Q.push(s); while (!Q.empty()) { int u = Q.front(); Q.pop(); vis[u] = false; for (auto nxt : E[u]) { int v = nxt.first, w = nxt.second; if (dis[v] > (longlong)dis[u] + w) { dis[v] = dis[u] + w; if (vis[v]) continue; vis[v] = true; Q.push(v); } } } }
intmain() { scanf("%d%d%d", &n, &m, &s); for (int i = 1; i <= m; i++) { int u, v, w; scanf("%d%d%d", &u, &v, &w); E[u].push_back(make_pair(v, w)); } SPFA(); for (int i = 1; i <= n; i++) printf("%d%c", dis[i], i == n ? '\n' : ' '); return0; }
bool vis[N]; queue<int> Q; int dis[N], cnt[N]; boolSPFA(int s) { memset(vis, false, sizeof(vis)); memset(dis, 0x3f, sizeof(dis)); memset(cnt, 0, sizeof(cnt)); dis[s] = 0; vis[s] = true; cnt[s] = 1; Q.push(s); while (!Q.empty()) { int u = Q.front(); Q.pop(); vis[u] = false; for (auto nxt : E[u]) { int v = nxt.first, w = nxt.second; if (dis[v] > dis[u] + w) { dis[v] = dis[u] + w; if (vis[v]) continue; vis[v] = true; Q.push(v); ++cnt[v]; if (cnt[v] > n + 1) returntrue; } } } returnfalse; }
int T;
intmain() { scanf("%d", &T); while (T--) { for (int u = 1; u <= n; u++) E[u].clear(); scanf("%d%d", &n, &m); for (int i = 1; i <= m; i++) { int u, v, w; scanf("%d%d%d", &u, &v, &w); E[u].push_back(make_pair(v, w)); if (w >= 0) E[v].push_back(make_pair(u, w)); } puts(SPFA(1) ? "YES" : "NO"); } return0; }