标签:
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 17921 | Accepted: 7571 |
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 本题是让求是否能够套汇的.(在货币的兑换过程是否可以获利)
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #define INF 0x3f3f3f3f using namespace std; struct node { double cij; int ci,cj; } edge[10000]; int n,m; char s[40][100]; char s1[100],s2[100]; double Max[10000]; //double 类型 int path[10000]; int flag=0; int Bellman(int u0) { flag=0; memset(Max,0,sizeof(Max)); //初始化为最小 Max[u0]=1; for(int i=1; i<=n; i++) //构成环所以要递推n次 { for(int j=0; j<m; j++) { if(Max[edge[j].ci]*edge[j].cij>Max[edge[j].cj]) Max[edge[j].cj]=Max[edge[j].ci]*edge[j].cij; } } if(Max[u0]>1.0) flag=1; } int main() { int Case=1; while(~scanf("%d",&n)) { if(n==0) break; for(int i=0; i<n; i++) { scanf("%s",s[i]); } scanf("%d",&m); int j,k; for(int i=0; i<m; i++) { double d; scanf("%s %lf %s",s1,&d,s2); for(j=0; strcmp(s1,s[j]); j++) ; for(k=0; strcmp(s2,s[k]); k++) ; edge[i].ci=j; edge[i].cj=k; edge[i].cij=d; } for(int i=0; i<n; i++) { Bellman(i); if(flag) break; } if(flag) printf("Case %d: Yes\n",Case++); else printf("Case %d: No\n",Case++); } }
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/became_a_wolf/article/details/47752863