题意:求自身到自身的最大转换率。
解析:最简单的方法就是floryd算法变形,求最大路径后,求最大环,看它是否满足条件。
注意:POJ要特别注意类型,此题就是一定用double
#include<map>
#include<iostream>
#include<string>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
const int INF = 0x3f3f3f3f;
int n, m, cases;
double rate;
string s, s1, s2;
double e[31][31];
map<string, int> map1;
int main(){
while(~scanf("%d", &n) && n){
fill(e[0], e[0] + 31*31, INF);
for(int i = 1; i <= n; ++i){
cin >> s;
map1[s] = i;
e[i][i] = 1;
}
cin >> m;
for(int i = 1; i <= m; ++i){
cin >> s1 >> rate >> s2;
e[map1[s1]][map1[s2]] = rate;
}
for(int k = 1; k <= n; ++k)
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= n; ++j)
e[i][j] = max(e[i][j], e[i][k] * e[k][j]);
bool flag = false;
for(int i = 1; i <= n; ++i)
if(e[i][i] > 1){
flag = true;
break;
}
if(flag) cout << "Case " << ++cases << ": Yes" <<endl;
else cout<< "Case " << ++cases << ": No" <<endl;
}
}