http://poj.org/problem?id=1860
模板提
1 #include <iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<stdlib.h> 6 #include<vector> 7 #include<queue> 8 #include<cmath> 9 using namespace std; 10 #define INF 0xfffffff 11 double dis[110],v; 12 int n,m; 13 struct node 14 { 15 int r,c; 16 double r1,c1,r2,c2; 17 }p[110]; 18 int bell_ford(int s) 19 { 20 int i,j; 21 memset(dis,0,sizeof(dis)); 22 dis[s] = v; 23 for(i = 1; i <= n ; i++) 24 { 25 int flag = 0; 26 for(j = 1; j <= m ; j++) 27 { 28 if((dis[p[j].r]-p[j].c1)*p[j].r1>dis[p[j].c]) 29 dis[p[j].c] = (dis[p[j].r]-p[j].c1)*p[j].r1; 30 if((dis[p[j].c]-p[j].c2)*p[j].r2>dis[p[j].r]) 31 dis[p[j].r] = (dis[p[j].c]-p[j].c2)*p[j].r2; 32 flag = 1; 33 34 } 35 if(!flag) break; 36 } 37 for(j = 1; j <= m ; j++) 38 { 39 if((dis[p[j].r]-p[j].c1)*p[j].r1>dis[p[j].c]) 40 return 1; 41 if((dis[p[j].c]-p[j].c2)*p[j].r2>dis[p[j].r]) 42 return 1; 43 } 44 return 0; 45 } 46 int main() 47 { 48 int i,s; 49 while(scanf("%d%d%d%lf",&n,&m,&s,&v)!=EOF) 50 { 51 for(i = 1; i <= m ; i++) 52 { 53 scanf("%d%d%lf%lf%lf%lf",&p[i].r,&p[i].c,&p[i].r1,&p[i].c1,&p[i].r2,&p[i].c2); 54 } 55 if(bell_ford(s)) 56 { 57 puts("YES"); 58 } 59 else 60 puts("NO"); 61 } 62 return 0; 63 }