1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| #include <bits/stdc++.h> using namespace std;
const int N = 5e3 + 10; int n, m, cnt; bool inq[N]; int head[N<<1], dist[N], vis[N];
struct Edge { int to, nxt, val; } e[N<<1];
void addEdge(int u, int v, int w) { e[++cnt].to = v; e[cnt].nxt = head[u]; e[cnt].val = w; head[u] = cnt; return ; }
bool spfa() { queue <int> sp; sp.push(0); vis[0] = 1, inq[0] = true; memset(dist, 0x3f, sizeof dist); dist[0] = 0; while (!sp.empty()) { int cur = sp.front(); sp.pop(); inq[cur] = false; for (int i=head[cur]; ~i; i=e[i].nxt) { int to = e[i].to; if (dist[to] > dist[cur]+e[i].val) { dist[to] = dist[cur] + e[i].val; if (!inq[to]) { inq[to] = true, sp.push(to); ++vis[to]; if (vis[to]>n) return true; } } } } return false; }
int main() { memset(head, -1, sizeof head); cin >> n >> m; for (int i=1; i<=m; ++i) { int u, v, w; cin >> u >> v >> w; addEdge(v, u, w); } for (int i=1; i<=n; ++i) addEdge(0, i, 0); if (spfa()) { puts("NO"); return 0; } for (int i=1; i<=n; ++i) cout << dist[i] << ' '; return 0; }
|