【HDOJ1217】【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): 9455    Accepted Submission(s): 4359


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
题目大意:给一堆国家的名字然后给一些国家的货币汇率,然后问能否通过一些转换,使自己获利。
题目分析:可以转化为求最长路问题,也就是求从起点到起点的最长路【只不过每次移动不是加法而是乘法】,看这条路最长能否超过1。也就是说求多起点多终点的最长路,并且由于汇率有大于一的和小于一的,就相当于负权边,所以不能使用DIJ算法,而且是多起点多终点的且数据规模不大,故使用Floyd算法最好。
【Floyd模板:最外层    K   次外层   I 最内层 J  转移方程: qwq[ I ][ J ]  =   max(  qwq[ I ][ J ]   ,  qwq[ I ][ K ] +  qwq[ K ][ J ]   )   也就是最外层的变量作为  "中间变量"  来更新内层】
 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 #include<algorithm>
 5 using namespace std;
 6 int n;
 7 char ss[50][120];
 8 double qwq[50][50];
 9 int find(char dd[])
10 {
11     for(int i = 0 ; i< n ;i++)
12     {
13         if(strcmp(dd,ss[i])==0)return i;
14     }
15 }
16 int main()
17 {
18     int case1=1;
19     while(scanf("%d",&n)&&n)
20     {
21         memset(qwq,0,sizeof(qwq));        
22         for(int i = 0 ; i< n ; i++)
23         {
24             scanf("%s",ss[i]);
25             qwq[i][i]=1;
26         }
27         int m;
28         scanf("%d",&m);
29         while(m--)
30         {
31             char qq[120],ww[120];
32             double asd;
33             scanf("%s%lf%s",qq,&asd,ww);
34             int u=find(qq);
35             int v=find(ww);
36             qwq[u][v]=asd;
37         }
38         for(int i = 0 ; i < n ; i++)
39         {
40             for(int j = 0 ; j < n; j++)
41             {
42                 for(int k = 0 ; k< n ; k++)
43                 {
44                     qwq[j][k]=max(qwq[j][i]*qwq[i][k],qwq[j][k]);
45                 }
46             }
47         }
48         bool flag=false;
49             for(int i = 0 ; i < n; i++)    
50             {
51                 if(qwq[i][i]>1.0)
52                 {
53                     flag=true;
54                     break;
55                 }
56             }
57             printf("Case %d: ",case1++);
58             if(flag){
59                 printf("Yes\n");
60             }
61             else
62             printf("No\n");
63     }
64     
65     return 0;
66 }

 

转载于:https://www.cnblogs.com/MekakuCityActor/p/9040846.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值