Poj2240--arbitrage (Bellman-ford algorithm)

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

3
USDollar
Britishpound
Frenchfranc
3
USDollar 0.5 Britishpound
Britishpound 10.0 Frenchfranc
Frenchfranc 0.21 USDollar

3
USDollar
Britishpound
Frenchfranc
6
USDollar 0.5 Britishpound
USDollar 4.9 Frenchfranc
Britishpound 10.0 Frenchfranc
Britishpound 1.99 USDollar
Frenchfranc 0.09 Britishpound
Frenchfranc 0.19 USDollar

0
Sample Output

Case 1:yes
Case 2:no

Similar to poj1860, the right value is a positive loop. It's just a node deal, I used a hash table here.

#include <iostream>#include <cstring>#include <string>#include <vector>#include <queue>#include <cstdio>#include <set>#include <cmath>#include <map>#include <algorithm>#define INF 0x3f3f3f3f#define MAXN 5005#define MOD 1000000007using namespace STD; Map<string,int>mpintN,m,e;DoubleDIS[MAXN];structnode{intBeg,end;DoubleR;}; Node edge[maxn<<1];voidAddintBintEnDoubleR) {edge[e].beg=b;    edge[e].end=en;    Edge[e].r=r; e++;}BOOLRelaxintP) {DoubleT=DIS[EDGE[P].BEG]*EDGE[P].R;if(dis[edge[p].end]<t) {dis[edge[p].end]=t;return true; }return false;}BOOLBellman () {memset(Dis,0,sizeof(dis)); dis[0]=1; for(intI=1; i<n; ++i) {BOOLflag=false; for(intj=0; j<e; ++J) {if(Relax (j)) flag=true; }if(dis[0]>1)return true;if(!flag)return false; } for(intj=0; j<e; ++J)if(Relax (j))return true;return false;}intMain () {intCnt=1; while(~scanf("%d", &n) &&n) {stringb; for(intI=0; i<n; ++i) {Cin>>a;        Mp[a]=i; }scanf("%d", &m);DoubleR E=0; while(m--) {Cin>>a>>r>>b;        Add (MP[A],MP[B],R); }if(Bellman ())printf("Case%d:yes\n", cnt++);Else            printf("Case%d:no\n", cnt++); }return 0;}

Poj2240--arbitrage (Bellman-ford algorithm)

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.