https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=377
题意:硬币兑换,能不能兑换一圈使得手中的钱变多。
floyd传递闭包。
代码:
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <queue>
#include <math.h>
#include <map>
#include <string>
using namespace std;
int n, m;
map<string, int> MP;
double a[50][50];
int main()
{
string s,y;
double tmp;
int cases = 1;
while (cin >> n && n)
{
memset(a, 0, sizeof(a));
MP.clear();
for (int i = 1; i <= n; i++)
{
cin >> s;
MP[s] = i;
}
for (int i = 1; i <= n; i++)
a[i][i] = 1.0;
cin >> m;
for (int i = 1; i <= m; i++)
{
cin >> s >> tmp >> y;
a[MP[s]][MP[y]] = tmp;
}
for (int k = 1; k <= n; k++)
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
{
if (a[i][j] < a[i][k] * a[k][j])
a[i][j] = a[i][k] * a[k][j];
}
int ok = 0;
for (int i = 1; i <= n; i++)
if (a[i][i] > 1.0)
{
ok = 1;
break;
}
printf("Case %d: ",cases++);
if (ok) printf("Yes\n");
else printf("No\n");
}
return 0;
}