poj 2240(floyd)

http://poj.org/problem?id=2240

题意:有些人会利用货币的不用汇率来进行套现,比如1美元换0.5英镑,而1英镑又可以换10法郎,而1法郎又可以换0.21的美元,那么经过货币的汇率转换后,它就可以获得1.05倍原来的美元。

现在给你N中货币,m种货币的汇率,求是否可以获利。

思路:首先这个是给你一些货币,那么我们可以把货币和数字建立一个映射。然后再用他给的汇率以及数字建立一个邻接矩阵。用一次floyd后对对角线的数字进行判断,如果大于1,那么说明可以获利,不然就不能获利。

这个题我用c++是78ms,用G++是700多ms。

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <map>
 4 #include <string>
 5 #include <iostream>
 6 
 7 using namespace std;
 8 
 9 map< string , int > s;
10 
11 double graph[ 40 ][ 40 ];
12 
13 int main()
14 {
15   //  freopen("in.txt","r",stdin);
16     int n,p = 0;
17     while( cin>>n,n )
18     {
19         p++;
20         memset( graph , 0 , sizeof( graph ) );
21         string a , b;
22         double m;
23         int x,mark = 0;
24         for(int i = 1 ; i <= n ; i++ )
25         {
26             cin>>a;
27             s[ a ] = i;          //对货币和数字建立一个映射。
28         }
29         cin>>x;
30         for( int i = 1 ; i <= x ; i++ )
31         {
32             cin>>a>>m>>b;
33             graph[ s[ a ] ][ s[ b ] ] = m;
34         }
35         for( int k = 1 ; k <= n ; k++ )          //floyd求出两个货币间的最大汇率。
36             for( int i = 1 ; i <= n ; i++ )
37                 for( int j = 1 ; j <= n ; j++ )
38                     if( graph[ i ][ j ] < graph [ i ][ k ]*graph[ k ][ j ])
39                         graph[ i ][ j ] = graph [ i ][ k ]*graph[ k ][ j ];
40         for(int i = 1 ; i <= n ; i++ )    //判断。
41             if( graph[ i ][ i ] > 1 ) mark = 1; 
42         if( mark ) printf("Case %d: Yes\n",p);
43         else printf("Case %d: No\n",p);
44     }
45     return 0;
46 }

 

posted @ 2016-08-03 15:01  一个_小菜鸟  阅读(425)  评论(0编辑  收藏  举报