首页 > 其他分享 >hdu:Arbitrage(最短路变形)

hdu:Arbitrage(最短路变形)

时间:2023-05-15 19:57:40浏览次数:40  
标签:10.0 hdu ma FrenchFranc int 短路 BritishPound Arbitrage USDollar

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

图论-最短路的变形

将本题变为求最长路,若从i点到i点的路大于1,则可以盈利

点击查看代码
#include<bits/stdc++.h>
using namespace std;
unordered_map<string,int> ma;
const double eps=1e-2;
double dp[50][50];
int main()
{
    int n;
    int cnt=0;
    while(cin>>n,n)
    {
    for(int i=1;i<=n;++i)
    {
        string s;
        cin>>s;
        ma[s]=i;
    }
    for(int i=1;i<=n;++i) dp[i][i]=1;
    int m;
    cin>>m;
    for(int i=0;i<m;++i) 
    {
        double rate;
        string s1,s2;
        cin>>s1>>rate>>s2;
        if(ma.count(s1)&&ma.count(s2))
        {
            dp[ma[s1]][ma[s2]]=rate;
            //dp[ma[s2]][ma[s1]]=1.0/rate;
        }
    }
    for(int k=1;k<=n;++k)
     for(int i=1;i<=n;++i)
      for(int j=1;j<=n;++j)
      {
          if(dp[i][k]&&dp[k][j]) dp[i][j]=max(dp[i][j],dp[i][k]*dp[k][j]);
      }
    cout<<"Case "<<++cnt<<": "; 
    bool flag=0;
    for(int i=1;i<=n;++i)
    if(dp[i][i]-1.0>eps) flag=1;
    if(flag) puts("Yes");
    else puts("No");
    } 
}

标签:10.0,hdu,ma,FrenchFranc,int,短路,BritishPound,Arbitrage,USDollar
From: https://www.cnblogs.com/ruoye123456/p/17402881.html

相关文章

  • hdu:六度分离(最短路)
    ProblemDescription1967年,美国著名的社会学家斯坦利·米尔格兰姆提出了一个名为“小世界现象(smallworldphenomenon)”的著名假说,大意是说,任何2个素不相识的人中间最多只隔着6个人,即只用6个人就可以将他们联系在一起,因此他的理论也被称为“六度分离”理论(sixdegreesofsepa......
  • hdu:求和(逆元)
    ProblemDescriptionApex实验室里培养了很多种类的细菌。细菌的繁殖遵循如下规则:第k种细菌在第t个单位时间内新增的数量为k^t。例如,k=2,t=4时,第种细菌的总数为2+4+8+16。现在,实验室里一共有n种细菌,分别为1,2,3,…,n。在第1单位时间结束后,第k种细菌的个数为k个。求第m个单位时......
  • ACM板子(1)(缺最短路、计算几何、数学、高级数据结构)
    ACM板子(1)(缺最短路、计算几何、数学、高级数据结构)快排、归并voidquicksort(int*num,intl,intr){if(r<=l)return;intx=l-1,y=r+1,z=num[l+r>>1];while(x<y){dox++;while(num[x]<z);doy--;while(num[y]>z);if(x<y)s......
  • hdu:XOR(线性基)
    XOR数学-线性基设原集为S,线性基为P,若|S|>|P|,则异或会出现0。若|P|==n,则会产生2^n个不同数(包括0)而线性基不包含0元素,故若|S|中元素可以异或出0,k需要自减点击查看代码#include<bits/stdc++.h>usingnamespacestd;typedeflonglongll;constintN=1e4+10;lla[N......
  • (hdu step 3.1.7)Children’s Queue(求n个人站在一起有m个人必须是连在一起的方案数)
    题目:Children’sQueueTimeLimit:2000/1000MS(Java/Others)MemoryLimit:65536/32768K(Java/Others)TotalSubmission(s):853AcceptedSubmission(s):479 ProblemDescriptionTherearemanystudentsinPHTSchool.Oneday,theheadmasterwhosen......
  • (hdu step 3.2.1)Max Sum(简单dp:求最大子序列和、起点、终点)
    题目:MaxSumTimeLimit:2000/1000MS(Java/Others)MemoryLimit:65536/32768K(Java/Others)TotalSubmission(s):1390AcceptedSubmission(s):542 ProblemDescriptionGivenasequencea[1],a[2],a[3]......a[n],yourjobistocalculatethemaxsu......
  • (hdu step 3.1.3)母牛的故事(简单递推)
    题目:母牛的故事TimeLimit:2000/1000MS(Java/Others)MemoryLimit:65536/32768K(Java/Others)TotalSubmission(s):659AcceptedSubmission(s):481 ProblemDescription有一头母牛,它每年年初生一头小母牛。每头小母牛从第四个年头开始,每年年初也生一头小......
  • (hdu step 3.2.3)Super Jumping! Jumping! Jumping!(DP:求最长上升子序列的最大和)
    题目:SuperJumping!Jumping!Jumping!TimeLimit:2000/1000MS(Java/Others)MemoryLimit:65536/32768K(Java/Others)TotalSubmission(s):896AcceptedSubmission(s):518 ProblemDescriptionNowadays,akindofchessgamecalled“SuperJumping!......
  • (hdu step 2.3.8)小兔的棋盘(卡特兰数:从左上角走到右上角的路径数)
    题目:     小兔的棋盘TimeLimit:1000/1000MS(Java/Others)MemoryLimit:32768/32768K(Java/Others)TotalSubmission(s):802AcceptedSubmission(s):502ProblemDescription小兔的叔叔从外面旅游回来给她带来了一个礼物,小兔高兴地跑回自己的房间,拆开一看是......
  • (hdu step 9.1.2)Doing Homework again(贪心——有n份作业,每份作业都有一定的完成时
    题目:DoingHomeworkagainTimeLimit:1000/1000MS(Java/Others)MemoryLimit:32768/32768K(Java/Others)TotalSubmission(s):63AcceptedSubmission(s):57 ProblemDescriptionIgnatiushasjustcomebackschoolfromthe30thACM/ICPC.Nowheha......