uva 436(floyd变形)

题意:有若干种货币他们之间存在汇率,然后你要判断的是你能不能通过交换货币是你手中的一种货币不断的升值。

思路:floyd变形Map[i][j] = max(Map[i][j], Map[i][k]*Map[k][j]) 状态表示i交换到j最多能换多少。然后最后统计一下所有自己欢自己是不是有大于1就可以了。

代码如下:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 #include <cmath>
 5 #include <cstring>
 6 #include <algorithm>
 7 #include <queue>
 8 #include <stack>
 9 #include <vector>
10 #include <set>
11 #include <map>
12 #define MP(a, b) make_pair(a, b)
13 #define PB(a) push_back(a)
14 
15 using namespace std;
16 
17 typedef long long ll;
18 typedef pair<int ,int> pii;
19 typedef pair<unsigned int, unsigned int> puu;
20 typedef pair<int ,double> pid;
21 typedef pair<ll, int> pli;
22 typedef pair<int, ll> pil;
23 
24 const int INF = 0x3f3f3f3f;
25 const double eps = 1e-6;
26 const int LEN = 110;
27 double Map[LEN][LEN];
28 int n, m;
29 string name[LEN];
30 map<string, int> mp;
31 
32 void floyd()
33 {
34     for(int k=0; k<n; k++){
35         for(int i=0; i<n; i++){
36             for(int j=0; j<n; j++){
37                 if(Map[i][j] < Map[i][k]*Map[k][j])
38                     Map[i][j] = Map[i][k] * Map[k][j];
39             }
40         }
41     }
42 }
43 
44 int main()
45 {
46 //    freopen("in.txt", "r", stdin);
47 
48     int kase = 1;
49     while(scanf("%d", &n)!=EOF && n){
50         for(int i=0; i<n; i++){
51             cin >> name[i];
52             mp[name[i]] = i;
53         }
54         scanf("%d", &m);
55         memset(Map, 0, sizeof Map);
56         double val;string a, b;
57         for(int i=0; i<m; i++){
58             cin >> a >> val >> b;
59             Map[mp[a]][mp[b]] = val;
60         }
61         floyd();
62         int ans = 0;
63         for(int i=0; i<n; i++){
64             if(Map[i][i]>1) ans = 1;
65         }
66         printf("Case %d: ", kase++);
67         if(ans)printf("Yes\n");
68         else printf("No\n");
69     }
70     return 0;
71 }
View Code

 

转载于:https://www.cnblogs.com/shu-xiaohao/p/3534000.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值