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
Case 1: Yes Case 2: No
#include <map> #include <set> #include <list> #include <queue> #include <stack> #include <vector> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <algorithm> using namespace std; double dp[33][33]; char str[110], str2[110]; int main() { int n, cnt, m; double rate; int icase = 1; map <string, int> node; while (~scanf("%d", &n), n) { node.clear(); cnt = 0; for (int i = 1; i <= n; ++i) { scanf("%s", str); if (node[str] == 0) { node[str] = ++cnt; } } memset (dp, 0, sizeof(dp)); for (int i = 1; i <= cnt; ++i) { dp[i][i] = 1; } scanf("%d", &m); for (int i = 1; i <= m; ++i) { scanf("%s%lf%s", str, &rate, str2); dp[node[str]][node[str2]] = rate; } for (int k = 1; k <= cnt; ++k) { for (int i = 1; i <= cnt; ++i) { for (int j = 1; j <= cnt; ++j) { dp[i][j] = max(dp[i][j], dp[i][k] * dp[k][j]); } } } bool flag = false; for (int i = 1; i <= cnt; ++i) { if (dp[i][i] > 1) { flag = true; break; } } if (flag) { printf("Case %d: Yes\n", icase++); } else { printf("Case %d: No\n", icase++); } } return 0; }
原文地址:http://blog.csdn.net/guard_mine/article/details/41652381