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

来源:互联网 发布:商铺装修设计软件 编辑:程序博客网 时间:2024/04/27 18:35
/* * 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;}

原创粉丝点击