题目链接:http://acm.acmcoder.com/showproblem.php?pid=1217
floyd传递闭包。
代码:
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <string>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <map>
using namespace std;
char a[100], b[100];
double c;
int n, m;
int f, q;
double Map[110][110];
struct
{
int num;
char s[110];
}p[35];
void init()
{
memset(Map, 0, sizeof(Map));
for (int i = 0; i < n; i++)
{
Map[i][i] = 1;
scanf("%s", p[i].s);
p[i].num = i;
}
scanf("%d",&m);
while (m--)
{
scanf("%s %lf %s", a, &c, b);
for (int i = 0; i < n; i++)
{
if (strcmp(a, p[i].s) == 0)
f = i;
if (strcmp(b, p[i].s) == 0)
q = i;
}
Map[f][q] = c; //Map[q][f] = 1.0 / c;
}
}
int solve()
{
for (int k = 0; k < n;k++)
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
{
if (Map[i][k] != 0 && Map[k][j] != 0 && Map[i][j] < Map[i][k] * Map[k][j])
{
Map[i][j] = Map[i][k] * Map[k][j];
}
}
for (int i = 0; i < n; i++)
if (Map[i][i] > 1)
return 1;
return 0;
}
int cases = 1;
int main()
{
while (scanf("%d", &n) != EOF && n)
{
init();
if (solve() == 1)
printf("Case %d: Yes\n",cases++);
else
printf("Case %d: No\n", cases++);
}
return 0;
}