poj 1364 King(差分约束)
题目:http://poj.org/problem?id=1364
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 using namespace std; 6 7 const int INF=(1<<28); 8 int n,m,d[110]; 9 struct node 10 { 11 int u,v,w; 12 }edge[110]; 13 14 bool bellman_ford() 15 { 16 int i,j; 17 memset(d,0,sizeof(d)); 18 for(i=0; i<n; i++) 19 { 20 for(j=0; j<m; j++) 21 if(d[edge[j].u]+edge[j].w<d[edge[j].v]) 22 d[edge[j].v]=d[edge[j].u]+edge[j].w; 23 } 24 for(j=0; j<m; j++) 25 if(d[edge[j].u]+edge[j].w<d[edge[j].v]) 26 return false; 27 return true; 28 } 29 30 int main() 31 { 32 int a,b,c,i; 33 char s[10]; 34 while(~scanf("%d",&n)&&n) 35 { 36 scanf("%d",&m); 37 for(i=0; i<m; i++) 38 { 39 scanf("%d%d%s%d",&a,&b,s,&c); 40 if(s[0]=='g') 41 { 42 edge[i].u=a+b; 43 edge[i].v=a-1; 44 edge[i].w=-c-1; 45 } 46 else 47 { 48 edge[i].u=a-1; 49 edge[i].v=a+b; 50 edge[i].w=c-1; 51 } 52 } 53 if(bellman_ford()) 54 printf("lamentable kingdom\n"); 55 else 56 printf("successful conspiracy\n"); 57 } 58 return 0; 59 }
参考博客:http://blog.csdn.net/wangjian8006/article/details/7956848