HDU 1217 Arbitrage (Freud)

Source: Internet
Author: User
Tags strcmp

Arbitrage

Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 6360 Accepted Submission (s): 2939


Problem Descriptionarbitrage is the use of discrepancies in currency exchange rates to transform one unit of a currency in To 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, 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.

Inputthe input file 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.

Outputfor each test case, print one line telling whether arbitrage was possible or not in the format "case Case:yes" Respe ctively "Case Case:no".

Sample input3usdollarbritishpoundfrenchfranc3usdollar 0.5 britishpoundbritishpound 10.0 Frenchfrancfrenchfranc 0.21 USDollar 3usdollarbritishpoundfrenchfranc6usdollar 0.5 britishpoundusdollar 4.9 frenchfrancbritishpound 10.0 Frenchfrancbritishpound 1.99 usdollarfrenchfranc 0.09 Britishpoundfrenchfranc 0.19 USDollar 0

Sample outputcase 1:yescase 2:no

Sourceuniversity of Ulm Local Contest 1996

Recommendeddy | We have a carefully selected several similar problems for you:1142 1162 1385 1301 1596 the shortest-circuiting deformation, because the data only to 30, so the Floyd algorithm can be used, but need Note that this is the maximum magnification. Test instructions: The main idea is to give you the exchange between the various currencies, ask if you have 1 units of a currency after a circuit after the exchange of >=1 units (profit). Attached code:
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cmath>5 #defineM 356 using namespacestd;7 DoubleMap[m][m];8 intN;9 Ten voidFloyd ()//calculating the maximum odds using the Floyd algorithm One { A     intk,i,j; -      for(k=1; k<=n; k++) -          for(i=1; i<=n; i++) the              for(j=1; j<=n; J + +) -                 if(map[i][j]<map[i][k]*Map[k][j]) -map[i][j]=map[i][k]*Map[k][j]; - } +  - intMain () + { A     intm,i,j,w=1; at     CharS[m],str[m][m]; -      while(~SCANF ("%d", &n) &&N) -     { -          for(i=1; i<=n; i++) -scanf"%s", Str[i]); -          for(i=1; i<=n; i++) in              for(j=1; j<=n; J + +) -             { to                 if(I==J) map[i][j]=1;//since the maximum exchange rate is found, the initial turn itself is 1, and the other translates to 0 +                 Elsemap[i][j]=0; -             } thescanf"%d",&m); *         intb; $         DoubleC;Panax Notoginseng          for(i=1; i<=m; i++) -         { thescanf"%s", s); +              for(a=1; a<=n; a++)//Convert it to a map array record A                 if(!strcmp (S,str[a])) the                      Break; +scanf"%LF",&c); -scanf"%s", s); $              for(b=1; b<=n; b++) $                 if(!strcmp (S,str[b])) -                      Break; -map[a][b]=C; the         } - Floyd ();Wuyicout<<" Case"<<w++<<": "; the         if(map[1][1]>1) -cout<<"Yes"<<Endl; Wu         Else -cout<<"No"<<Endl; About     } $     return 0; -}

HDU 1217 Arbitrage (Freud)

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.