(Weird Floyd & self-ring) MZ training 2014 #15 E question (poj 2240)

Source: Internet
Author: User

Have you ever seen such a strange Floyd?

First.

[Description]

There is a difference in the exchange rate of the currency, for example, assume that 1 dollar buys 0.5, 1 pound buys 10 francs, and 1 French francs buys 0.21 dollars. Then, by currency conversion, a smart trader can buy 0.5*10*0.21 = 1.05 USD from 1 USD, making a profit of 5%.

Your task is to write a program that uses a currency exchange rate list as the input, and then determine whether or not you can make a profit.

[Intput]

The input contains multiple groups of test data. The first row of each group contains N (1 <= n <= 30), which indicates that there are n medium currencies and the next n rows, each row is a currency name with no space in the name. The next row contains an integer m, followed by M. Each row contains three parts: Si, rij, SJ, it indicates that the exchange rate of Si to SJ is rij. Each group of data is separated by an empty row. When n = 0, the input ends.

 

[Output]

Output "case I: YES" or "case I: No" for each group of data indicates whether group I data is feasible.

 

[Sample input]

3

Usdollar

Britishpound

Frenchfranc

3

Usdollar 0.5 briishpound

Britishpound 10.0 frenchfranc

Frenchfrankc 0.21 usdollar

 

3

Usdollar

Britishpound

Frenchfranc

6

Usdollar 0.5 briishpound

Usdollar 4.9 frenchfranc

Britishpound 10.0 frenchfranc

Britishpound 1.99 usdollar

Frenchfranc 0.09 britishpound

Frenchfrankc 0.19 usdollar

 

0

[Sample onput]

Case 1: Yes

Case 2: No

At first glance, it looks like a mathematical problem. Actually, it's Graph Theory (I believe everyone can see it ).

Consider exchange rates as edges and currencies as points.

Floyd.

<span style="color:#660000;"><span style="font-size:18px;">for(int k=1;k<=n;k++)</span><span style="font-size:14px;"></span><span style="font-size:18px;">for(int i=1;i<=n;i++)for(int j=1;j<=n;j++){if(i==j || i==k || j==k) continue;if(dis[i][j]<dis[i][k]*dis[k][j]){dis[i][j]=dis[i][k]*dis[k][j];}}</span></span>
Pay special attention to the transfer equation of this question: Of course, the exchange rate or something must be multiplied.

Note: This question cannot be ruled out from the self-loop (my subconscious Code allows me to adjust for a few hours), that is, I changed my own exchange rate more than 1 (this Nima is unscientific !)

<Span style = "color: # 3366ff; Background-color: RGB (102,255,153 ); "> # include <cstdio> # include <iostream> # include <cstring> # include <cstdlib> # include <algorithm> # include <cmath> using namespace STD; int N, m; double dis [35] [35]; char name [35] [3110]; char TMP [3110]; int ansnum = 0; int main () {While (1) {scanf ("% d", & N); If (n = 0) break; memset (name, 0, sizeof (name); For (INT I = 1; I <= N; I ++) for (Int J = 1; j <= N; j ++) dis [I] [J] = 0.0; for (INT I = 1; I <= N; I ++) scanf ("% s", name [I]); scanf ("% d", & M ); int U, V; double W; For (INT I = 1; I <= m; I ++) {memset (TMP, 0, sizeof (TMP )); scanf ("% s", TMP); For (Int J = 1; j <= N; j ++) {If (strcmp (TMP, name [J]) = 0) {u = J; break;} scanf ("% lf", & W); memset (TMP, 0, sizeof (TMP )); scanf ("% s", TMP); For (Int J = 1; j <= N; j ++) {If (strcmp (TMP, name [J]) = 0) {v = J; break;} dis [u] [v] = max (DIS [u] [v], W );} for (int K = 1; k <= N; k ++) for (INT I = 1; I <= N; I ++) for (Int J = 1; j <= N; j ++) {if (I = j | I = k | j = k) continue; if (DIS [I] [J] <dis [I] [k] * Dis [k] [J]) {dis [I] [J] = dis [I] [k] * Dis [k] [J] ;}} int OK = 0; For (INT I = 1; I <= N; I ++) {for (Int J = 1; j <= N; j ++) {// if (I = J) continue; this statement must not contain if (DIS [I] [J] * Dis [J] [I]> 1.0) {OK = 1 ;}if (OK) break ;} if (OK) break;} printf ("case % d:", ++ ansnum); If (OK) printf ("Yes \ n "); else printf ("NO \ n");} return 0 ;}</span>



(Weird Floyd & self-ring) MZ training 2014 #15 E question (poj 2240)

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.