首页 > 代码库 > poj1860 Currency Exchange

poj1860 Currency Exchange

思路:

Bellman-Ford判断是否含有经过源点的正权回路。

实现:

 1 #include <iostream>
 2 #include <cstdio>
 3 using namespace std;
 4 
 5 const int MAXN = 105, MAXM = 105;
 6 
 7 int n, m, s;
 8 double d, dist[MAXN];
 9 struct edge
10 {
11     int from, to;
12     double c, r;
13 };
14 edge es[2 * MAXM];
15 
16 bool check(int s, double d)
17 {
18     for (int i = 1; i <= n; i++) dist[i] = 0;
19     dist[s] = d;
20     bool update = true;
21     while (update)
22     {
23         if (dist[s] > d) return true;
24         update = false;
25         for (int i = 1; i <= 2 * m; i++)
26         {
27             edge & e = es[i];
28             if (dist[e.from] > 0 && dist[e.to] < (dist[e.from] - e.c) * e.r)
29             {
30                 dist[e.to] = (dist[e.from] - e.c) * e.r;
31                 update = true;
32             }
33         }
34     }
35     return dist[s] > d;
36 }
37 
38 int main()
39 {
40     cin >> n >> m >> s >> d;
41     for (int i = 1; i < 2 * m; i += 2)
42     {
43         cin >> es[i].from >> es[i].to >> es[i].r >> es[i].c;
44         cin >> es[i + 1].r >> es[i + 1].c;
45         es[i + 1].from = es[i].to;
46         es[i + 1].to = es[i].from;
47     }
48     if (!check(s, d)) puts("NO");
49     else puts("YES");
50 }

 

poj1860 Currency Exchange