最近做题总是错,要改几次才过。这是很难得的一个1a了。
bellman算法,和之前做过的1860类似,一个类型。
/*
POJ: 2240 Arbitrage
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
#include <string>
using namespace std;
const int M = 40;
map<string, int> search_map;
double deal_map[M][M];
int n, m;
double dis[M];
bool bellman()
{
for(int i = 2; i <= n; i++) {
dis[i] = .0;
}
dis[1] = 1.0;
for(int i = 0; i < n - 1; i++) {
for(int j = 1; j <= n; j++) {
for(int k = 1; k <= n; k++) {
if(dis[j] < dis[k] * deal_map[k][j])
dis[j] = dis[k] * deal_map[k][j];
}
}
}
for(int j = 1; j <= n; j++) {
for(int k = 1; k <= n; k++) {
if(dis[j] < dis[k] * deal_map[k][j])
return true;
}
}
return false;
}
int main()
{
//freopen("data.in", "rb", stdin);
int ans = 1;
while(scanf("%d", &n) != EOF && n != 0) {
search_map.clear();
for(int i = 1; i <= n; i++) {
string str;
cin >> str;
search_map.insert(make_pair(str, i));
}
for(int i = 0; i <= n; i++) {
for(int j = 0; j <= n; j++)
deal_map[i][j] = .0;
}
scanf("%d", &m);
for(int i = 0; i < m; i++) {
string str1, str2;
double rate;
cin >> str1 >> rate >> str2;
deal_map[search_map[str1]][search_map[str2]] = rate;
}
if(bellman()) {
printf("Case %d: Yes\n", ans++);
}
else {
printf("Case %d: No\n", ans++);
}
}
return 0;
}