Poj 2240 arbitrage (Floyd)

Source: Internet
Author: User
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)

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.