POJ 2240 Arbitrage

Source: Internet
Author: User

Description:

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 would 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" Respectivel Y "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

Test instructions: Knowing the name of the currency and the rate at which each currency is exchanged, it is now time to determine whether the value increases when each currency returns to that currency after a series of redemptions. (As with POJ 1860 Currency Exchange, just find the subscript that corresponds to the currency name)
#include <stdio.h>#include<string.h>#include<queue>#include<algorithm>using namespacestd;Const intn= -;Const intinf=0x3f3f3f3f;structnode{Chars[ the];} No[n];DoubleG[n][n], dist[n];intVis[n], cou[n], N;voidInit () {intI, J;  for(i =1; I <= N; i++) {Dist[i]= -INF; Vis[i]= Cou[i] =0;  for(j =1; J <= N; J + +) G[i][j]= -INF; G[i][i]=0; }}intJudge (CharA[])///Locate the subscript for string a{    inti;  for(i =1; I <= N; i++)    {        if(strcmp (A, no[i].s) = =0)            returni; }    return-1;}intSPFA () {intI, V; Queue<int>Q; Q.push (1); dist[1] =1;  while(!Q.empty ()) {v=Q.front ();        Q.pop ();  for(i =1; I <= N; i++)        {            if(Dist[i] < dist[v]*G[v][i]) {Dist[i]= dist[v]*G[v][i]; if(!Vis[i])                    {Q.push (i); Vis[i]=1; Cou[i]++; if(Cou[i] >= N)///There is a loop that represents an increase in the value of the currency because only after the currency exchange value increases will this step be made                        return 1; }}} Vis[v]=0; }    return 0;}intMain () {intI, J, M, ans, k =0; Chara[ the], b[ the]; DoubleD;  while(SCANF ("%d", &N), N) {Init (); K++;  for(i =1; I <= N; i++) scanf ("%s", NO[I].S); scanf ("%d", &m);  while(m--) {scanf ("%s%lf%s", A, &d, b); I=Judge (a); J=Judge (b); G[I][J]=D; } ans=SPFA (); if(ANS) printf ("Case %d:yes\n", K); Elseprintf"Case %d:no\n", K); }    return 0;}

POJ 2240 Arbitrage

Related Keywords:

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.