poj1860 Currency Exchange

Posted 王宜鸣

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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的主要内容,如果未能解决你的问题,请参考以下文章

POJ-1860 Currency Exchange spfa判负环

POJ-1860 Currency Exchange( Bellman_Ford, 正环 )

POJ——T1860 Currency Exchange

POJ 1860 Currency Exchange

POJ 1860 Currency Exchange (最短路)

poj1860 Currency Exchange(spfa判断正环)