POJ 2240 Arbitrage

Posted ducklu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ 2240 Arbitrage相关的知识,希望对你有一定的参考价值。

题目大意:跟我上次做的那道感觉一模一样,上次链接:https://www.cnblogs.com/ducklu/p/9231563.html

解题思路:Bellman_Ford判断有无正环(对了,第一次RE了,数组要开大点,我开始只开了40)

 1 #include <iostream>
 2 #include <cstring>
 3 #include <algorithm>
 4 #include <map>
 5 #define INF 1000000
 6 #define MAXVERTEXNUM 1000
 7 using namespace std;
 8 
 9 int Nv, Ne, num;
10 struct Edge
11 {
12     int a, b;
13     double Weight;
14 }E[MAXVERTEXNUM];
15 double dist[MAXVERTEXNUM];
16 
17 bool Bellman_ford()
18 {
19     for (int i = 1; i <= Nv; ++i)
20     {
21         bool flag = false;
22         for (int j = 0; j < num; ++j)
23         {
24             if (dist[E[j].b] < dist[E[j].a] * E[j].Weight)
25             {
26                 dist[E[j].b] = dist[E[j].a] * E[j].Weight;
27                 flag = true;
28             }
29         }
30         if (!flag)
31             break;
32     }
33 
34     for (int j = 0; j < num; ++j)
35         if (dist[E[j].b] < dist[E[j].a] * E[j].Weight)
36             return true;
37 
38     return false;
39 }
40 
41 int main()
42 {
43 //    freopen("test.txt", "r", stdin);
44     int cas = 0;
45     while (true)
46     {
47         num = 0;
48         map<string, int> m;
49         memset(dist, 0, sizeof(dist));
50         dist[1] = 1;
51 
52         cin >> Nv;
53         if (!Nv)
54             return 0;
55         cas++;
56 
57         for (int i = 1; i <= Nv; ++i)
58         {
59             string temp;
60             cin >> temp;
61             m.insert( {temp, i} );
62         }
63 
64         cin >> Ne;
65 
66         for (int i = 1; i <= Ne; ++i)
67         {
68             string s1, s2;
69             double W;
70             cin >> s1 >> W >> s2;
71 
72             E[num].a = m[s1], E[num].b = m[s2];
73             E[num++].Weight = W;
74         }
75 
76         if (Bellman_ford())
77             cout << "Case " << cas << ": Yes" << endl;
78         else
79             cout << "Case " << cas << ": No" << endl;
80 
81 //        getchar();
82     }
83 
84     return 0;
85 }

 

以上是关于POJ 2240 Arbitrage的主要内容,如果未能解决你的问题,请参考以下文章

poj2240Arbitrage

POJ2240:Arbitrage(最长路+正环)

POJ 2240 Arbitrage

poj 2240 Arbitrage 题解

POJ 2240 Arbitrage (spfa判环)

Arbitrage POJ - 2240