Nyoj Arbitrage (Floyd or SPFA or Bellman-ford)

Source: Internet
Author: User

Describe
Arbitrage is the use of discrepancies in currency exchange rates to transform one unit of a currency into more than one UN It's 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, MA King a profit of 5 percent.

Your job is to write a program this takes a list of currency exchange rates as input and then determines whether arbitrage is possible or not.

Input
The input file is contain one or more test cases. Om the first line of all test case there was an integer n (1<=n<=30), representing the number of different Currencie S. The next n lines each contain the name of one currency. Within a name no spaces would appear. The next line contains one integer m, representing the length of the table to follow. The last m lines all 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 is impossible.
Test cases is separated from 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 are possible or not in the format "case Case:yes" Respe ctively "Case 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
Source
Nkoj or 1996/97 Ulm Internal Contest
Uploaded by
Miao-dong building

Test instructions: Given the rate of exchange between some currencies and currencies, ask if it is possible to convert a currency to a certain number of columns and then increase the value of the currency. For example, $1 is more than $1 after some conversion. You can output yes, otherwise output No.

AC Code:

1#include <vector>2#include <map>3#include <Set>4#include <algorithm>5#include <iostream>6#include <cstdio>7#include <cmath>8#include <cstdlib>9#include <string>Ten#include <cstring> One#include <queue> A using namespacestd; - #defineINF 0x3f3f3f3f - #defineMAX 111 the  - DoubleMp[max][max]; - intn,m; -  + voidFloyd () - { +      for(intk=1; k<=n; k++) A          for(intI=1; i<=n; i++) at              for(intj=1; j<=n; J + +) -                 if(mp[i][j]< mp[i][k]*Mp[k][j]) -mp[i][j]=mp[i][k]*Mp[k][j]; - } -  - voidInit () in { -      for(intI=1; i<=n; i++){ to          for(intj=1; j<=n; J + +){ +             if(i==j) -mp[i][j]=1; the             Else *mp[i][j]=0; $         }Panax Notoginseng     } - } the  + intMain () A { the     intsum=0; +     DoubleRate ; -     Chara[111],b[111],c[111]; $      while(~SCANF ("%d", &n) &&N) { $ init (); -map<string,int>MMP; -          for(intI=1; i<=n; i++){ thescanf"%s", a); -mmp[a]=i;Wuyi         } thescanf"%d",&m); -          for(intI=1; i<=m; i++){ Wuscanf"%s%lf%s",b,&rate,&c); -             intx=Mmp[b]; About             inty=Mmp[c]; $mp[x][y]=Rate ; -             //printf ("%d\n", Mp[x][y]); -         } - Floyd (); A         intflag=0; +          for(intI=1; i<=n; i++){ the             //printf ("%d\n", Mp[i][i]); -             if(mp[i][i]>1){ $flag=1; the                  Break; the             } the         } theprintf"Case %d:",++sum); -printf"%s\n", Flag?"Yes":"No");  in     } the}

Nyoj Arbitrage (Floyd or SPFA or Bellman-ford)

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.