HDU 1217 Arbitrage

Problem Description

Arbitrage is the use ofdiscrepancies in currency exchange rates to transform one unit of a currencyinto more than one unit of the same currency. For example, suppose that 1 USDollar buys 0.5 British pound, 1 British pound buys 10.0 French francs, and 1French franc buys 0.21 US dollar. Then, by converting currencies, a clevertrader 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 asinput and then determines whether arbitrage is possible or not.

 

Input

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

 

Output

For each test case, print oneline telling whether arbitrage is possible or not in the format "Casecase: 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

 

 

题目简介:给n种钱,然后输入n种货币名称。给m种兑换方式。然后输入m行两种货币之间的汇率。

方法:最短路。

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
	int n, m, i, j, k, r, s, t, count = 1, flag = 0;
	double  d[31][31] = {0}, c;
	char a[31][100] = {NULL}, p[100] = {NULL}, q[100] = {NULL};
	while(scanf("%d",&n)!=EOF,n)/*货币总数*/
	{
		memset(d, 0, sizeof(d));/*初始化货币之间汇率*/
		for(i = 1;i<=n;i++)
		{
			scanf("%s",&a[i]);
		}/*记录货币名称*/



		scanf("%d",&m);/*货币兑换总数*/
		for(i = 1;i<=m;i++)
		{
			scanf("%s",&p);/*货币1*/
			t = strlen(p);
			for(j = 1;j<=n;j++)
			{
				if(strcmp(a[j], p)==0)
				{
					r = j;
					break;
				}
			}/*将货币名装换成数字*/


			for(j = 0;j<t;j++)
			{
				p[j] = NULL;
			}/*初始化p,方便下次使用*/


			scanf("%lf",&c);/*货币1对货币2的汇率*/
			scanf("%s",&q);/*货币2*/
			t = strlen(q);
			for(j = 1;j<=n;j++)
			{
				if(strcmp(a[j], q)==0)
				{
					s = j;
					break;
				}
			}/*将货币名装换成数字*/
			for(j = 0;j<t;j++)
			{
				q[j] = NULL;
			}/*初始化q,方便下次使用*/


			d[r][s] = c;/*记录下货币1对货币2的汇率*/
		}
		/*floyd*/
		for(k = 1;k<=n;k++)
		{
			for(i = 1;i<=n;i++)
			{
				for(j = 1;j<=n;j++)
				{
					if(d[i][j]<d[i][k] * d[k][j])
					{
						d[i][j] = d[i][k] * d[k][j];
					}
				}
			}
		}

		/*判断*/
		flag = 0;
		for(i = 1;i<=n;i++)
		{
			if(d[i][i]>1)/*判断是否有某种情况出现套利*/
			{
				flag = 1;
				break;
			}
		}


		printf("Case %d: ",count++);
		if(flag==0)
			printf("No\n");
		else
			printf("Yes\n");
		getchar();//处理每组输入完后的那个空行
	}
	system("pause");
	return 0;
}


你可能感兴趣的:(HDU 1217 Arbitrage)