题目地址:http://poj.org/problem?id=2240
1 /*
2 最短路:Floyd模板题
3 只要把+改为*就ok了,热闹后判断d[i][i]是否大于1
4 文件输入的ONLINE_JUDGE少写了个_,WA了N遍:)
5 */
6 #include <cstdio>
7 #include <iostream>
8 #include <cstring>
9 #include <algorithm>
10 #include <string>
11 #include <map>
12 #include <cmath>
13 #include <vector>
14 #include <set>
15 #include <queue>
16 using namespace std;
17
18 const int MAXN = 1e6 + 10;
19 const int INF = 0x3f3f3f3f;
20 double d[33][33];
21
22 void Floyd_Warshall(int n)
23 {
24 for (int k=1; k<=n; ++k)
25 {
26 for (int i=1; i<=n; ++i)
27 {
28 for (int j=1; j<=n; ++j)
29 {
30 if (d[i][j] < d[i][k] * d[k][j])
31 {
32 d[i][j] = d[i][k] * d[k][j];
33 }
34 }
35 }
36 }
37
38 for (int i=1; i<=n; ++i)
39 {
40 if (d[i][i] > 1)
41 {
42 puts ("Yes"); return ;
43 }
44 }
45
46 puts ("No"); return ;
47 }
48
49 int main(void) //POJ 2240 Arbitrage
50 {
51 #ifndef ONLINE_JUDGE
52 freopen ("F.in", "r", stdin);
53 #endif
54
55 int n, num; int cas = 0;
56 while (cin >> n && n)
57 {
58 for (int i=1; i<=n; ++i)
59 {
60 for (int j=1; j<=n; ++j)
61 {
62 if (i == j) d[i][j] = 1;
63 else d[i][j] = 0;
64 }
65 }
66
67 map<string, int> m;
68 string s, s1, s2;
69 for (int i=1; i<=n; ++i)
70 {
71 cin >> s;
72 m[s] = i;
73 }
74
75 cin >> num;
76 for (int i=1; i<=num; ++i)
77 {
78 double w;
79 cin >> s1 >> w >> s2;
80 d[m[s1]][m[s2]] = w;
81 }
82
83 cout << "Case " << ++cas << ": ";
84 Floyd_Warshall (n);
85 }
86
87 return 0;
88 }
原文:http://www.cnblogs.com/Running-Time/p/4372777.html