
5

POJ 2240 - Arbitrage
source link: https://exp-blog.com/algorithm/poj/poj2240-arbitrage/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

POJ 2240 - Arbitrage | 眈眈探求
- POJ 2240 - Arbitrage
- Time: 1000MS
- Memory: 65536K
- 难度: 初级
- 分类: 最短路径算法
求自身到自身的最大转换率。
最简单的方法就是**floryd算法变形**,求最大路径后,求最大环,看它是否满足条件。
每一个节点都必须有到自身的环(不甚清楚原因)。
注意:
- 本题需要建立容器,建立字符串到int的映射(一一对应)关系,把然后字符串作为数组下标,**模拟数组**
- 切记该double的地方一定不能为int
//Memory Time
//276K 79MS
#include <iostream>
#include<map>
#include<string>
using namespace std;
const int inf=10000; //无限大
int n; //货币种类
int m; //兑换方式
map<string,int>STL; //建立一个 使字符串与整数有一一对应关系 的容器STL,以便利用邻接矩阵存储数据
double rate;
char str[50],str1[50],str2[50];
double dist[31][31];
int i,j,k;
void floyd(void)
{
for(k=1;k<=n;k++)
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if(dist[i][j] < dist[i][k] * dist[k][j]) //变形的最大路径,变"+"为"*"
dist[i][j] = dist[i][k] * dist[k][j];
return;
}
int main(void)
{
int cases=1;
while(cases)
{
/*Initial*/
memset(dist,inf,sizeof(dist));
/*Input*/
cin>>n;
if(!n)break;
for(i=1;i<=n;i++)
{
cin>>str;
STL[str]=i; //将输入的货币从1到n依次编号
dist[i][i]=1; //到自身的转换率默认为1,但通过floyd可能会被改变
//有向图的顶点(一般)存在环
}
cin>>m;
for(i=1;i<=m;i++)
{
cin>>str1>>rate>>str2;
dist[STL[str1]][STL[str2]]=rate; //构造图
}
/*Floyd Algorithm*/
floyd();
/*Output*/
int flag=false;
for(i=1;i<=n;i++)
if(dist[i][i]>1)
{
flag=true;
break;
}
if(flag)
cout<<"Case "<<cases++<<": Yes"<<endl;
else
cout<<"Case "<<cases++<<": No"<<endl;
}
return 0;
}
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK