hdu 1217 (Floyd变形)

链接:http://acm.hdu.edu.cn/showproblem.php?pid=1217

Arbitrage

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4430    Accepted Submission(s): 2013

Problem 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 file 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

----------------------------------------------------------------------------

================================================

题意自己看吧,Floyd的变形,求出每个点到自己的最大权值,并且用乘法不是加法

用了map容器便于操作

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <stdlib.h>
 4 #include <iostream>
 5 #include <algorithm>
 6 #include <map>
 7
 8 #define MAXX 35
 9 #define INF 1000000000
10 double d[MAXX][MAXX];
11 using namespace std;
12
13 void Flody(int n)
14 {
15     int i,j,k;
16     for(k=0; k<n; k++)
17         for(i=0; i<n; i++)
18             for(j=0; j<n; j++)
19                 if(d[i][k] * d[k][j] > d[i][j])
20                     d[i][j] = d[i][k] * d[k][j];
21 }
22
23 int main()
24 {
25     int n,i,j,t,tmp=0;
26     while(scanf("%d",&n)!=EOF&&n)
27     {
28         char str[105],str1[105],str2[105];
29         int cas=0;
30         double rate;
31         map<string,int> m;
32         map<string,int>::iterator it;
33         for(i=0; i<n; i++)
34             for(j=0; j<n; j++)
35                 d[i][j] = 0;
36         for(i=0; i<n; i++)
37         {
38             scanf("%s",str);
39             m[str]=cas++;
40         }
41         scanf("%d",&t);
42         for(i=0; i<t; i++)
43         {
44             scanf("%s%lf%s",str1,&rate,str2);
45             d[m[str1]][m[str2]]=rate;
46         }
47         Flody(n);
48         bool flag=false;
49         for(i=0; i<n; i++)
50         {
51             if(d[i][i]>1.0)
52             {
53                 flag=true;
54                 break;
55             }
56         }
57         if(flag)
58         {
59             printf("Case %d: Yes\n",++tmp);
60         }
61         else
62         {
63             printf("Case %d: No\n",++tmp);
64         }
65     }
66     return 0;
67 }

hdu 1217 (Floyd变形)

时间: 2024-10-11 18:06:07

hdu 1217 (Floyd变形)的相关文章

hdu 1596(Floyd 变形)

http://acm.hdu.edu.cn/showproblem.php?pid=1596 find the safest road Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 6911    Accepted Submission(s): 2450 Problem Description XX星球有很多城市,每个城市之间有一条或

hdu 1217 Arbitrage Floyd||SPFA

转载请注明出处:http://acm.hdu.edu.cn/showproblem.php?pid=1217 Problem 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

hdu 1599 floyd 最小环

floyd真的是水很深啊 各种神奇 #include<stdio.h> #include<string.h> #include<math.h> #include<iostream> #include<algorithm> #include<queue> #include<stack> #define mem(a,b) memset(a,b,sizeof(a)) #define ll __int64 #define MAXN

hdu 1217 Arbitrage (spfa算法)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1217 题目大意:通过货币的转换,来判断是否获利,如果获利则输出Yes,否则输出No. 这里介绍一个STL中的map容器去处理数据,map<string,int>V,M; 现在我目前的理解是将字符串转换成数字,然后就是根据spfa的模板找最短路了..哇哈哈( ⊙o⊙ )哇 1 #include <iostream> 2 #include <cstdio> 3 #include

HDU 1847(floyd)

畅通工程续 高仿代码如下 #include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int inf = 0x3f3f3f3f;#define N 205int d[N][N];void floyd(int n){    for(int k = 0;k < n;k++)    for(int i = 0;i <

[ACM] hdu 1217 Arbitrage (bellman_ford最短路,判断是否有正权回路或Floyed)

Arbitrage Problem 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 p

POJ2253——Frogger(Floyd变形)

Frogger DescriptionFreddy Frog is sitting on a stone in the middle of a lake. Suddenly he notices Fiona Frog who is sitting on another stone. He plans to visit her, but since the water is dirty and full of tourists' sunscreen, he wants to avoid swimm

HDU 1217 Arbitrage(Bellman-Ford判断负环+Floyd)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1217 题目大意:问你是否可以通过转换货币从中获利 如下面这组样例: USDollar 0.5 BritishPound BritishPound 10.0 FrenchFranc FrenchFranc 0.21 USDollar 可以通过US->Br->French->US这样转换,把1美元变成1*0.5*10*0.21=1.05美元赚取%5的利润. 解题思路:其实就相当于bellman-

HDU 4034 Graph Floyd变形

戳这里:HDU 4034 //思路:根据题意可得,若 mat[i][j] > mat[i][k] + mat[k][j] 则无解;若 mat[i][j] == mat[i][k] + mat[k][j] 且分别对应 i->j  i->k  k->j 的三条有向边,则可以删掉 i->j 有向边使得原来 i 到 j 的最短路径不发生变化; 那么我们将输入的邻接矩阵当成一个有向完全图,不断的删除多余的边,得到只含最少边的最优解.实现起来要注意删边操作对判无解操作的影响 1 #inc