【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

时间: 2024-08-02 08:43:06

【HDOJ1217】【Floyd求最长路】的相关文章

HDU - 6201 transaction transaction transaction(spfa求最长路)

题意:有n个点,n-1条边的无向图,已知每个点书的售价,以及在边上行走的路费,问任选两个点作为起点和终点,能获得的最大利益是多少. 分析: 1.从某个结点出发,首先需要在该结点a花费price[a]买书,然后再在边上行走,到达目的地后,在目的地b获得price[b]. 2.因此可以建立两个虚拟结点, 虚拟结点1连向n个点,边权分别为-price[i],表示以i为起点,需花费price[i]买书. n个点连向虚拟结点2,边权分别为price[i],表示以i为终点,通过卖书可得price[i]. 3

poj 3592 Instantaneous Transference 强连通图 缩点 再求最长路

1 #include<iostream> 2 #include<stdio.h> 3 #include<string.h> 4 #include<stack> 5 #include<queue> 6 using namespace std; 7 #define maxx 44 8 #define maxx2 44*44 9 #define INF 99999999 10 char s[maxx][maxx]; 11 bool tong[maxx2

zoj-3795-Grouping-tarjan缩点求最长路

用tarjan进行缩点. 然后用dfs求最长路.水体... #include<stdio.h> #include<string.h> #include<algorithm> #include<iostream> #include<vector> #include<map> #include<stack> using namespace std; #define maxn 110000 vector<int>ol

POJ 3592--Instantaneous Transference【SCC缩点新建图 &amp;amp;&amp;amp; SPFA求最长路 &amp;amp;&amp;amp; 经典】

Instantaneous Transference Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 6177   Accepted: 1383 Description It was long ago when we played the game Red Alert. There is a magic function for the game objects which is called instantaneous

zoj3795 Grouping --- 强连通,求最长路

给定图,求把至少把图拆成几个集合能够使集合内的点没有直接或间接关系. 首先由题意可得图中可能含环,而环里面的点肯定是要拆开的. 缩点建图得DAG图,可以想象一下..把图从入度为零的点向下展开,位于同一层的点放在一个集合是没有关系的, 那么题目所求的问题就转化成求图中最长路的问题了. 这个题的实质和 这题 其实是一模一样的.. #include <iostream> #include <cstring> #include <string> #include <cst

POJ 3592--Instantaneous Transference【SCC缩点新建图 &amp;&amp; SPFA求最长路 &amp;&amp; 经典】

Instantaneous Transference Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 6177   Accepted: 1383 Description It was long ago when we played the game Red Alert. There is a magic function for the game objects which is called instantaneous

HDU 2196 Computer(树形DP求最长路)

Problem Description A school bought the first computer some time ago(so this computer's id is 1). During the recent years the school bought N-1 new computers. Each new computer was connected to one of settled earlier. Managers of school are anxious a

UVA 10029 Edit Step Ladders ——(DAG求最长路)

题意:升序的给出一本若干个单词,每个单词都可删除一个字母,添加一个字母或者改变一个字母,如果任意一个操作以后能变成另外一个字典中的单词,那么就连一条有向边,求最长的长度. 分析:DAG的最长路和最短路在算法竞赛入门里边原原本本有的,结果我现在忘记了,,真是太弱了..方法就是,用map对应键值(以建图),然后删除操作和修改操作可以看做同一个操作,之后每个操作都是在相应的位置添加一个 '*' 就可以了.想说的有两点,一个是为什么删除和修改可以看做一个操作,其实删除这个操作根本就是多余的,因为一个单词

zoj 5303 Grouping 缩点求最长路

点击打开链接 Grouping Time Limit: 2 Seconds      Memory Limit: 65536 KB Suppose there are N people in ZJU, whose ages are unknown. We have some messages about them. The i-th message shows that the age of person si is not smaller than the age of person ti.