题意:判断是否有一种货币通过和其它不同的货币的汇率进行兑换, 最后是否会多出原来的价值.比如: 1美元去兑换, 1*0.5*10.0*0.21 = 1.05.
Flody最短路径.
#include <stdio.h>
#include <string>
#include <map>
using namespace std;
#define MAX 35
double g[MAX][MAX];
map<string, int> currencies;
int n, m;
bool floyd(int s) {
int i, j, k;
for (k = 1; k <= n; k++) {
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++) {
if (g[i][j] < g[i][k]*g[k][j]) {
g[i][j] = g[i][k]*g[k][j];
}
}
}
}
if (g[s][s] > 1.0) {
return true;
}
return false;
}
int main() {
int i, j;
int T = 0;
double rate;
char type[50];
char a[50], b[50];
bool flag;
while (scanf("%d", &n) != EOF && n) {
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++) {
if (j == i) {
g[i][j] = 1.0;
} else {
g[i][j] = 0.0;
}
}
}
for (i = 1; i <= n; i++) {
scanf("%s", type);
currencies[type] = i;
}
scanf("%d", &m);
for (i = 1; i <= m; i++) {
scanf("%s%lf%s", a, &rate, b);
g[currencies[a]][currencies[b]] = rate;
}
flag = false;
for (i = 1; i <= n; i++) {
if (floyd(i)) {
flag = true;
printf("Case %d: Yes\n", ++T);
break ;
}
}
if (!flag) {
printf("Case %d: No\n", ++T);
}
}
return 0;
}