POJ2240 - Arbitrage

又是各种钱币换来换去,问能不能赚钱。

Floyd算法判环。

//Memory Time
//228K	32MS

#include<iostream>
#include<stdio.h>
using namespace std;

char  dollarName[30][30];
float edge[30][30];

int outMatch(char src[30], char dst[][30], int dollarType);
bool match(char src[], char dst[], int length);
void floyd(float edge[][30], int dollarType);

int main()
{
	int dollarType;
	int caseNum = 0;
	while(cin >> dollarType && dollarType != 0)
	{
		caseNum++;
		for(int i = 0; i < dollarType; i++)
		{
			for(int j = 0; j < dollarType; j++)
				edge[i][j] = 0;
			edge[i][i] = 1;
		}
		
		for(int i = 0; i < dollarType ; i++)
		{
			scanf("%s", dollarName[i]);
		}
		int M;
		scanf("%d", &M);
		for(int i = 0; i < M; i++)
		{
			char tempName[30];
			scanf("%s",tempName);
			int src = outMatch(tempName, dollarName, dollarType);
			float cost;	scanf("%f", &cost);
			scanf("%s",tempName);
			int dst = outMatch(tempName, dollarName, dollarType);
			edge[src][dst] = cost;
		}
		
		floyd(edge,dollarType);
	
		bool flag = 0;
		for(int i = 0; i < dollarType; i++)
		{
			if(edge[i][i] > 1)
			{
				flag = 1;
				break;
			}
		}
		cout << "Case " << caseNum << ": ";
		if(flag)
			cout << "Yes\n";
		else
			cout << "No\n"; 
	}

	return 0;
}

int outMatch(char src[30], char dst[][30], int dollarType)
{
	int tempSrc;
	for(tempSrc = 0; tempSrc < dollarType; tempSrc++)
	{
		int temp1 = strlen(src);
		int temp2 = strlen(dst[tempSrc]);
		if(temp1 == temp2 && match(src, dst[tempSrc], temp1))
		{
			return tempSrc;
		}
	}
	return tempSrc;
}

bool match(char src[], char dst[], int length)
{
	bool flag = 1;
	for(int j = 0; j < length; j++)
	{
		if(src[j] != dst[j])
		{
			flag = 0;		
			return flag;		
		}	
	}
	return flag;
}

void floyd(float edge[][30], int dollarType)
{
	for(int i = 0; i < dollarType; i++)
		for(int j = 0; j < dollarType; j++)
			for(int k = 0; k < dollarType; k++)
			{
				if(edge[j][i]*edge[i][k] > edge[j][k])
					edge[j][k] = edge[j][i]*edge[i][k];
			}
	return;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值