E - Currency Exchange

 1 //bellman_ford判断负环
 2 #include <vector>
 3 #include <iostream>
 4 #include <cstring>
 5 #include <algorithm>
 6 using namespace std;
 7 
 8 const int maxn = 200 + 5;
 9 
10 int n,m;
11 double v;
12 struct node{
13     int u, v;
14     double r,c;
15 }p[maxn];
16 
17 double dis[maxn];
18 int cnt;
19 
20 bool bellman_ford(int x){
21     memset(dis, 0, sizeof(dis));
22     dis[x] = v;
23     for(int i = 1;i < n;i++){
24         bool flag = true;
25         for(int j = 0;j < cnt;j++){
26             int aa = p[j].u;
27             int bb = p[j].v;
28             double rr = p[j].r;
29             double cc = p[j].c;
30             if(dis[bb] < (dis[aa] - cc)*rr){
31                 dis[bb] = (dis[aa] - cc)*rr;
32                 flag = false;
33             }
34         }
35         if(flag)
36             break;
37     }
38 
39     //判断出现负环
40     for(int i = 0; i< cnt;i++)
41         if( dis[p[i].v] < (dis[p[i].u]-p[i].c)*p[i].r)
42             return true;
43     return false;
44 }
45 
46 int main(){
47     int s;
48     cin >> n >> m >> s >>v;
49     cnt = 0;
50     while(m--){
51         int a, b;
52         double c1,r1,c2,r2;
53         cin >> a >> b >> c1 >> r1 >> c2 >> r2;
54         p[cnt].u = a, p[cnt].v = b, p[cnt].r = c1, p[cnt++].c = r1;
55         p[cnt].u = b, p[cnt].v = a, p[cnt].r = c2, p[cnt++].c = r2;
56     }
57     if(bellman_ford(s))
58         cout << "YES" << endl;
59     else
60         cout << "NO" << endl;
61 
62     return 0;
63 }

 

posted @ 2018-05-10 20:51  ouyang_wsgwz  阅读(292)  评论(0编辑  收藏  举报