HDOJ 1217 Floyd

 1 #include <iostream>
 2 #include <string>
 3 #include <map>
 4 
 5 using namespace std;
 6 
 7 map<string, int> edge;
 8 double matrix[30][30];
 9 
10 bool floyd_Arbitrage(int n){
11     for (int k = 0; k < n; k++)
12         for (int i = 0; i < n; i++)
13             for (int j = 0; j < n; j++)
14                 if (matrix[i][k] * matrix[k][j] - matrix[i][j] > 1e-8)
15                     if (i == j)
16                         return true;
17                     else
18                         matrix[i][j] = matrix[i][k] * matrix[k][j];
19     return false;
20 }
21 
22 int main(){
23     int n;
24     int count = 0;
25     while (cin >> n && n){
26         int m;
27         count += 1;
28         for (int i = 0; i < n; i++)
29             for (int j = 0; j < n; j++){
30                 if (i != j)
31                     matrix[i][j] = 0;
32                 else
33                     matrix[i][j] = 1;
34             }
35         for (int i = 0; i < n; i++){
36             string temp;
37             cin >> temp;
38             edge[temp] = i;
39         }
40         cin >> m;
41         for (int i = 0; i < m; i++){
42             string head, tail;
43             double temp;
44             cin >> head >> temp >> tail;
45             matrix[edge[head]][edge[tail]] = temp;
46         }
47         if (floyd_Arbitrage(n))
48             cout << "Case " << count << ": Yes
";
49         else
50             cout << "Case " << count << ": No
";
51     }
52     return 0;
53 }
原文地址:https://www.cnblogs.com/neopolitan/p/7908402.html