ACM PKU 2240 Arbitrage

题目大意:http://poj.org/problem?id=2240

一个人想利用兑换关系赚钱,给你几种兑换关系,要你求出这个人最开始有一美元,通过一连串的兑换看看最终是不是能赚到钱;

问题转化为:

先构图,单向图;然后求多源最短路径,只要G[i][i]>1.0的话就是赚钱了,意思就是给你一块钱,走一圈之后的钱比1块钱多就是赚钱了;

找一个回路的最大值;

这里Floyd求的肯定是最大值了,还有要注意的是double G[][],rate浮点型的;

 1 #include <iostream>
2 #include <stdio.h>
3 #include <string>
4 #include <string.h>
5 #include <map>
6 const int MAXN=30;
7 using namespace std;
8
9 map<string,int>mymap;
10
11 double G[MAXN+10][MAXN+10];
12 int N,M;
13 bool mark;
14
15 int main()
16 {
17 int i,j,k,t=0;
18 while (cin>>N&&N!=0)
19 {
20 memset(G,0,sizeof(G));
21 mymap.clear();
22 string str;
23 for(i=0; i<N; i++)
24 {
25 cin>>str;
26 mymap[str]=i;
27 G[i][i]=1.0;
28 }
29 cin>>M;
30 string str1,str2;
31 double rate;
32 for(i=0; i<M; i++)
33 {
34 cin>>str1>>rate>>str2;
35 G[mymap[str1]][mymap[str2]]=rate;
36 }
37 for(k=0; k<N; k++) //Floyd多源最长路径;
38 for(i=0; i<N; i++)
39 for(j=0; j<N; j++)
40 {
41 if(G[i][j]<G[i][k]*G[k][j])
42 G[i][j]= G[i][k]*G[k][j];
43 }
44 mark=false;
45 for(i=0; i<N; i++)
46 {
47 if(G[i][i]>1.0)
48 {
49 mark=true;
50 break;
51 }
52 }
53 if(mark)cout<<"Case "<<++t<<": Yes"<<endl;
54 else cout<<"Case "<<++t<<": No"<<endl;
55 }
56 return 0;
57 }

  

转载于:https://www.cnblogs.com/Chinese-Coder-Clarence/articles/2139663.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值