poj 2240--Arbitrage
2013-03-16 20:28
381 查看
Description
Arbitrageistheuseofdiscrepanciesincurrencyexchangeratestotransformoneunitofacurrencyintomorethanoneunitofthesamecurrency.Forexample,supposethat1USDollarbuys0.5Britishpound,1Britishpoundbuys10.0Frenchfrancs,and1Frenchfrancbuys0.21USdollar.Then,byconvertingcurrencies,aclevertradercanstartwith1USdollarandbuy0.5*10.0*0.21=1.05USdollars,makingaprofitof5percent.
Yourjobistowriteaprogramthattakesalistofcurrencyexchangeratesasinputandthendetermineswhetherarbitrageispossibleornot.
Input
Theinputwillcontainoneormoretestcases.Omthefirstlineofeachtestcasethereisanintegern(1<=n<=30),representingthenumberofdifferentcurrencies.Thenextnlineseachcontainthenameofonecurrency.Withinanamenospaceswillappear.Thenextlinecontainsoneintegerm,representingthelengthofthetabletofollow.Thelastmlineseachcontainthenameciofasourcecurrency,arealnumberrijwhichrepresentstheexchangeratefromcitocjandanamecjofthedestinationcurrency.Exchangeswhichdonotappearinthetableareimpossible.
Testcasesareseparatedfromeachotherbyablankline.Inputisterminatedbyavalueofzero(0)forn.
Output
Foreachtestcase,printonelinetellingwhetherarbitrageispossibleornotintheformat"Casecase:Yes"respectively"Casecase:No".
SampleInput
SampleOutput
网上的bellman
Arbitrageistheuseofdiscrepanciesincurrencyexchangeratestotransformoneunitofacurrencyintomorethanoneunitofthesamecurrency.Forexample,supposethat1USDollarbuys0.5Britishpound,1Britishpoundbuys10.0Frenchfrancs,and1Frenchfrancbuys0.21USdollar.Then,byconvertingcurrencies,aclevertradercanstartwith1USdollarandbuy0.5*10.0*0.21=1.05USdollars,makingaprofitof5percent.
Yourjobistowriteaprogramthattakesalistofcurrencyexchangeratesasinputandthendetermineswhetherarbitrageispossibleornot.
Input
Theinputwillcontainoneormoretestcases.Omthefirstlineofeachtestcasethereisanintegern(1<=n<=30),representingthenumberofdifferentcurrencies.Thenextnlineseachcontainthenameofonecurrency.Withinanamenospaceswillappear.Thenextlinecontainsoneintegerm,representingthelengthofthetabletofollow.Thelastmlineseachcontainthenameciofasourcecurrency,arealnumberrijwhichrepresentstheexchangeratefromcitocjandanamecjofthedestinationcurrency.Exchangeswhichdonotappearinthetableareimpossible.
Testcasesareseparatedfromeachotherbyablankline.Inputisterminatedbyavalueofzero(0)forn.
Output
Foreachtestcase,printonelinetellingwhetherarbitrageispossibleornotintheformat"Casecase:Yes"respectively"Casecase:No".
SampleInput
3 USDollar BritishPound FrenchFranc 3 USDollar0.5BritishPound BritishPound10.0FrenchFranc FrenchFranc0.21USDollar 3 USDollar BritishPound FrenchFranc 6 USDollar0.5BritishPound USDollar4.9FrenchFranc BritishPound10.0FrenchFranc BritishPound1.99USDollar FrenchFranc0.09BritishPound FrenchFranc0.19USDollar 0
SampleOutput
Case1:Yes Case2:No 这题跟CurrencyExchange有些类似,都是求一定兑换之后总量能不能增加,可以用bellman-ford解决 不过我一开始用了floyd的变形,n的值较小--
网上的bellman
#include<iostream> #include<string> #include<map> #definemaxn40 usingnamespacestd; map<string,int>mp; structEdge { intv,u; doublew; }e[1005]; intsize,n; doubledist[maxn]; voidAddEdge(inta,intb,doublec) { e[size].u=a; e[size].v=b; e[size].w=c; size++; } boolBellman_Ford() { inti,j; for(i=1;i<=n;i++){ boolflag=false; for(j=0;j<size;j++){ //cout<<e[j].u<<''<<e[j].w<<endl; if(dist[e[j].u]*e[j].w>dist[e[j].v]){ dist[e[j].v]=dist[e[j].u]*e[j].w; flag=true; } } if(!flag)break; if(i==n&&flag)returntrue; } returnfalse; } intmain() { intm,ca,i; doublec; stringstr1,str2; ca=0; while(cin>>n&&n){ ++ca; mp.clear(); for(i=1;i<=n;i++){ cin>>str1; mp[str1]=i; } cin>>m; memset(e,0,sizeof(e)); size=0; while(m--){ cin>>str1>>c>>str2; AddEdge(mp[str1],mp[str2],c); } memset(dist,0,sizeof(dist)); dist[1]=1; cout<<"Case"<<ca<<":"; if(Bellman_Ford()) cout<<"Yes"<<endl; else cout<<"No"<<endl; } }
相关文章推荐
- poj 2240 Arbitrage
- POJ2240-Arbitrage-C语言&&NYOJ-188
- POJ 2240 Arbitrage
- POJ 2240_Arbitrage
- POJ 2240 - Arbitrage
- poj 2240 Arbitrage
- POJ - 2240 Arbitrage
- POJ - 2240 Arbitrage 求有无正环
- POJ2240-Arbitrage
- poj 2240 Arbitrage
- POJ 2240 Arbitrage
- poj 2240 Arbitrage(Bellman-Ford||Floyd)(中等)
- POJ-2240-Arbitrage(弗洛伊德|贝尔曼)
- poj_2240 Arbitrage(Bellman_ford + map)
- POJ 2240 Arbitrage(最短路,floyd)
- poj 2240 Arbitrage
- POJ 2240 Arbitrage(最短路 Floyd)
- poj 2240 Arbitrage 【最短路】 【更新:用STL map处理字符串】
- POJ 2240 Arbitrage
- POJ 2240 Arbitrage【Bellman_ford坑】