POJ2240 Arbitrage(AC)

Arbitrage
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 18726 Accepted: 7910

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
 
#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>

//求负回路的问题
//正向是正值,反向就是负值,然后牵扯到字符串变换和浮点数的保存
//用bellman-ford算法
//自己做着做着就不对了,怎么取到的值都是负值了,不知道是不是哪边理解的不对,自己犯了一个错误
//这道题严格意义上不算自己做的,参考了很多,对bellman-ford算法还是不是特别了解的
//POJAC

#define MAXN 40
#define MAXPATH 10000
int N = 0;
int num = 0;

typedef struct exchange
{
	int start;
	int end;
	float change;
}exchanges;

char country[MAXN][MAXN];  //country的字符设为最大为40个
exchanges ex[MAXN*MAXN];
float  dis[MAXN]; //题中说最多不会超过30个国家货币

int bellmanford()
{
	int i = 0;
	int j = 0;

	int flag = 0;
	dis[1] = 1; //源点到源点就是1,1元还是1元
	//有多少个国家
	for (i = 1; i <= N;i++)
	{
		//有都多少个汇率
		for (j = 0; j < num;j++)
		{
			if (dis[ex[j].end] < dis[ex[j].start] * ex[j].change) //汇率是要乘的不是加的,否则就不对了,还有是要找汇率大的,因此不能是大于号,要是小于号,这个自己也是看了很久,看了别人的例子才知道的,自己想的不对
			{
				dis[ex[j].end] = dis[ex[j].start] * ex[j].change;
				flag = 1;
			}
		}
		if (0 == flag)
		{
			break;
		}
	}

	//查找是否有负环
	for (j = 0; j < num; j++)
	{
		if (dis[ex[j].end] < dis[ex[j].start] * ex[j].change)
		{
			return 1;
		}
	}

	return 0;
}

//寻找字符串
int search(char s[],int size)
{
	int i    = 0;
	int j    = 0;
	int flag = 0;
	for (i = 1; i <= N;i++)
	{
		flag = 0;
		for (j = 0; j < size; j++)
		{
			if (country[i][j] != s[j])
			{
				flag = 1;
				break;
			}
		}
		if (0 == flag)
		{
			return i;
		}
	}
	return 0;
}

int strlen(char* s)
{
	int size = 0;
	int i = 0;
	while (*s != '\0')
	{
		s++;
		size += 1;
	}
	return size;
}

void init()
{
	int i = 0;
	int j = 0;
	for (i = 0; i < MAXN;i++)
	{
		for (j = 0; j < MAXN; j++)
		{
			country[i][j] = '\0';
		}
		dis[i]   = 0;
	}

	for (i = 0; i < MAXN*MAXN; i++)
	{
		ex[i].start  = 0;
		ex[i].end    = 0;
		ex[i].change = 0.0;
	}
	return;
}


int main()
{
	int i = 0;
	int path = 0;
	freopen("input.txt","r",stdin);
	char S[MAXN];
	char E[MAXN];
	float D = 0.0;
	int test = 1;
	int start = 0;
	int end = 0;
	
	while (1 == scanf("%d",&N) && (0 != N))
	{
		init();
		num = 0;
		for (i = 1; i <= N;i++)
		{
			scanf("%s", &country[i]);
		}
		scanf("%d",&path); //一共有path种换法
		for (i = 1; i <= path; i++)
		{
			scanf("%s %f %s", &S,&D,&E);
			start = search(S, strlen(S));
			end = search(E, strlen(E));;
			ex[num].start    = start;
			ex[num].end      = end;
			ex[num++].change = D;

			//ex[num].start    = end;
			//ex[num].end      = start;
			//ex[num++].change = -(1/D); //写这个是不对的,因为US->French有自己的汇率,French->US也有自己的汇率,这个不能混为一谈,自己原来这样设是不对的
		}
		if (1 == bellmanford())
		{
			printf("Case %d: Yes\n",test);
		}
		else
		{
			printf("Case %d: No\n", test);
		}
		test += 1;
	}
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值