POJ-2240 Arbitrage (Shortest path Floyd algorithm && Bellman-ford algorithm)

Source: Internet
Author: User
Tags strcmp
ArbitragePOJ-2240
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 is P Ossible or not in the format ' case Case:yes ' respectively ' 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

Test instructions

The problem of currency arbitrage is to use the exchange rate difference between currencies, so that a unit of a certain currency can be exchanged back to more than one unit of the same currency. The title of each group of data gives the n currency, m type of exchange rate relationship, each relationship is the former exchange rate (one-way) of each unit. Whether the exchange rate through these exchange rates will appear arbitrage phenomenon.

Problem Solving Ideas:

In fact, each currency can be regarded as a point, the exchange rate between the currencies as the two points of the weight of the edge, after the construction of the map is to ask from a point of view, after a few paths can be more than their own large value (but note that the weight on the path is multiplied by the relationship is not added).

Floyd algorithm:

Because up to 30 kinds of currencies, so with the Flayd algorithm can also, the idea of the Floyd algorithm is also relatively good understanding, mainly is the structure of the diagram, (see Code comments) need to note that the topic requires maximum exchange rate relationship, so update the data is more than itself, and the weight of the multiplication relationship, the specific look at the code comment.

AC Code:

#include <stdio.h> #include <string.h> char huo[36][36];
Char aa[36], bb[36];

Double mp[36][36], x;
    int main () {int n, g = 1;     while (scanf ("%d", &n) && N) {for (int i = 1; I <= n; i++) scanf ("%s", Huo[i]);                      Deposit currency type int m;    Initialization diagram, because there is no exchange rate relationship of memset (Mp,0,sizeof (MP));           Currency cannot be exchanged, assigned 0 for (int i = 1; I <= n; i++) mp[i][i] = 1;
        The exchange rate between the same currency is 1 scanf ("%d", &m);
            for (int i = 1; I <= m; i++) {int x1, x2;
            scanf ("%s%lf%s", AA,&AMP;X,BB);
                for (int j = 1; J <= N; j + +) {if (strcmp (huo[j],aa) = = 0)//lookup is the first of several currencies, converted to points
                    {x1 = j;
                Break }} for (int j = 1; J <= N; j + +) {if (strcmp (huo[j],bb) = = 0)//
                Find is the first currency, convert to point {    x2 = j;
                Break   }} MP[X1][X2] = x;
            Weighted value} for (int k = 1, k <= N; k++) {for (int i = 1; I <= n; i++) {for (int j = 1; J <= N; j + +) {if (Mp[i][j] < Mp[i][k] * MP K
                [j])//Because the maximum exchange rate relationship is found, so for "<" and "*" mp[i][j] = mp[i][k] * Mp[k][j];
        }}} int flag = 0;
                for (int i = 1; I <= n; i++) {if (Mp[i][i] > 1)//discovery has more than its own value, indicating an arbitrage phenomenon {
                flag = 1;
            Break
        }} printf ("Case%d:", g++);
        if (flag) printf ("yes\n");
    else printf ("no\n");
} return 0;
 }

Bellman-ford algorithm:

Note that the shortest path of the algorithm from the source point V0 to the vertex v has a maximum of n-1 edges, but in the subject, because to find a loop, back to the source point V0, so there is a maximum of n edges, and more than n edge is not necessary, because if there is an arbitrage, n-side formation of the loop can form an arbitrage, specific

AC Code:

#include <stdio.h> #include <string.h> #define MAXN 50//vertex maximum #define MAXM 1000//number of sides maximum #define MAX (A, B) ((a) > (b)?
    (a):(B)) struct Exchange//exchange rate Relationship {int CI, CJ;
Double CIJ; }EX[MAXM]; An array of exchange rate relationships int I, j, K;    loop variable int n, m;
Currency type, number of exchange rates int flag, Kase = 0;
Char name[maxn][20], a[20], b[20]; Double x, MAXDIST[MAXN];
    The longest path length of the exchange rate, the source point I to each other vertex (including itself) int readcase ()//read into data {scanf ("%d", &n);
    if (n = = 0) return 0;
    for (i = 0; i < n; i++)//read into N currency name scanf ("%s", Name[i]);
    scanf ("%d", &m);
        for (int i = 0; i < m; i++)//read in exchange rate {scanf ("%s%lf%s", a,&x,b);
        for (j = 0; strcmp (A,name[j]); j + +);
        for (k = 0; strcmp (b,name[k]); k++); Ex[i].ci = j; EX[I].CIJ = x;
    EX[I].CJ = k;
} return 1;
    The//bellman-ford algorithm, with vertex v0 as the source point, asks it to the maximum distance of each vertex (including itself) void Bellman (int v0) {flag = 0;
    memset (maxdist,0,sizeof (maxdist));
    Maxdist[v0] = 1; for (k = 0; K < N 
            k++)//from Maxdist (0) to Maxdist (1),..., maxdist (n) {for (i = 0; i < m; i++)//To determine each edge, whether it can increase the maximum distance by adding { if (maxdist[ex[i].ci] * ex[i].cij > MAXDIST[EX[I].CJ]) MAXDIST[EX[I].CJ] = Maxdist[ex[i].
        CI] * EX[I].CIJ;
}} if (Maxdist[v0] > 1.0) flag = 1; } int main () {while (Readcase ())//read into currency type {for (i = 0; i < n; i++) {Bellman (i  );
        To find the longest path if (flag) break from the first vertex;
        } if (flag) printf ("Case%d:yes\n", ++kase);
    else printf ("Case%d:no\n", ++kase);
} 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.