Give you n currencies, and then m to ensure that each currency can be changed to another currency. The exchange rate of the two currencies is given to you. You need to find out if you can find a way to make money by making the final exchange as the starting currency.
Idea: This topic is also a graph creation model. It can be assumed that, starting from a certain point and returning to the origin, the value of your own has increased, so you only need to multiply the exchange rate to the starting point when it is greater than 1. Therefore, the obvious Floyd algorithm.
AC code:
# Include <cstdio> # include <cstring> # include <map> # include <string> # include <algorithm> using namespace std; double str [31] [31]; map <string, int> hash; // you can use ING to find whether the value is true in simple cases. // You can also convert a currency point to a specific number, for example, RMB can be used as the int n, m; void Floyd () {int I, j, k; double flag =-1; for (k = 1; k <= n; k ++) for (I = 1; I <= n; I ++) for (j = 1; j <= n; j ++) {str [I] [j] = max (str [I] [j], str [I] [k] * str [k] [j]);} for (I = 1; I <= n; I ++) flag = max (flag, str [I] [I]); if (flag> 1) prin Tf ("Yes \ n"); else printf ("No \ n");} int main () {int I, j, k; int cnt = 1; char s [31]; while (scanf ("% d", & n )! = EOF) {getchar (); int tot, flag; if (n = 0) break; memset (str, 0, sizeof (str); hash. clear (); // flag = tot = 0; for (I = 1; I <= n; I ++) {scanf ("% s ", s); if (! Hash [s]) hash [s] = ++ tot; // give the currency to a code} scanf ("% d", & m); for (I = 1; I <= m; I ++) {double a; char str1 [31], str2 [31]; scanf ("% s % lf % s", str1, &, str2); int x = hash [str1]; int y = hash [str2]; str [x] [y] =; if (x = y & a> 1) flag = 1;} printf ("Case % d:", cnt ++); if (flag) printf ("Yes \ n"); else Floyd ();} return 0 ;}
HDU 1217 Arbitrage