POJ 2240 Arbitrage(Floyd)

题目链接:http://poj.org/problem?id=2240

       题意是给了n种货币,m种汇率,然后问能不能经过一系列的转换后使钱增值。

       我们可以用Floyd算法去更新每两种货币间能获得的最大价值,就是更新max(A->C , A->B * B -> C),最后判断一下有没有存在从i货币到i货币的汇率大于1的,有的话就说明是可以经过一系列转换增值的,感觉挺好的一道题。不要有什么大胆的想法...


AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
#define maxn 35
using namespace std;
int n,m;
double pre[maxn][maxn];
string str,ch1,ch2;

void Floyd(){
	for(int k=0;k<n;k++){
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++){
				pre[i][j] = max(pre[i][j], pre[i][k] * pre[k][j]);
			}
		}
	}
}

int main()
{
	int Case = 1;
	while(~scanf("%d",&n)){
		if(n == 0)break;
		map<string,int> ma;
		for(int i=0;i<n;i++){
			cin>>str;
			ma[str] = i;
		}
		scanf("%d",&m);
		memset(pre,0,sizeof(pre));
		for(int i=0;i<m;i++){
			double ans ;
			cin>>ch1>>ans>>ch2;
			pre[ma[ch1]][ma[ch2]] = ans;
		}
		Floyd();
		int flag = 0;
		for(int i=0;i<n;i++){
			if(pre[i][i] > 1){
				printf("Case %d: Yes\n",Case++);
				flag = 1;
				break;
			}
		}
		if(!flag)
		printf("Case %d: No\n",Case++);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/Charles_Zaqdt/article/details/81214704