E

 1 #include <iostream>
 2 #include <math.h>
 3 #include <algorithm>
 4 #include <cstdio>
 5 #include <cstring>
 6 #define MAXN 520
 7 #define MAXM 6000
 8 #define INF 0Xffffff
 9 using namespace std ;
10 struct node
11 {
12     int u, v;
13     double rate,cost;
14 } edge[MAXM];
15 int T, n, m, l,  u, w;
16 double dis[MAXN];
17 int ans;
18 double V;
19 int bellman()
20 {
21     for(int i=0; i<n; i++)
22         dis[i] = 0;
23     dis[l] = V;
24 
25     for(int i=0; i<n; i++)
26     {
27         int flag = 0;
28         for(int j=0; j<ans; j++)
29         {
30             if(dis[edge[j].v]<(dis[edge[j].u]-edge[j].cost)*edge[j].rate)
31             {
32                 dis[edge[j].v]=(dis[edge[j].u]-edge[j].cost)*edge[j].rate;
33                 flag = 1;
34             }
35         }
36         if(!flag)
37             break;
38 
39         if(i==n-1)
40             return 1;
41     }
42     return 0;
43 
44 
45 }
46 void add(int A,int B,double C,double D)
47 {
48     edge[ans].u = A;
49     edge[ans].v = B;
50     edge[ans].rate = C;
51     edge[ans].cost = D;
52     ans++;
53 }
54 int main()
55 {
56     int A,B;
57     double Rab,Cab,Rba,Cba;
58     while(scanf("%d %d %d %lf", &n, &m, &l,&V)!=EOF)
59     {
60         ans = 0;
61 
62         for(int i=0; i<m; i++)
63         {
64             scanf("%d%d%lf%lf%lf%lf", &A,&B,&Rab,&Cab,&Rba,&Cba);
65             add(A, B, Rab,Cab);
66             add(B, A, Rba,Cba);
67         }
68 
69 
70         if(!bellman()) printf("NO
");
71         else printf("YES
");
72     }
73 
74     return 0;
75 }
原文地址:https://www.cnblogs.com/weiyuan/p/5698172.html