Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 16456 | Accepted: 5732 |
Description
Input
Output
Sample Input
3 2 1 20.0 1 2 1.00 1.00 1.00 1.00 2 3 1.10 1.00 1.10 1.00
Sample Output
YES
1 // spfa 2 #include<stdio.h> 3 #include<string.h> 4 #include<queue> 5 using namespace std; 6 7 const int INF = 0x3f3f3f3f; 8 queue <int> que; 9 double rate[101][101]; 10 double commission[101][101]; 11 double dis[101]; 12 int n,m,s; 13 double v; 14 int inque[101]; 15 int map[101][101]; 16 17 bool spfa() 18 { 19 while(!que.empty()) 20 que.pop(); 21 memset(inque,0,sizeof(inque)); 22 for(int i = 1; i <= n; i++) 23 dis[i] = 0; 24 dis[s] = v; 25 que.push(s); 26 inque[s] = 1; 27 while(!que.empty()) 28 { 29 int u = que.front(); 30 que.pop(); 31 inque[u] = 0; 32 for(int i = 1; i <= n; i++) 33 { 34 if(map[u][i] != INF && dis[i] < (dis[u]-commission[u][i])*rate[u][i]) 35 { 36 dis[i] = (dis[u]-commission[u][i])*rate[u][i]; 37 if(inque[i] == 0) 38 { 39 inque[i] = 1; 40 que.push(i); 41 } 42 } 43 } 44 if(dis[s] > v) return true;//若松弛后dis[s] > v 说明有正环 45 } 46 return false; 47 } 48 int main() 49 { 50 int a,b; 51 while(~scanf("%d %d %d %lf",&n,&m,&s,&v)) 52 { 53 memset(map,INF,sizeof(map)); 54 for(int i = 1; i <= m; i++) 55 { 56 scanf("%d %d",&a, &b); 57 scanf("%lf %lf %lf %lf",&rate[a][b],&commission[a][b],&rate[b][a],&commission[b][a]); 58 map[a][b] = 0; 59 map[b][a] = 0; 60 } 61 if(spfa()) 62 printf("YES\n"); 63 else printf("NO\n"); 64 } 65 return 0; 66 }
1 //Bellman_ford 2 3 #include<stdio.h> 4 #include<string.h> 5 struct node 6 { 7 int u,v; 8 double rate,comm; 9 }map[210]; 10 int vis[101]; 11 int n,m,s,cnt; 12 double v; 13 double dis[101]; 14 15 bool Bellman_ford() 16 { 17 memset(dis,0,sizeof(dis));//dis[]初始化为0; 18 dis[s] = v; 19 int i,j,flag; 20 for(i = 1;i <= n; i++) 21 { 22 flag = 0; 23 for(j = 0; j < cnt; j++)//松弛任意两点 24 { 25 if(dis[map[j].v] < (dis[map[j].u]-map[j].comm)*map[j].rate) 26 { 27 dis[map[j].v] = (dis[map[j].u]-map[j].comm)*map[j].rate; 28 flag = 1; 29 } 30 } 31 if(flag == 0) 32 break; 33 } 34 if(i >= n+1)//若松弛n次还可以松弛说明存在正环; 35 return true; 36 else return false; 37 } 38 int main() 39 { 40 int a,b; 41 double c,d,e,f; 42 while(~scanf("%d %d %d %lf",&n,&m,&s,&v)) 43 { 44 cnt = 0; 45 while(m--) 46 { 47 scanf("%d %d %lf %lf %lf %lf",&a,&b,&c,&d,&e,&f); 48 map[cnt++] = ((struct node){a,b,c,d}); 49 map[cnt++] = ((struct node){b,a,e,f}); 50 } 51 if(Bellman_ford()) 52 printf("YES\n"); 53 else printf("NO\n"); 54 } 55 return 0; 56 }