最短路 找环路积大于1的 用bellman做的
#include <set>
#include <cmath>
#include <queue>
#include <stack>
#include <string>
#include <cstdio>
#include <cstring>
#include <map>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
const double PI = acos(-1.0);
template <class T> inline T MAX(T a, T b){if (a > b) return a;return b;}
template <class T> inline T MIN(T a, T b){if (a < b) return a;return b;}
const int N = 111;
const int M = 11111;
const LL MOD = 1000000007LL;
const int dir[4][2] = {1, 0, -1, 0, 0, -1, 0, 1};
const int INF = 0x3f3f3f3f;
double mp[33][33];
double dist[33];
int n;
bool bellman(int v0)
{
int u, v, i, j, k;
double w;
for (i = 1; i <= n; ++i)
dist[i] = mp[v0][i];
// memset(dist, 0, sizeof(dist));
// dist[v0] = 1;
for (i = 0; i < n; ++i)
{
for (v = 1; v <= n; ++v)
{
for (j = 1; j <= n; ++j)
{
if (dist[v] < mp[j][v] * dist[j])
dist[v] = mp[j][v] * dist[j];
}
}
if (dist[v0] > 1.0) return true;
}
return false;
}
int main()
{
int cas = 1;
while (cin >> n && n)
{
map <string, int> index;
int a, b, u, v, m, i, j, k, flag = 1;
double w;
for (i = 0; i <= n; ++i)
fill(mp[i], mp[i] + n + 1, 0.0);
string name, next;
for (i = 1; i <= n; ++i)
{
cin >> name;
index[name] = i;
}
cin >> m;
for (i = 0; i < m; ++i)
{
cin >> name >> w >> next;
a = index[name];
b = index[next];
mp[a][b] = w;
}
for (i = 1; i <= n; ++i)
{
if (bellman(i)) {flag = 0; break;}
}
if (!flag) printf("Case %d: Yes\n", cas++);
else printf("Case %d: No\n", cas++);
}
return 0;
}