HDU 1217 Arbitrage(最短路径,Floyd算法)

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

最短路径,弗洛伊德算法,dp,用到了map函数

 1 #include<stdio.h>
 2 #include<string>
 3 #include<iostream>
 4 #include<map>
 5 using namespace std;
 6 const int INF=0.00001;
 7 int n,m,t;
 8 double dp[50][50],c;
 9 string s[50],x1,x2;
10 map<string,int>p;
11
12 int main(){
13     t=1;
14     while(scanf("%d",&n)!=EOF&&n){
15         for(int i=1;i<=n;i++)
16             for(int j=1;j<=n;j++)
17                 dp[i][j]=INF; //初始化
18         for(int i=1;i<=n;i++){
19             cin>>s[i];
20             p[s[i]]=i;
21         }
22     /*    map<string,int>::iterator iter;  //map函数的用法
23         for(iter=p.begin();iter!=p.end();iter++)
24          cout<<iter->first<<"   "<<iter->second<<endl;*/
25         cin>>m;
26         for(int i=1;i<=m;i++){
27             cin>>x1>>c>>x2;
28             dp[p[x1]][p[x2]]=c;
29         }
30         for(int i=1;i<=n;i++)//floyd算法
31             for(int j=1;j<=n;j++)
32                 for(int k=1;k<=n;k++)
33                     if(dp[j][i]*dp[i][k]>dp[j][k])
34                         dp[j][k]=dp[j][i]*dp[i][k];
35         printf("Case %d: ",t++);
36         if(dp[1][1]>1)//因为是自己到自己的转换,所以dp[1][1],如果自己到终点的转换,dp[1][n]
37             printf("Yes\n");
38         else
39              printf("No\n");
40     }
41 } 

原文地址:https://www.cnblogs.com/cake-lover-77/p/10202160.html

时间: 2024-10-08 15:08:43

HDU 1217 Arbitrage(最短路径,Floyd算法)的相关文章

hdu 1217 Arbitrage 两种算法AC代码,Floyd+Bellman-Ford 大水题一枚 注意是有向图~~

Arbitrage Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4998    Accepted Submission(s): 2286 Problem Description Arbitrage is the use of discrepancies in currency exchange rates to transform

hdu 1217 Arbitrage (Floyd + 最大路径)

Arbitrage Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4899    Accepted Submission(s): 2241 Problem Description Arbitrage is the use of discrepancies in currency exchange rates to transform

HDU 1874 求最短路径 Floyd 算法

畅通工程续 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 31192    Accepted Submission(s): 11388 Problem Description 某省自从实行了很多年的畅通工程计划后,终于修建了很多路.不过路多了也不好,每次要从一个城镇到另一个城镇时,都有许多种道路方案可以选择,而某些方案要比另一些方案行

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

[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

最短路径—Floyd算法

Floyd算法 所有顶点对之间的最短路径问题是:对于给定的有向网络G=(V,E),要对G中任意两个顶点v,w(v不等于w),找出v到w的最短路径.当然我们可以n次执行DIJKSTRA算法,用FLOYD则更为直接,两种方法的时间复杂度都是一样的. 1.定义概览 Floyd-Warshall算法(Floyd-Warshall algorithm)是解决任意两点间的最短路径的一种算法,可以正确处理有向图或负权的最短路径问题,同时也被用于计算有向图的传递闭包.Floyd-Warshall算法的时间复杂度

HDU 1217 Arbitrage 【最短路,map+spfa】

Arbitrage Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6985    Accepted Submission(s): 3212 Problem Description Arbitrage is the use of discrepancies in currency exchange rates to transform

hdu 1217 Arbitrage(佛洛依德)

Arbitrage Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6360    Accepted Submission(s): 2939 Problem Description Arbitrage is the use of discrepancies in currency exchange rates to transform o