ZOJ1092 POJ2240 Arbitrage,Floyd算法

105 篇文章 0 订阅
13 篇文章 0 订阅

原本想用Dijkstra算法的,但是死活AC不了,所以就用Floyd算法了ToT;虽然Dijkstra算法和Floyd算法时间复杂度一样,但是其应用环境有很大不同。


/*******************************************************************************
 # Author : Neo Fung
 # Email : neosfung@gmail.com
 # Last modified: 2011-08-15 19:07
 # Filename: ZOJ1092 POJ2240 Arbitrage.cpp
 # Description : 
 ******************************************************************************/
// ZOJ1092 POJ2240 Arbitrage.cpp : Defines the entry point for the console application.
//

// #include "stdafx.h"
#include <iostream>
#include <string>
#include <map>
#include <memory.h>

using namespace std;

#define INFINITE 0.0

int main(void)
{
	int n,m;
	double len;
	string str1,str2;
	map< string,int > mymap;
	double currency[35][35];
	int ncases(1);
	
	while(cin>>n &&n)
	{
		mymap.clear();
		for(int i=0;i<n;++i)
		{
			cin>>str1;
			mymap.insert( make_pair(str1,i));

			for(int j=0;j<n;++j)
				currency[i][j]=INFINITE;

			currency[i][i]=1;
		}

		cin>>m;
		for(int i=0;i<m;++i)
		{
			cin>>str1>>len>>str2;
			currency[mymap[str1]][mymap[str2]]=len;
		}

		for(int i=0;i<n;++i)
			for(int j=0;j<n;++j)
				for(int k=0;k<n;++k)
					if (currency[j][k]< currency[j][i]*currency[i][k])
					{
						currency[j][k] = currency[j][i]*currency[i][k];
					}

		bool flag(false);
		for (int i=0;i<n;++i)
		{
			if (currency[i][i]>1.0)
			{
				flag=1;
				break;
			}
		}

		cout<<"Case "<<ncases++<<": ";
		if (flag)
		{
			cout<<"Yes"<<endl;
		}
		else
			cout<<"No"<<endl;

	}
	return 0;
}





评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值