Simple POJ 2240 ARBITRAGE,SPFA.

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.

The question is whether the currency can be converted and let itself increase ...

Use the SPFA to judge the ring ... Enumerate each point for SPFA (i.e. Floyd is also possible ...). )。。。

The code is as follows:

#include <iostream>#include<cstring>#include<cstdio>#include<queue>using namespacestd;Const intinf=10e8;Const intmaxn= +;structedge{intv; DoubleCost ; Edge (int_v=0,Double_cost=0): V (_v), cost (_cost) {}};vector<Edge>E[MAXN];BOOLVIS[MAXN];intCOUNODE[MAXN];BOOLSPFA (DoubleLowcost[],intNintstart) {Queue<int>que; intu,v; DoubleC; intLen;  for(intI=1; i<=n;++i) {vis[i]=0; Counode[i]=0; Lowcost[i]=0; } Vis[start]=1; Counode[start]=1; Lowcost[start]=1;    Que.push (start);  while(!Que.empty ()) {u=Que.front ();        Que.pop (); Vis[u]=0; Len=e[u].size ();  for(intI=0; i<len;++i) {v=e[u][i].v; C=E[u][i].cost; if(lowcost[u]*c>Lowcost[v]) {Lowcost[v]=lowcost[u]*C; if(!Vis[v]) {Vis[v]=1; ++Counode[v];                    Que.push (v); if(counode[v]>=N)return 0; }            }        }    }    return 1;} InlinevoidAddedge (intUintVDoublec) {E[u].push_back (Edge (V,c));}Charss[ +][ -];DoubleANS[MAXN];intN;intFindChar*R) {     for(intI=1; i<=n;++i)if(strcmp (s,ss[i]) = =0)            returni;}intMain () {intM; BOOLOK; Charts1[ -],ts2[ -]; intt1,t2; DoubleTR; intcas=1;  for(SCANF ("%d", &n); N;SCANF ("%d", &n), + +CAs) {         for(intI=1; i<=n;++i) {scanf ("%s", Ss[i]);        E[i].clear (); } scanf ("%d",&l);  for(intI=1; i<=m;++i) {scanf ("%s%lf%s",ts1,&tr,ts2); T1=find (TS1); T2=find (TS2);        Addedge (T1,T2,TR); } OK=0;  for(intI=1; i<=n;++i)if(!SPFA (ans,n,i)) {OK=1;  Break; } printf ("Case %d:", CAs); if(OK) printf ("yes\n"); Elseprintf ("no\n"); }    return 0;}
View Code

Simple POJ 2240 ARBITRAGE,SPFA.

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.