poj 2240 Arbitrage(最短路问题)

Description

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

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

Input

The input will contain one or more test 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 n lines each contain the name of one currency. Within a name no spaces will appear. The next line contains one integer m, representing the length of the table to follow. The last m lines each contain the name ci of a source currency, a real number rij which represents the exchange rate from ci to cj and a name cj of the destination currency. Exchanges which do not appear in the table are impossible. 
Test cases are separated from each other by a blank line. Input is terminated by a value of zero (0) for n.

Output

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

Sample Input

3
USDollar
BritishPound
FrenchFranc
3
USDollar 0.5 BritishPound
BritishPound 10.0 FrenchFranc
FrenchFranc 0.21 USDollar

3
USDollar
BritishPound
FrenchFranc
6
USDollar 0.5 BritishPound
USDollar 4.9 FrenchFranc
BritishPound 10.0 FrenchFranc
BritishPound 1.99 USDollar
FrenchFranc 0.09 BritishPound
FrenchFranc 0.19 USDollar

0

Sample Output

Case 1: Yes
Case 2: No

题意:给出一些货币和货币之间的兑换比率,问是否可以使某种货币经过一些列兑换之后,货币值增加。
   举例说就是1美元经过一些兑换之后,超过1美元。可以输出Yes,否则输出No。

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <stdlib.h>
 4 #include <string.h>
 5 #include <algorithm>
 6 using namespace std;
 7 #define INF 9999999
 8 #define MAX 300
 9 int n;
10 float g[MAX][MAX];
11 char name[50][100];
12 int fun(char *str)
13 {
14     int i;
15     for(i=1; i<=n; i++)
16     {
17         if(strcmp(name[i],str)==0)
18             return i;
19     }
20 }
21 void FLOYD()
22 {
23     int i,j,k;
24     double t;
25     for(k=1; k<=n; k++)
26     {
27         for(i=1; i<=n; i++)
28         {
29             for(j=1; j<=n; j++)
30             {
31                 t=g[i][k]*g[k][j];
32                 if(t>g[i][j])
33                     g[i][j]=t;
34                 if(g[i][j]>1&&i==j)
35                 {
36                     printf("Yes
");
37                     return ;
38                 }
39             }
40         }
41     }
42     printf("No
");
43 }
44 int main()
45 {
46     int i,k=0,m;
47     float t;
48     char str1[80],str2[80];
49     while(~scanf("%d",&n),n)
50     {
51         memset(g,0,sizeof(g));
52         for(i=1; i<=n; i++)
53             scanf("%s",&name[i]);
54         scanf("%d",&m);
55         for(i=1; i<=m; i++)
56         {
57             scanf("%s %llf %s",str1,&t,str2);
58             g[fun(str1)][fun(str2)]=t;
59         }
60         printf("Case %d: ",++k);
61         FLOYD();
62     }
63     return 0;
64 }
View Code
原文地址:https://www.cnblogs.com/cxbky/p/4854844.html