您的位置:首页 > 其它

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

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;
}
}



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: