poj 2240 Arbitrage

本文介绍了一种通过货币兑换率寻找套利机会的算法实现。利用SPFA和Floyd算法判断是否存在货币兑换循环使得最终获得的金额大于初始金额,从而实现盈利。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Arbitrage
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 22330 Accepted: 9479

Description

Arbitrage is the use of discrepancies in currency exchange rates to transform one unit of a currency into more than one unit of the same currency. For example, suppose that 1 US Dollar buys 0.5 British pound, 1 British pound buys 10.0 French francs, and 1 French franc buys 0.21 US dollar. Then, by converting currencies, a clever trader can start with 1 US dollar and buy 0.5 * 10.0 * 0.21 = 1.05 US dollars, making a profit of 5 percent. 

Your job is to write a program that takes a list of currency exchange rates as input and then determines whether arbitrage is possible or not. 

Input

The input will contain one or more test cases. Om the first line of each test case there is an integer n (1<=n<=30), representing the number of different currencies. The next n lines each contain the name of one currency. Within a name no spaces will appear. The next line contains one integer m, representing the length of the table to follow. The last m lines each contain the name ci of a source currency, a real number rij which represents the exchange rate from ci to cj and a name cj of the destination currency. Exchanges which do not appear in the table are impossible. 
Test cases are separated from each other by a blank line. Input is terminated by a value of zero (0) for n.

Output

For each test case, print one line telling whether arbitrage is possible or not in the format "Case case: Yes" respectively "Case case: No".

Sample Input

3
USDollar
BritishPound
FrenchFranc
3
USDollar 0.5 BritishPound
BritishPound 10.0 FrenchFranc
FrenchFranc 0.21 USDollar

3
USDollar
BritishPound
FrenchFranc
6
USDollar 0.5 BritishPound
USDollar 4.9 FrenchFranc
BritishPound 10.0 FrenchFranc
BritishPound 1.99 USDollar
FrenchFranc 0.09 BritishPound
FrenchFranc 0.19 USDollar

0

Sample Output

Case 1: Yes
Case 2: No

123

题意大体意思 是先输入 几种 金钱 

然后再输入 任意两种的金钱的 兑换比例

然后问  能不能 从自己开始往外兑换金钱 然后 兑换会自己  是 是否比例大于1.0  即 金钱增加了 如果大于1.0 则yes

否则no


可以用SPFA 也可以用 floyd  


spfa

/*
	SPFA 最短路径 
*/

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<queue>

#define N 100
using namespace std;

double map[N][N];
double dist[N];
bool vis[N];
struct node{
	char name[100];
}A[100];
int n,m;
int find(char *s)
{
    for(int i = 0; i < n; i++)
        if(strcmp(A[i].name, s) == 0)
            return i;
}
bool SPFA(int start)
{
	memset(vis,0,sizeof(vis));
	queue<int>Q;
	int i,j,u;
	for(i=0;i<n;i++)
	{
		dist[i]=0;
	}
	while(!Q.empty())
		Q.pop();
	dist[start]=1.0;
	vis[start]=true;//设置被访问过 
	Q.push(start);//压入队列 
	while(!Q.empty())
	{
		u=Q.front();//取出对头 
		Q.pop();//释放 
		vis[u]=false;//设置可以访问 
		for(i=0;i<n;i++)
		{
			if(dist[u]*map[u][i]>dist[i])//更新 
			{
				dist[i]=dist[u]*map[u][i];
//				printf("%lf %lf\n",dist[i],map[u][i]);
					if(dist[start]>1.0)//比例大于1.0
						return true;
					if(!vis[i])
					{
						vis[i]=true;
						Q.push(i);
					}
			}
		}
	}
	return false; 
		
}
int main()
{
	int i,j,x,y;
	char str1[100],str2[100];
	double bit;
	int cout=0;
	while(~scanf("%d",&n),n)
	{
		cout++;
		for(i=0;i<n;i++)//初始化 
		{
			for(j=0;j<n;j++)
			{
				if(i==j)
					map[i][j]=1.0;
				else
					map[i][j]=0;
			}
				
		}
		for(i=0;i<n;i++)
			scanf("%s",A[i].name);
		scanf("%d",&m);
		for(i=0;i<m;i++)
		{
			scanf("%s%lf%s",str1,&bit,str2);
			x=find(str1);y=find(str2);//将对应的 转换成 map 中位置 
			map[x][y]=bit;
		}
//		for(i=0;i<10;i++)
//		{
//				for(j=0;j<10;j++)
//				printf("%lf ",map[i][j]);
//			printf("\n");
//		}
		
		int ans=0;
		for(i=0;i<n;i++)
		{
			if(SPFA(i)==true)
			{
				ans=1;
				break;
			}
		}
        printf("Case %d: ",cout);
        printf("%s\n", ans ? "Yes" : "No");
	}
	return 0;
}

Floyd 

/*
	floyd 
	美元兑换 poj 2240	 
*/

#include<stdio.h>
#include<string.h>


struct name{
	char A[100];
}str[100];

int n,T;
double map[100][100];

void M_emest(int T)
{
	int i,j;
	for(i=1;i<=T;i++)
		for(j=1;j<=T;j++)
			if(i==j) map[i][j]=1;
			else map[i][j]=0;
}
int Floyd()
{
	int i,j,k;
	for(k=1;k<=T;k++)
	{
		for(i=1;i<=T;i++)
		{
			for(j=1;j<=T;j++)
			{
				if(map[i][j]<map[i][k]*map[k][j])
//					printf("%lf %lf\n",map[i][k],map[k][j]);
					map[i][j]=map[i][k]*map[k][j];
//					printf("map[%d][%d]=%lf\n",i,j,map[i][j]);
			}
		}
	}
	for(i=1;i<=T;i++)
		if(map[i][i]>1.0)
			return 1;	
	return 0;
}
int main()
{
	int i,j;
	char a[20],str1[20],str2[20];
	double ss;
	int c=0;
	while(~scanf("%d",&T),T)
	{
		M_emest(T);
		for(i=1;i<=T;i++)
			scanf("%s",str[i].A);
			
		scanf("%d",&n);
		
		for(i=1;i<=n;i++)
		{
			scanf("%s %lf %s",str1,&ss,str2);
			int x,y;
			for(j=1;j<=T;j++)//查找位置 
			{
				if(strcmp(str[j].A,str1)==0)
				{
					x=j;
				}
				if(strcmp(str[j].A,str2)==0)
				{
					y=j;
				}
			}
			map[x][y]=ss;
		}
		printf("Case %d: ",++c);
		printf(Floyd()?"Yes\n":"No\n");
	}
	return 0;
}



转载于:https://www.cnblogs.com/sizaif/p/9078587.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值