Link: poj 2240
First, the exchange rate between the n currencies is given. For example, usdollar 0.5 britishpound indicates that 1 usdollar is converted to 0.5 britishpound. Ask if there are any currencies in the n currencies that have been exchanged for several times. After being exchanged into the original currency, the amount of currency can be added. Thought: This question is actually the deformation of Floyd. The conversion rate is used as the path weight of the graph. A graph is a directed graph. Finally, the relaxation operation is transformed into: If (DIS [I] [J] <dis [I] [k] * Dis [k] [J]).
# Include <stdio. h> # include <string. h> int main () {int n, m, I, J, K, L, R, T = 0; char s [35] [30], S1 [30], s2 [30]; Double A [35] [35], C; while (scanf ("% d", & N )! = EOF) {If (n = 0) break; t ++; memset (A, 0, sizeof (a); // It is initialized to 0 at the beginning, instead of infinity, it can be initialized to an infinitely small for (I = 1; I <= N; I ++) {scanf ("% s", s [I]); A [I] [I] = 1.0; // your own tax rate is initialized to 1} scanf ("% d", & M); While (M --) {scanf ("% S % lf % s", S1, & C, S2); L = r = 0; for (I = 1; I <= N; I ++) {If (strcmp (S1, s [I]) = 0) // your own exchange rate exists. All two strings may be equal to L = I; if (strcmp (S2, s [I]) = 0) // previously, because else was added to exclude the above situation, Wa r = I; if (L & R) break;} A [l] [r] = C;} For (k = 1; k <= N; k ++) // Floyd algorithm for (I = 1; I <= N; I ++) for (j = 1; j <= N; j ++) if (A [I] [k] * A [k] [J]> A [I] [J]) // relaxation condition a [I] [J] = A [I] [k] * A [k] [J]; k = 0; for (I = 1; I <= N; I ++) if (a [I] [I]> 1) {k = 1; break;} If (k) printf ("case % d: yes \ n ", T); else printf (" case % d: NO \ n ", T);} return 0 ;}
Poj 2240 arbitrage (Floyd)