poj2240 Arbitrage (SPFA award ring)

Source: Internet
Author: User

Arbitrage
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 10997 Accepted: 4622

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

Source

ULM Local 1996 In fact, the main idea of the topic is whether the exchange rate of the different countries to earn more than the original money. Which is the SPFA award ring.
#include <stdio.h> #include <vector> #include <string.h> #include <queue>using namespace std; int n,m;//adjacency table vector<int>map[33];//Storage Country name Char name[33][100];//two countries exchange rate double f[33][33];//beginning country to current country converted currency Amount Double price[33];//the number of degrees int in_degree[33];//into the queue int sum[33];//whether in queue bool In_queue[33];int tonum (char *str) {for (int i=0; i<n;i++) if (strcmp (Str,name[i]) ==0) return i;} BOOL SPFA (int x) {queue<int>s;sum[x]++;p rice[x]=1.0;in_queue[x]=true;s.push (x); while (!s.empty ()) {int pos= S.front (); S.pop (); in_queue[pos]=false;for (int i=0;i<map[pos].size (); i++) {int edge=map[pos][i];if (Price[edge] <price[pos]*f[pos][edge]) {price[edge]=price[pos]*f[pos][edge];if (!in_queue[edge]) {in_queue[edge]=true; S.push (Edge), if (++sum[edge]>in_degree[edge]) {return true;}}}} return false;} int main () {int t=0;while (~scanf ("%d", &n)) {if (n==0) Break;memset (in_degree,0,sizeof (In_degree)); Memset (name,0, sizeof (name)); memset (map,0,sizeof (map)); Memset (F,0,sizeof (f)); for (int i=0;i<n;i++) scanF ("%s", Name[i]), scanf ("%d", &m), for (int i=0;i<m;i++) {char str1[100],str2[100];memset (str1,0,sizeof (str1)); memset (str2,0,sizeof (str2));d ouble x;scanf ("%s%lf%s", str1,&x,str2); int a=tonum (STR1); int b=tonum (STR2); f[a][b ]=x;in_degree[b]++;map[a].push_back (b);} BOOL Ok=false;for (int i=0;i<n;i++) {if (SPFA (i)) {ok=true;break;} memset (in_queue,false,sizeof (In_queue)); memset (price,0,sizeof (Price)); memset (sum,0,sizeof (sum));} if (OK) printf ("Case%d:yes\n", ++t); elseprintf ("Case%d:no\n", ++t);} return 0; }


poj2240 Arbitrage (SPFA award ring)

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.