Arbitrage (HDU 1217) __floyd algorithm

Source: Internet
Author: User
Arbitrage Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 4160 accepted Submission (s): 1890


Problem Description Arbitrage is the use of discrepancies in currency exchange rates to transform one unit of a currency I Nto more than one of the same currency. For example, suppose 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 that takes a list of currency exchange rates as input and then determines whether is possible or not.

Input the input file would contain one or more test cases. Om the ' the ' the ' the ' the ' the ' the ' the ' the ' the ' of each ' test case there ' is ' an integer n (1<=n<=30) 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 and representing the length of the table to follow. The last m lines each contain the name CI to 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 does not appear in the table are impossible.
Test cases are separated from each of the blank line. The Input is terminated by a value of zero (0) for N.

Print one line telling whether Arbitrage's possible or not in the format ' case Case:yes ' resp ectively "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 Briti Shpound 1.99 USDollar Frenchfranc 0.09 britishpound Frenchfranc 0.19 USDollar 0
Sample Output
Case 1:yes Case 2:no This problem as long as you understand the shortest path of the solution to the idea, it is not difficult to AC.
#include <iostream> #include <string> #include <map> using namespace std; const int INF = 0.00001;
    Because of the maximum value, the INF should be used to represent an infinitely small number int main () {int n,m,i,j,k,t=0;
    Double d[50][50];

    String x[50],x1,x2;  while (Cin >> N, N) {for (i=1; i<=n; i++) for (j=1; j<=n; j + +) D[i][j] =
        INF;
        Map<string,int> p;
            For (I=1 i<=n; i++) {cin >> x[i];
        P[x[i]] = i;
        Cin >> m;
        Double C;
            For (I=1 i<=m; i++) {cin >> x1 >> C >> x2;
        D[P[X1]][P[X2]] = c; For (k=1 k<=n; k++) for (I=1 i<=n; i++) for (j=1; j<=n; j + +) if (d[i
                  ][K] * D[k][j] > D[i][j])//MAX, so it is ">".
        D[I][J] = d[i][k] * D[k][j];
        cout << "Case" << ++t << ":";
        cout << d[1][1] << Endl; IF (D[1][1] > 1) cout<< "Yes" <<endl;
        Because is to seek their own conversion, so is d[1][1];           else cout<< "No" <<endl;
    If it is to find the beginning to the end of the conversion of N, it should be d[1][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.