Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 18416 | Accepted: 7800 |
Description
Input
Output
Sample Input
3 USDollar BritishPound FrenchFranc 3 USDollar 0.5 BritishPound BritishPound 10.0 FrenchFranc FrenchFranc 0.21 USDollar 3 USDollar BritishPound FrenchFranc 6 USDollar 0.5 BritishPound USDollar 4.9 FrenchFranc BritishPound 10.0 FrenchFranc BritishPound 1.99 USDollar FrenchFranc 0.09 BritishPound FrenchFranc 0.19 USDollar 0
Sample Output
Case 1: Yes Case 2: No
Source
Ulm Local 1996
题目大意:
不同货币间有不同的汇率,问是否存在货币a-->货币b--->.......----> 货币a实现获利
可以转化为加权有向图货币对应节点,货币兑换关系对应边
则货币i经由货币k兑换至货币j的比率为 dist[i][k]*dist[k][j]
\因为本题含回路所以要加条件 i!=j&&k!=j&&k!=i 防止死循环
最后判断 dist[i][j]*dist[j][i]>1 表示可以套汇
ACcode:
#pragma warning(disable:4786)//使命名长度不受限制 #pragma comment(linker, "/STACK:102400000,102400000")//手工开栈 #include <map> #include <set> #include <queue> #include <cmath> #include <stack> #include <cctype> #include <cstdio> #include <cstring> #include <stdlib.h> #include <iostream> #include <algorithm> #define rd(x) scanf("%d",&x) #define rd2(x,y) scanf("%d%d",&x,&y) #define rds(x) scanf("%s",x) #define rdc(x) scanf("%c",&x) #define ll long long int #define maxn 1005 #define mod 1000000007 #define INF 0x3f3f3f3f //int 最大值 #define FOR(i,f_start,f_end) for(int i=f_start;i<=f_end;++i) #define MT(x,i) memset(x,i,sizeof(x)) #define PI acos(-1.0) #define E exp(1) #define esp 1e-6 using namespace std; char money[maxn][55],x[55],y[55]; double dist[maxn][maxn],t; int n,m; int get_id(char *str){ FOR(i,1,n)if(!strcmp(money[i],str))return i; } int main(){ int cnt=1; while(rd(n)&&n){ MT(dist,0); FOR(i,1,n)rds(money[i]); rd(m);FOR(i,1,m){ scanf("%s%lf%s",&x,&t,&y); dist[get_id(x)][get_id(y)]=t; } FOR(k,1,n)FOR(i,1,n)FOR(j,1,n) if(i!=j&&k!=j&&k!=i) dist[i][j]=max(dist[i][k]*dist[k][j],dist[i][j]); bool flag=false; FOR(i,1,n)FOR(j,1,n) if(dist[i][j]*dist[j][i]>1+esp){ flag=true;break; } printf("Case %d: %s",cnt++,flag?"Yes\n":"No\n"); } return 0; } /* 3 USDollar BritishPound FrenchFranc 3 USDollar 0.5 BritishPound BritishPound 10.0 FrenchFranc FrenchFranc 0.21 USDollar 3 USDollar BritishPound FrenchFranc 6 USDollar 0.5 BritishPound USDollar 4.9 FrenchFranc BritishPound 10.0 FrenchFranc BritishPound 1.99 USDollar FrenchFranc 0.09 BritishPound FrenchFranc 0.19 USDollar 0 */