-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbellmanFord2.c
More file actions
57 lines (45 loc) · 1.02 KB
/
Copy pathbellmanFord2.c
File metadata and controls
57 lines (45 loc) · 1.02 KB
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
#include <stdio.h>
#define inf 99999999
int main()
{
int dis[10],i,k,n,m,u[10],v[10],w[10];
// 优化增加的变量
int bak[10],check,flag;
scanf("%d %d", &n, &m);
for(i=1; i<=n ; i++)
scanf("%d %d %d", &u[i], &v[i], &w[i]);
for(i=1; i<=n; i++)
dis[i] = inf;
dis[1] = 0;
for(k=1; k<=n-1; k++)
{
check = 0;
for(i=1; i<=m; i++)
{
if(dis[v[i]] > dis[u[i]] + w[i])
{
dis[v[i]] = dis[u[i]] + w[i];
check = 1;
}
}
if(check == 0)
break;
}
//负权回路检测
flag = 0;
for(i=1; i<=m; i++){
if(dis[v[i]] > dis[u[i]] + w[i])
{
//dis[v[i]] = dis[u[i]] + w[i];
flag = 1;
}
}
if(flag == 1)
printf("There is negative weight circuit!\n");
else
{
for(i=1; i<=n; i++)
printf("%d ", dis[i]);
}
return 0;
}