标签:
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 19063 | Accepted: 8069 |
Description
Input
Output
Sample Input
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
Sample Output
Case 1: Yes Case 2: No
思路:
抽象出来这题就是要求一个图的最大环,仍然用Floyd算法
要注意下G数组的对角线的值都要是1
#include <iostream> #include <cstring> #include <map> using namespace std; map<string,int> money; double G[37][37]; int n,m; void Floyd() { for(int k = 1;k <= n;k++) for(int i = 1;i <= n;i++) for(int j = 1;j <= n;j++) if(G[i][j] < G[i][k]*G[k][j]) G[i][j] = G[i][k]*G[k][j]; } int main() { int countt = 0; while(cin>>n && n) { string tmp; for(int i = 1;i <= n;i++) { cin>>tmp; money.insert(make_pair(tmp,i)); G[i][i] = 1; } cin>>m; string t1,t2; double t; for(int i = 1;i <= m;i++) { cin>>t1>>t>>t2; G[money[t1]][money[t2]] = t; } Floyd(); int flag = 0; for(int i = 1;i <= n;i++) if(G[i][i] > 1) { flag = 1; break; } if(flag) cout<<"Case "<<++countt<<": Yes"<<endl; else cout<<"Case "<<++countt<<": No"<<endl; } return 0; }
标签:
原文地址:http://www.cnblogs.com/immortal-worm/p/5187630.html