标签:
题目链接:
http://poj.org/problem?id=2240
题目描述:
给n种货币,给m个货币间的汇率,问能不能通过货币之间的转化而获得利益,
解题思路:
由题意知,这个题不止求一条路径,所以最适合就是选择floyd去解决这个题目,判断map[i][i]有没有大于1的值
ps:floyd模板题目。
1 #include <cstdio> 2 #include <cstring> 3 #include <cstdlib> 4 #include <algorithm> 5 #include <iostream> 6 using namespace std; 7 #define maxn 35 8 char name[maxn][maxn];//各种货币的名字 9 double map[maxn][maxn];//map[i][j]i-->j的汇率 10 int n; 11 12 void init (); 13 int find (char str[]); 14 void floyd (); 15 16 int main () 17 { 18 int m, i, j, k = 0; 19 double s; 20 char str1[maxn], str2[maxn]; 21 22 while (scanf ("%d", &n), n) 23 { 24 init(); 25 for (i=0; i<n; i++) 26 scanf ("%s", name[i]); 27 scanf ("%d", &m); 28 while (m --) 29 { 30 scanf ("%s %lf %s", str1, &s, str2); 31 int a = find (str1); 32 int b = find (str2); 33 map[a][b] = s; 34 } 35 floyd(); 36 m = 0; 37 for (i=0; i<n; i++) 38 if (map[i][i] > 1) 39 m = 1; 40 if ( m ) 41 printf ("Case %d: Yes\n", ++k); 42 else 43 printf ("Case %d: No\n", ++k); 44 } 45 return 0; 46 } 47 48 void init ()//初始化为一 49 { 50 int i, j; 51 for (i=0; i<n; i++) 52 for (j=0; j<n; j++) 53 map[i][j] = 1; 54 } 55 int find (char str[]) 56 { 57 int i; 58 for (i=0; i<n; i++) 59 if (!strcmp(name[i], str)) 60 return i; 61 } 62 void floyd () 63 { 64 int i, j, k; 65 for (i=0; i<n; i++) 66 for (k=0; k<n; k++) 67 for (j=0; j<n; j++) 68 map[i][j] = max (map[i][j], map[i][k]*map[k][j]); 69 }
标签:
原文地址:http://www.cnblogs.com/alihenaixiao/p/4224664.html