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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
| #include <stdio.h> int main() { int dis[51],bak[51] , u[51], v[51], w[51], first[50], next[51]; int n, m, i, j ,check,flog; int inf = 999999; scanf_s("%d %d", &n, &m); for (i = 1; i <= m; i++) { scanf_s("%d %d %d", &u[i], &v[i], &w[i]); } for (i = 1; i <= n; i++) { dis[i] = inf; } dis[1] = 0; for (i = 1; i <= n - 1; i++) { for (j = 1; j <= m; j++) { bak[i] = dis[i]; } check = 0; for (j = 1; j <= m; j++) {
if (dis[v[j]] > dis[u[j]] + w[j]) { dis[v[j]] = dis[u[j]] + w[j]; } } for (j = 1; j <= m; j++) { if (bak[i] != dis[i]) { check = 1; break; } } if (check = 1) { break; } } flog = 0; for (j = 1; j <= m; j++) { if (dis[v[i]] > dis[u[i]] + w[i]) { flog = 1; break; } } if (flog == 1) { printf("有负权回路。"); } else { for (i = 1; i <= n; i++) { printf("%d ", dis[i]); } }
getchar(); getchar(); return 0; }
|