POJ 1860 Currency Exchange (Bellmanford algorithm inverse use to determine if there is a positive ring)

Source: Internet
Author: User
Tags printf
Topic links

POJ1860 Topic

There are N (1≤\len≤\le100) currencies (numbered 1 to n), m currency 22 redemption laws (including exchange rate R and handling fee C), the specific law is: When you exchange B currency in 100A, a to B exchange rate is 29.75, the handling fee is 0.39, then you can get (100- 0.39) * 29.75 = 2963.3975 b currency. Now one person has the S-model currency, which is V, and asks if he can go through the exchange and eventually get the S model currency and increase the amount (currency cannot be negative during the transaction). Analysis

A currency is regarded as a node on a graph, and a method of exchange can be regarded as an edge.
The weight of A to B is (Va-cab) *rab, note that the weight of s to S is v.
After the composition can be found that the topic is to determine whether the graph constitutes a positive right loop, so you can use Bellman-ford algorithm to find the longest road, with the opposite relaxation conditions relaxation N-1 wheel, if you can continue to relax there is a positive right loop. Code

#include <iostream> #include <cstdio> using namespace std;
const int maxm=210;

const int maxn=110;
    struct Edge {int beg;
    int end;
Double r,c;
};

Edge EDGE[MAXM];
int edgenum,n,m,s;
Double Dis[maxn],val;
    void Add_edge (int beg,int end,double r,double c) {edgenum++; edge[edgenum].beg=beg;//Start currency edge[edgenum].end=end;//End currency edge[edgenum].r=r;//exchange rate edge[edgenum].c=c;//fee} Boo L Bellmen_ford () {for (int i=1;i<=n;i++) dis[i]=0;//here is exactly the opposite of Bellman's purpose.
        Initialize the amount of money in the initial hand of the source point to the point distance of Infinity dis[s]=val;//for (int k=1;k<=n-1;k++)//N-1 wheel slack {bool Flag=false;
            for (int i=1;i<=edgenum;i++) {int U=edge[i].beg;
            int v=edge[i].end; if (dis[v]< (DIS[U]-EDGE[I].C) *EDGE[I].R)//Find the longest path {////to compare is "a point to the right of their own
                The right flag=true of the value "and" one point to another;
            dis[v]= (DIS[U]-EDGE[I].C) *EDGE[I].R; }} if (!flag) RETUrn false;
            } for (int i=1;i<=edgenum;i++)//positive ring can be infinitely relaxed if (dis[edge[i].end]< (DIS[EDGE[I].BEG]-EDGE[I].C) *EDGE[I].R)
    return true;
return false;
    } int main () {int a A, B;
    Double R_ab,c_ab,r_ba,c_ba;
        while (scanf ("%d%d%d%lf", &n,&m,&s,&val)!=eof) {edgenum=0; for (int i=1;i<=m;i++) {scanf ("%D%D%LF%LF%LF%LF",&a,&b,&r_ab,&c_ab,&r_ba,&
            C_BA);
            Add_edge (A,b,r_ab,c_ab);
        Add_edge (B,A,R_BA,C_BA);
        } bool Ans=bellmen_ford ();
        if (ans) printf ("yes\n");
    else printf ("no\n");
} return 0;
 }

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.