您的位置:首页 > 其它

(Floyed-Warshall13.1.2)POJ 2240 Arbitrage(Floyed-Warshall公式的使用||求所有货币之间的最佳兑换方案)

2013-11-09 12:44 477 查看
/*
* POJ_2240.cpp
*
*  Created on: 2013年11月9日
*      Author: Administrator
*/

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

const int maxn = 40;
const int maxl = 1010;

char str[maxn][maxl];//货币序列
char strA[maxl];//源货币
char strB[maxl];//目标货币

double dist[maxn][maxn];//dist[i][j]: 货币i到货币j的兑换率
/**
* n: 活种类数
* m: 兑换关系数
*/
int n,m;

int find(char* s){//返回货币s在货币序列str中序号
int i;
for(i = 1 ; i <= n ; ++i){
if(strlen(s) == strlen(str[i]) && strcmp(s,str[i]) == 0){
return i;
}
}
}

int main(){
int counter = 1;
while(scanf("%d",&n)!=EOF,n){
memset(dist,0,sizeof(dist));

int i;
for(i = 1 ; i <= n ; ++i){
scanf("%s",&str[i]);
}

scanf("%d",&m);
for(i = 1 ; i <= m ; ++i){
double w;
scanf("%s %lf %s",strA,&w,strB);
dist[find(strA)][find(strB)] = w;
}

int j,k;
for(k = 1 ; k <= n ;++k){//计算所有货币之间的兑换的最佳方案
for(i = 1 ; i <= n ; ++i){
for(j = 1 ; j <= n ; ++j){
if(i != k && k != j && j != i){
if(dist[i][k]*dist[k][j] > dist[i][j]){
dist[i][j] = dist[i][k]*dist[k][j];
}
}
}
}
}

bool flag = false;
for(i = 1 ; i <= n ; ++i){//枚举每一对货币,判断是否存在获利的可能...
for(j = 1 ; j <= n ; ++j){
if(dist[i][j]*dist[j][i] > 1){
flag = true;
}
}
}

printf("Case %d: ",counter++);
printf(flag?"Yes\n":"No\n");
}

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