# hdu 1217 floyd+map容器

93人阅读 评论(0)

Arbitrage

TimeLimit: 2000/1000 MS (Java/Others)    Memory Limit:65536/32768 K (Java/Others)
Total Submission(s): 6518    Accepted Submission(s): 3015

Problem Description

Arbitrage is the use of discrepanciesin currency exchange rates to transform one unit of a currency into more thanone unit of the same currency. For example, suppose that 1 US Dollar buys 0.5British pound, 1 British pound buys 10.0 French francs, and 1 French franc buys0.21 US dollar. Then, by converting currencies, a clever trader can start with1 US dollar and buy 0.5 * 10.0 * 0.21 = 1.05 US dollars, making a profit of 5percent.

Your job is to write a program that takes a list of currency exchange rates asinput and then determines whether arbitrage is possible or not.

Input

The input file will contain one or moretest cases. Om the first line of each test case there is an integer n(1<=n<=30), representing the number of different currencies. The next nlines each contain the name of one currency. Within a name no spaces willappear. The next line contains one integer m, representing the length of thetable to follow. The last m lines each contain the name ci of a sourcecurrency, a real number rij which represents the exchange rate from ci to cjand a name cj of the destination currency. Exchanges which do not appear in thetable are impossible.
Test cases are separated from each other by a blank line. Input is terminatedby a value of zero (0) for n.

Output

For each test case, print one linetelling whether arbitrage is possible or not in the format "Case case:Yes" respectively "Case case: No".

Sample Input

3

USDollar

BritishPound

FrenchFranc

3

USDollar0.5 BritishPound

BritishPound10.0 FrenchFranc

FrenchFranc0.21 USDollar

3

USDollar

BritishPound

FrenchFranc

6

USDollar0.5 BritishPound

USDollar4.9 FrenchFranc

BritishPound10.0 FrenchFranc

BritishPound1.99 USDollar

FrenchFranc0.09 BritishPound

FrenchFranc0.19 USDollar

0

Sample Output

Case1: Yes

Case2: No

Source

Universityof Ulm Local Contest 1996

Floyd的算法可以求出任意两点间的最短路径，最后比较本国与本国的汇率差，如果大于1，则可以。否则不可以

AC：

#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
#define INF 0x3f3f3f3f
#define MAX 100
using namespace std;
int vis[MAX],dis[MAX];
double map1[MAX][MAX];
char currency[31];
int n,m;
void floyd()
{
for(int k = 0; k < n; k++)
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
if(map1[i][j] < map1[i][k]*map1[k][j])
map1[i][j] = map1[i][k]*map1[k][j];
for(int i = 0; i < n; i++)
if(map1[i][i] > 1.0)
{
printf("Yes\n");
return ;
}
printf("No\n");
return ;

}
int main()
{
int case1 = 1;
while(~scanf("%d",&n))
{
if(n==0)
break;
map<string,int>s;
for(int i = 0; i < n; i++)
{
scanf("%s",currency);
s[currency] = i;
}
scanf("%d",&m);
char p[31],q[31];
double c;
memset(map1,0,sizeof(map1));
for(int i = 0; i < m; i++)
{
scanf("%s%lf%s",p,&c,q);
map1[s[p]][s[q]] = c;
}
printf("Case %d: ",case1++);
floyd();
}
return 0;
}


0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：15577次
• 积分：1211
• 等级：
• 排名：千里之外
• 原创：108篇
• 转载：19篇
• 译文：0篇
• 评论：0条
评论排行