POJ-2240 Arbitrage (SPFA判正环 or Floyd)

描述

传送门:Arbitrage

Arbitrage is the use of discrepancies in currency exchange rates to transform one unit of a currency into more than one unit of the same currency. For example, suppose that 1 US Dollar buys 0.5 British pound, 1 British pound buys 10.0 French francs, and 1 French franc buys 0.21 US dollar. Then, by converting currencies, a clever trader can start with 1 US dollar and buy 0.5 10.0 0.21 = 1.05 US dollars, making a profit of 5 percent.

Your job is to write a program that takes a list of currency exchange rates as input and then determines whether arbitrage is possible or not.

输入描述

The input will contain one or more test cases. Om the first line of each test case there is an integer n (1<=n<=30), representing the number of different currencies. The next n lines each contain the name of one currency. Within a name no spaces will appear. The next line contains one integer m, representing the length of the table to follow. The last m lines each contain the name ci of a source currency, a real number rij which represents the exchange rate from ci to cj and a name cj of the destination currency. Exchanges which do not appear in the table are impossible.
Test cases are separated from each other by a blank line. Input is terminated by a value of zero (0) for n.

输出描述

For each test case, print one line telling whether arbitrage is possible or not in the format “Case case: Yes” respectively “Case case: No”.

示例

输入

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
3
USDollar
BritishPound
FrenchFranc
3
USDollar 0.5 BritishPound
BritishPound 10.0 FrenchFranc
FrenchFranc 0.21 USDollar

3
USDollar
BritishPound
FrenchFranc
6
USDollar 0.5 BritishPound
USDollar 4.9 FrenchFranc
BritishPound 10.0 FrenchFranc
BritishPound 1.99 USDollar
FrenchFranc 0.09 BritishPound
FrenchFranc 0.19 USDollar

0

输出

1
2
Case 1: Yes
Case 2: No

题解

题目大意

判断是否存在使得汇率增多的环,任意一个点的汇率增多都可以。

思路

跟POJ-1860差不多,懒得写了。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <stack>
#include <cmath>
#include <deque>
#include <queue>
#include <list>
#include <set>
#include <map>
#define debug() printf("WTF!\n");
#define mem(a, b) memset(a, b, sizeof(a))
typedef long long ll;
const double EPS = 1e-9;
const double PI = acos(-1);
const int INF = 0x3f3f3f3f;
const int MAXN = 1e3 +5;
using namespace std;
int n, m;
double v;
double dis[MAXN];
int vis[MAXN], cnt[MAXN];
map<string, int> mp;
struct node{
int to;
double cost;
node(){}
node(int _to, double _cost) : to(_to), cost(_cost) {}
};
vector<node> E[MAXN];

bool SPFA(){
mem(vis, 0);
mem(dis, 0);
mem(cnt, 0);
queue<int> que;
dis[1] = 1; vis[1] = 1; cnt[1] = 1;
que.push(1);
while(!que.empty()){
int now = que.front();
que.pop();
vis[now] = 0;
for(int i = 0; i < E[now].size(); i++){
int v = E[now][i].to;
if(dis[v] < dis[now]*E[now][i].cost){
dis[v] = dis[now]*E[now][i].cost;
if(!vis[v]){
que.push(v);
vis[v] = 1;
cnt[v]++;
if(cnt[v] > n-1) return true;
}
}
}
}
return false;
}

int main(){
int cnt = 1;
while(cin >> n && n){
mp.clear();
for(int i = 0; i < MAXN; i++) E[i].clear();
for(int i = 1; i <= n; i++){
string s;
cin >> s;
mp[s] = i;
}
cin >> m;
for(int i = 0; i < m; i++){
string a, b;
double c;
cin >> a >> c >> b;
E[mp[a]].push_back(node(mp[b], c));
}
cout << "Case " << cnt ++ << ": ";
if(SPFA()) cout << "Yes" << endl;
else cout << "No" << endl;
}
}