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; }