输入的边有两种格式:
1. 边长确定,即xi - xj = b; 可以转化成 xi - xj <= b(xj>= xi +(-b)) 和 xi - xj >=b (即 xj - xi <= -b).
2. 边长不定,xi - xj >= 1; 可以转化成 xj - xi <= -1;
分析:差分约束
View Code
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 6 using namespace std; 7 8 struct Edge 9 { 10 int s,e; 11 int val; 12 }edge[200010]; 13 14 int pe; 15 int N,M; 16 int dis[1010]; 17 18 bool bellman_ford() 19 { 20 bool sign; 21 22 for(int j=0;j<N;j++) 23 { 24 sign=false; 25 for(int i=0;i<pe;i++) 26 if(dis[edge[i].e] > dis[edge[i].s] + edge[i].val) 27 { 28 dis[edge[i].e] = dis[edge[i].s] + edge[i].val; 29 sign=true; 30 } 31 if(!sign) 32 break; 33 } 34 if(sign) 35 return false;//存在负环 36 else 37 return true; 38 } 39 40 int main() 41 { 42 while(scanf("%d%d",&N,&M) != EOF) 43 { 44 memset(dis,0,sizeof(dis));//虚拟出一个x0,xi-x0<=0 45 pe=0; 46 47 char op; 48 int a,b,x; 49 50 for(int i=0;i<M;i++) 51 { 52 getchar(); 53 scanf("%c",&op); 54 if(op=='P') 55 { 56 scanf("%d%d%d",&a,&b,&x); 57 edge[pe].s=a; 58 edge[pe].e=b; 59 edge[pe].val=-x; 60 pe++; 61 edge[pe].s=b; 62 edge[pe].e=a; 63 edge[pe].val=x; 64 pe++; 65 } 66 else 67 { 68 scanf("%d%d",&a,&b); 69 edge[pe].s=a; 70 edge[pe].e=b; 71 edge[pe++].val=-1; 72 } 73 } 74 75 if(bellman_ford()) 76 puts("Reliable"); 77 else 78 puts("Unreliable"); 79 } 80 return 0; 81 }