ZOJ 1092 Arbitrage (Floyd variant)

Source: Internet
Author: User

Given the exchange rate of various currencies, you can switch between currencies to see if you can use some kind of exchange, which will make you end up with more money than you have just started, and of course the same currency.

If usually do the shortest path certainly do not know can use Floyd to do, I saw a paper before, said, the relaxation process of

D[V] < D[u] + W (v,u)

The plus can be extended to various forms of operation, then did not notice, thought should be no use, happened in this problem Floyd can also change the symbol. It's magical, remember, you might use it later.

#include <stdio.h> #include <string.h> int n,i,j,k,edges; int from,to; Double weight; int flag,start = 1; Double biao[31][31]; Char kindof[31][50]; Char t[50]; int find (char *a) {int i; for (i = 1; I <= N, i++) if (strcmp (a,kindof[i]) = = 0) return i;} int main (void) {while (s CANF ("%d", &n)! = EOF && N) {for (i = 1; i <=; i++) for (j = 1; J <=; j + +) Biao[i][j] = 0; for ( i = 1; I <= 30; i++) Biao[i][i] = 1; for (i = 1; I <= n; i++) scanf ("%s", Kindof[i]); scanf ("%d", &edges); for (i = 1; I <= edges; i++) {scanf ('%s ', t); from = find (t); scanf ("%lf%s", &weight,t); to = find (t); biao[from][t O] = weight; } for (k = 1; k <= N, k++) for (i = 1; I <= n; i++) for (j = 1; J <= N; j + +) if (Biao[i][j] < biao[i][k] * BIAO[K][J]) biao[i][j] = biao[i][k] * Biao[k][j]; printf ("Case%d:", start++); for (i = 1, flag = 0; I <= n; i++) if (Biao[i][i] > 1.0) {flag = 1;printf ("yes/n"); break;} if (!flag) printf ("no/ n "); } RETurn 0; }

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.