您的位置:首页 > 其它

POJ 2240 Arbitrage( floyd)

2015-02-04 18:20 351 查看
大概意思就是先给出n中货币的名字,然后给出m种汇率,A C B,代表1单位的A可以换C单位的B.问是否存在一种方法可以使得某种方法,使自身换到是自身汇率能大于1....字符串输入有点...麻烦?也不算难...像是POJ 1860的弱化版?floyd可以直接跑出来的样子...

#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
char name[50],s[50][50];;
int n;
double map[50][50];
int find()
{
for(int i=1; i<=n; i++)
if(strcmp(s[i],name)==0)
return i;
}
int main()
{
int m,cas=1;
while(cin>>n,n)
{
for(int i=1; i<=n; i++)
{
scanf("%s",s[i]);
}
cin>>m;
memset(map,0,sizeof(map));
for(int i=1; i<=n; i++)
{
map[i][i]=1.0;
}
for(int i=0; i<m; i++)
{
int a,b;
double c;
scanf("%s",name);
a=find();
cin>>c;
scanf("%s",name);
b=find();
map[a][b]=c;
}
for(int k=1; k<=n; k++)
{
for(int i=1; i<=n; i++)
{
for(int j=1; j<=n; j++)
map[i][j]=max(map[i][j],map[i][k]*map[k][j]);
}
}
int flag=0;
for(int i=1; i<=n; i++)
{
if(map[i][i]>1.0)
flag=1;
}
if(flag)
printf("Case %d: Yes\n",cas);
else
printf("Case %d: No\n",cas);
cas++;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: