POJ2240Arbitrage

转载请注明出处:優YoU http://user.qzone.qq.com/289065406/blog/1299339309

提示:本题需要建立容器,建立字符串到int的映射(一一对应)关系,把然后字符串作为数组下标,模拟数组

 

题意:求自身到自身的最大转换率。
  
最简单的方法就是floryd算法变形,求最大路径后,求最大环,看它是否满足条件。
  
每一个结点都必须有到自身的环(不甚清楚原因)
注意:该double的地方一定不能为int,切记
!!!

 1 //Memory Time 
2 //276K 79MS
3
4 #include <iostream>
5 #include<map>
6 #include<string>
7 using namespace std;
8
9 const int inf=10000; //无限大
10 int n; //货币种类
11 int m; //兑换方式
12
13 map<string,int>STL; //建立一个 使字符串与整数有一一对应关系 的容器STL,以便利用邻接矩阵存储数据
14
15 double rate;
16 char str[50],str1[50],str2[50];
17 double dist[31][31];
18
19 int i,j,k;
20
21 void floyd(void)
22 {
23 for(k=1;k<=n;k++)
24 for(i=1;i<=n;i++)
25 for(j=1;j<=n;j++)
26 if(dist[i][j] < dist[i][k] * dist[k][j]) //变形的最大路径,变"+"为"*"
27 dist[i][j] = dist[i][k] * dist[k][j];
28 return;
29 }
30
31 int main(void)
32 {
33 int cases=1;
34 while(cases)
35 {
36 /*Initial*/
37
38 memset(dist,inf,sizeof(dist));
39
40 /*Input*/
41
42 cin>>n;
43 if(!n)break;
44
45 for(i=1;i<=n;i++)
46 {
47 cin>>str;
48 STL[str]=i; //将输入的货币从1到n依次编号
49 dist[i][i]=1; //到自身的转换率默认为1,但通过floyd可能会被改变
50 //有向图的顶点(一般)存在环
51 }
52
53 cin>>m;
54 for(i=1;i<=m;i++)
55 {
56 cin>>str1>>rate>>str2;
57 dist[STL[str1]][STL[str2]]=rate; //构造图
58 }
59
60 /*Floyd Algorithm*/
61
62 floyd();
63
64 /*Output*/
65
66 int flag=false;
67 for(i=1;i<=n;i++)
68 if(dist[i][i]>1)
69 {
70 flag=true;
71 break;
72 }
73 if(flag)
74 cout<<"Case "<<cases++<<": Yes"<<endl;
75 else
76 cout<<"Case "<<cases++<<": No"<<endl;
77 }
78 return 0;
79 }

原文地址:https://www.cnblogs.com/lyy289065406/p/2121686.html