Topic Links:
http://poj.org/problem?id=2240
Title Description:
To the N currencies, to the exchange rate between M-currencies, ask whether the conversion between the money to gain benefits,
Problem Solving Ideas:
By test instructions know, this problem not only find a path, so the most suitable is to choose Floyd to solve the problem, Judge Map[i][i] there is no value greater than 1
Ps:floyd template title.
1#include <cstdio>2#include <cstring>3#include <cstdlib>4#include <algorithm>5#include <iostream>6 using namespacestd;7 #defineMAXN 358 CharNAME[MAXN][MAXN];//the names of various currencies9 DoubleMAP[MAXN][MAXN];//Map[i][j]i-->j's exchange rateTen intN; One A voidinit (); - intFind (Charstr[]); - voidFloyd (); the - intMain () - { - intM, I, j, k =0; + Doubles; - CharSTR1[MAXN], STR2[MAXN]; + A while(SCANF ("%d", &N), N) at { - init (); - for(i=0; i<n; i++) -scanf ("%s", Name[i]); -scanf ("%d", &m); - while(M--) in { -scanf ("%s%lf%s", STR1, &s, str2); to intA =find (STR1); + intb =find (STR2); -MAP[A][B] =s; the } * Floyd (); $m =0;Panax Notoginseng for(i=0; i<n; i++) - if(Map[i][i] >1) them =1; + if(m) Aprintf ("Case %d:yes\n", ++k); the Else +printf ("Case %d:no\n", ++k); - } $ return 0; $ } - - voidInit ()//initialized to a the { - intI, J;Wuyi for(i=0; i<n; i++) the for(j=0; j<n; J + +) -MAP[I][J] =1; Wu } - intFind (Charstr[]) About { $ inti; - for(i=0; i<n; i++) - if(!strcmp (name[i], str)) - returni; A } + voidFloyd () the { - intI, J, K; $ for(i=0; i<n; i++) the for(k=0; k<n; k++) the for(j=0; j<n; J + +) theMAP[I][J] = max (Map[i][j], map[i][k]*map[k][j]); the}
POJ 2240 Arbitrage