POJ 2240 arbitrage: Shortest Way Floyd

Source: Internet
Author: User
Tags printf

arbitrage:http://poj.org/problem?id=2240

Effect:

Give you m currency, give you m currency exchange rules, ask whether through these rules can finally make a profit. Eg:1 dollar for 0.5 pounds, 1 pounds for 10 francs, 1 francs for 0.21 dollars, so that 1 dollars can change 0.5*10*0.21=1.05 dollars, net earn 0.05 dollars.

Ideas:

Use Floyd to find out the biggest exchange between each of the two kinds of money, and iterate through it to see if there is any kind of coin that finally makes a profit, there is the output Yes, no is no. When dealing with the relationship between a coin name and a number, you can save it with a map (more useful), and of course you can compare it with a string.

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

#include <stdio.h> #include <iostream> #include <map> #define INF 0x3f3f3f3f using namespace std;
Char money[30];
Char change1[30], change2[30];
Double Trans;
Double map[50][50];
int n, m;
  
Using map to establish the relationship between string and number map<string, int>p; void Floyd () {for (int k = 1; k <= N; k++) {for (int i = 1; I <= n; i++) {for (i NT J = 1; J <= N;
                J + +) {The maximum path of the///transformation changes ' + ' to ' * ' if (Map[i][j] < map[i][k]*map[k][j])
                {Map[i][j] = map[i][k]*map[k][j];
}}} return;
    } void Solve () {int cnt = 1;  while (~SCANF ("%d%*c", &n) && N) {for (int i = 1; I <= n; i++) {scanf ("%s"),
            Money);
            P[money] = i;
        Map[i][i] = 1;
        } scanf ("%d%*c", &m); for (int i = 1; I <= m i++) {scanf ("%s%lf%s", Change1, &trans, Change2); Map[p[change1]][p[change2]] = trans;
        To map the data map} Floyd ();
        BOOL flag = FALSE;
                for (int i = 1; I <= n; i++) {if (Map[i][i] > 1) {flag = true;
            Break
        } if (flag) {printf ("Case%d:yes\n", cnt++);
        else {printf ("Case%d:no\n", cnt++);
  
    int main () {Solve ()}}
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.