[ACM] HDU 1217 arbitrage (bellman_ford is the shortest path to determine whether a positive loop or floyed exists)

Source: Internet
Author: User
Tags integer numbers

Arbitrage

Problem descriptionarbitrage 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.
 
Inputthe input file will contain in 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.
 
Outputfor each test case, print one line telling whether Arbitrage is possible or not in the format "case: yes" respectively "case: No ".
 
Sample Input
3USDollarBritishPoundFrenchFranc3USDollar 0.5 BritishPoundBritishPound 10.0 FrenchFrancFrenchFranc 0.21 USDollar3USDollarBritishPoundFrenchFranc6USDollar 0.5 BritishPoundUSDollar 4.9 FrenchFrancBritishPound 10.0 FrenchFrancBritishPound 1.99 USDollarFrenchFranc 0.09 BritishPoundFrenchFranc 0.19 USDollar0
 
Sample output
Case 1: YesCase 2: No
 
Sourceuniversity of Ulm Local contest 1996


Solution:

Method 1:

A currency, after several rounds of exchange, returns to the currency to increase its value. This is "Arbitrage ". The bellman_ford algorithm is used to obtain the maximum profit, instead of short circuit. The bellman algorithm is used to change the value. Determine whether a positive loop exists.

Code:

# Include <iostream> # include <string. h> using namespace STD; const int maxn = 35; double dis [maxn]; string STR [maxn]; int nodenum, edgenum; struct edge {int S, E; double W;} edge [maxn * maxn]; bool bellman_ford (INT start) {for (INT I = 1; I <= nodenum; I ++) dis [I] = 0; // Change 1 dis [start] = 1; bool OK; For (INT I = 1; I <= nodeNum-1; I ++) {OK = 0; For (Int J = 1; j <= edgenum; j ++) {If (DIS [edge [J]. s] * edge [J]. w> dis [edge [J]. e]) // change 2 {dis [edge [J]. E] = dis [edge [J]. S] * edge [J]. W; OK = 1 ;}} if (! OK) break;} For (INT I = 1; I <= edgenum; I ++) if (DIS [edge [I]. s] * edge [I]. w> dis [edge [I]. e]) return true; return false;} int main () {int c = 1; while (CIN> nodenum & nodenum) {for (INT I = 1; I <= nodenum; I ++) CIN> STR [I]; CIN> edgenum; string from, to; double W; For (INT I = 1; I <= edgenum; I ++) {CIN> from> W> to; Int J, K; For (j = 1; j <= nodenum; j ++) if (from = STR [J]) break; For (k = 1; k <= nodenum; k ++) if (to = STR [k]) break; edge [I]. S = J; edge [I]. E = K; edge [I]. W = W;} If (bellman_ford (1) cout <"case" <C ++ <": yes" <Endl; else cout <"case" <C ++ <": No" <Endl;} return 0 ;}
Method 2:

Create a graph and an adjacent matrix to find the shortest short circuit between any two edges. If dis [I] [I]> 1, it indicates that a positive weight loop exists. MAP is used in the code, and the ing between string and integer numbers is

Code:

#include <iostream>#include <map>#include <string.h>using namespace std;const int maxn=40;string str[maxn];double mp[maxn][maxn];int nodeNum,edgeNum;void floyed(){    for(int k=1;k<=nodeNum;k++)        for(int i=1;i<=nodeNum;i++)            for(int j=1;j<=nodeNum;j++)            {                if(mp[i][j]<mp[i][k]*mp[k][j])                    mp[i][j]=mp[i][k]*mp[k][j];            }}int main(){    int c=1;    while(cin>>nodeNum&&nodeNum)    {        map<string,int>st;        for(int i=1;i<=nodeNum;i++)        {            cin>>str[i];            st[str[i]]=i;        }        for(int i=1;i<=nodeNum;i++)            for(int j=1;j<=nodeNum;j++)        {            if(i==j)                mp[i][j]=1;            else                mp[i][j]=0;        }        cin>>edgeNum;        string from,to;double w;        for(int i=1;i<=edgeNum;i++)        {            cin>>from>>w>>to;            mp[st[from]][st[to]]=w;        }        floyed();        bool ok=0;        for(int i=1;i<=nodeNum;i++)            if(mp[i][i]>1)             ok=1;        if(ok)            cout<<"Case "<<c++<<": Yes"<<endl;        else            cout<<"Case "<<c++<<": No"<<endl;    }    return 0;}




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.