首页 > 代码库 > poj 2240 Arbitrage
poj 2240 Arbitrage
Time Limit: 1000 MS Memory Limit: 65536 KB
64-bit integer IO format: %I64d , %I64u Java class name: Main
[Submit] [Status] [Discuss]
Description
Arbitrage is the use of discrepancies in currency exchange rates to transform one unit of a currency into more than one unit of the same currency. For example, suppose that 1 US Dollar buys 0.5 British pound, 1 British pound buys 10.0 French francs, and 1 French franc buys 0.21 US dollar. Then, by converting currencies, a clever trader can start with 1 US dollar and buy 0.5 * 10.0 * 0.21 = 1.05 US dollars, making a profit of 5 percent.
Your job is to write a program that takes a list of currency exchange rates as input and then determines whether arbitrage is possible or not.
Your job is to write a program that takes a list of currency exchange rates as input and then determines whether arbitrage is possible or not.
Input
The input will contain one or more test cases. Om the first line of each test case there is an integer n (1<=n<=30), representing the number of different currencies. The next n lines each contain the name of one currency. Within a name no spaces will appear. The next line contains one integer m, representing the length of the table to follow. The last m lines each contain the name ci of a source currency, a real number rij which represents the exchange rate from ci to cj and a name cj of the destination currency. Exchanges which do not appear in the table are impossible. Test cases are separated from each other by a blank line. Input is terminated by a value of zero (0) for n.
Output
For each test case, print one line telling whether arbitrage is possible or not in the format "Case case: Yes" respectively "Case case: No".
Sample Input
3USDollarBritishPoundFrenchFranc3USDollar 0.5 BritishPoundBritishPound 10.0 FrenchFrancFrenchFranc 0.21 USDollar3USDollarBritishPoundFrenchFranc6USDollar 0.5 BritishPoundUSDollar 4.9 FrenchFrancBritishPound 10.0 FrenchFrancBritishPound 1.99 USDollarFrenchFranc 0.09 BritishPoundFrenchFranc 0.19 USDollar0
Sample Output
Case 1: YesCase 2: No
题意:对于每一个顶点构成的回路 兑换率是否大于1
#include <iostream> #include <string.h> #include <stdio.h> using namespace std; #define maxx 35 #define maxn 1000 char name[maxx][20],a[20],b[20]; ///链表进行存储 double maxdis[maxn]; ///类似于dis[]数组 不过这次求最大回路 double x; ///兑换率 int n,t; int falg; struct exchange { int ci,cj; double cij; }ex[maxn]; void Bellman(int v0) { falg=0; memset(maxdis,0,sizeof(maxdis)); maxdis[v0]=1; for(int k=1;k<=n;k++) ///要寻找回路 所以从maxdis[0]递推maxdis[1]....maxdis[n] { for(int i=0;i<t;i++) ///每条边加入是否值变大使最大 { if(maxdis[ex[i].ci]*ex[i].cij>maxdis[ex[i].cj]) ///是程的关系了~~~ { maxdis[ex[i].cj]=maxdis[ex[i].ci]*ex[i].cij; ///求最大的 遇到大的就更新 } } } if(maxdis[v0]>1) falg=1; } int main() { int num; int casee=0; while(scanf("%d",&n),n) { for(int i=0;i<n;i++) { cin>>name[i]; //scanf("%s",name[num]); } int i,j,k; scanf("%d",&t); for(i=0;i<t;i++) { cin>>a>>x>>b; //scanf("%s%if%s",a,&x,b); for(j=0;strcmp(a,name[j]);j++); ///,是空循环 没有循环语句的意思 for(k=0;strcmp(b,name[k]);k++); ///将字符串变成了数字 对应关系 ex[i].ci=j; ex[i].cij=x; ex[i].cj=k; ///cout<<i<<‘ ‘<<j<<‘ ‘<<k<<"!!!!!!!"<<endl; 输出一次啊 挺神奇的东东 } for(int i=0;i<n;i++) { Bellman(i); ///遍历每一个点的回路 if(falg) ///如果出现兑换率〉1的现象 标记直接退出就好 break; } if(falg) printf("Case %d: Yes\n",++casee); else printf("Case %d: No\n",++casee); } return 0; }
poj 2240 Arbitrage
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。