hdu 1217 Arbitrage

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1217

题意就是通过货币兑换进行增值。

floyd的运用,以前这题却一直不会。唉....

View Code
 1 #include <iostream>
 2 #include <cstring>
 3 #include <cstdio>
 4 #include <cmath>
 5 using namespace std;
 6 char str[35][100];
 7 double  map[35][35];
 8 int n,m;
 9 int find(char *a)
10 {
11     int i;
12     for(i=1;i<=n;i++)
13         if(strcmp(str[i],a)==0)return i;
14 }
15 void init()
16 {
17     int i,j;
18     for(i=1;i<=30;i++)
19     {
20         for(j=1;j<=30;j++)
21             map[i][j]=0.0;
22         map[i][i]=1;
23     }
24 }
25 int main()
26 {
27     int i,j,s,t,k,flag,w=0;
28     char name[100];
29     char name1[100];
30     double x;
31     while(scanf("%d",&n),n)
32     {
33         init();
34         flag=0;
35         w++;
36         for(i=1;i<=n;i++)
37             scanf("%s",str[i]);
38         scanf("%d",&m);
39         for(i=1;i<=m;i++)
40         {
41             scanf("%s%lf%s",name,&x,name1);
42             s=find(name);
43             t=find(name1);
44             map[s][t]=x;
45         }
46         for(k=1;k<=n;k++)
47             for(i=1;i<=n;i++)
48                 for(j=1;j<=n;j++)
49                 {
50                     if(map[i][j]<map[i][k]*map[k][j])
51                         map[i][j]=map[i][k]*map[k][j];
52                     
53                 }
54                 for(i=1;i<=n;i++)
55                 if(map[i][i]-1.0>0.00000001)flag=1;
56                 if(flag)printf("Case %d: Yes\n",w);
57                 else printf("Case %d: No\n",w);
58     }
59     return 0;
60 }
posted @ 2012-04-12 11:44  我们一直在努力  阅读(173)  评论(0编辑  收藏  举报