要套现的前提是有一种货币经过几次交换能是其到其本身的汇率大于1。就转换成了求任一货币到
其自身的汇率,不过要通过其他货币才能转换到自身,所以用floyd算法,求出所有货币到其他货币
或者自身的最大汇率。这里的floyd计算时是汇率相乘取最大值,这也是与普通floyd的区别。字符串
的处理是本题设置的一个考点吧,不过还好对于这个已经无压力了。
/*Accepted 192K 47MS C++ 1074B 2012-04-14 12:57:03 */#include<cstdio> #include<cstring> #include<cstdlib> const int MAXN = 1 << 5; double r[MAXN][MAXN]; int n, m; char s[MAXN][MAXN];int findbuf( char *buf) {int i;for( i = 0; i < m; i ++)if( !strcmp( buf, s[i]))return i; }void Read_Gragh() {char buf1[MAXN], buf2[MAXN];double rate;for( int i = 0; i < n; i ++)scanf( "%s", s[i]);memset( r, 0, sizeof r);for( int i = 0; i < n; i ++) r[i][i] = 1;scanf( "%d", &m);for( int i = 0; i < m; i ++){scanf( "%s%lf%s", buf1, &rate, buf2);r[ findbuf( buf1)][ findbuf(buf2)] = rate;} }bool floyd() {for( int k = 0; k < n; k ++)for( int i = 0; i < n; i ++)for( int j = 0; j < n; j ++)if( r[i][j] < r[i][k] * r[k][j])r[i][j] = r[i][k] * r[k][j];for( int i = 0; i < n; i ++)if( r[i][i] > 1) return true;return false; }int main() {int cas = 0;while( scanf( "%d", &n), n){Read_Gragh();printf( "Case %d: ", ++ cas);bool ok = floyd();if( ok) printf( "Yes\n");else printf( "No\n");}return 0; }