[Cpp]
// Find a loop, exchange one circle from one currency, and check whether the accumulation of the exchange rate is greater than 1
// Extended the conventional minimum short-circuit bellman, which is calculated to dist (n), that is, to the loop
Maxdist (k) [v] = max {maxdist (k-1) [v], maxdist (k-1) [u] * C (u, v )}
# Include <iostream>
# Include <cstring>
# Include <cstdio>
# Define maxn 50
# Define maxm 1000
Using namespace std;
Struct exchange
{
Int ci, cj;
Double cij;
} Ex [maxm];
Char name [maxn] [20], a [20], B [20];
Double x;
Int n, m;
Double maxdist [maxn];
Int flag;
Int kase = 0;
Int readcase ()
{
Scanf ("% d", & n );
If (n = 0)
Return 0;
For (int I = 0; I <n; I ++)
Scanf ("% s", name [I]);
Scanf ("% d", & m );
For (int I = 0; I <m; I ++)
{
Int q, w;
Scanf ("% s % lf % s", a, & x, B );
For (q = 0; strcmp (a, name [q]); q ++)
;
For (w = 0; strcmp (B, name [w]); w ++)
;
Ex [I]. ci = q;
Ex [I]. cj = w;
Ex [I]. cij = x;
}
Return 1;
}
Void bellman (int v0)
{
Flag = 0;
Memset (maxdist, 0, sizeof (maxdist ));
Maxdist [v0] = 1;
For (int k = 1; k <= n; k ++) // 1 --- n
{
For (int I = 0; I <m; I ++)
{
If (maxdist [ex [I]. ci] * ex [I]. cij> maxdist [ex [I]. cj])
Maxdist [ex [I]. cj] = maxdist [ex [I]. ci] * ex [I]. cij;
}
}
If (maxdist [v0] & gt; 1.0)
Flag = 1;
}
Int main ()
{
While (readcase ())
{
For (int I = 0; I <n; I ++)
{
Bellman (I );
If (flag)
Break;
}
If (flag)
Printf ("Case % d: Yes \ n", ++ kase );
Else
Printf ("Case % d: No \ n", ++ kase );
}
}