Arbitrage(最短路-floyd算法变形求正权)

Arbitrage

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 16127   Accepted: 6780

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

题意:题目首先给出N中货币,然后给出了这N种货币之间的兑换的兑换率。如 USDollar 0.5 BritishPound 表示 :1 USDollar兑换成0.5 BritishPound。问在这N种货币中是否有货币可以经过若干次兑换后,兑换成原来的货币可以使货币量增加。
思路:和poj上另一道最短路的题Currency Exchange差不多。都是求正环。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#include <map>
using namespace std;
int n,m;
double dis[50][50],rate;
map<string,int>name;//申请图的内存空间,string代表字符串,int代表数值;就是给那些钱币标号。
void floyd()
{
	int i,j,k;
	for(k=1;k<=n;k++)
	for(i=1;i<=n;i++)
		for(j=1;j<=n;j++)
        dis[i][j]=max(dis[i][j],dis[i][k]*dis[k][j]);//变形在这里
}
int main()
{
	char str[110],str1[110],str2[110];
	int cnt=1;
	int i,j;
	while(~scanf("%d",&n))
	{
	    if(n==0)
            break;
		for(i=1;i<=n;i++)
		{
			scanf("%s",str);
            name[str]=i;//标号
		}
	for(i=1;i<=n;i++)
	{
		for(j=1;j<=n;j++)
			if(i==j)
                dis[i][j]=1;
            else
                dis[i][j]=0;
	}
		scanf("%d",&m);
		for(i=0;i<m;i++)
		{
			scanf("%s%lf%s",str1,&rate,str2);
			dis[name[str1]][name[str2]]=rate;
		}
		floyd();
		int flag=0;
		for(i=1;i<=n;i++)
			if(dis[i][i]>1)//如果dis[i][i]>1证明存在正权回路
			{
				flag=1;
				break;
			}
		printf("Case %d: ",cnt++);
		if(flag)
			printf("Yes\n");
		else
			printf("No\n");
	}
}



时间: 2025-02-01 19:52:47

Arbitrage(最短路-floyd算法变形求正权)的相关文章

POJ2240——Arbitrage(Floyd算法变形)

Arbitrage DescriptionArbitrage 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

[ACM] hdu 1217 Arbitrage (bellman_ford最短路,判断是否有正权回路或Floyed)

Arbitrage Problem 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 p

Uvaoj 10048 - Audiophobia(Floyd算法变形)

1 /* 2 题目大意: 3 从一个点到达另一个点有多条路径,求这多条路经中最大噪音值的最小值! . 4 5 思路:最多有100个点,然后又是多次查询,想都不用想,Floyd算法走起! 6 */ 7 #include<iostream> 8 #include<cstring> 9 #include<cstdio> 10 #define INF 0x3f3f3f3f 11 using namespace std; 12 13 int map[105][105]; 14 1

最短路--floyd算法模板

floyd算法是求所有点之间的最短路的,复杂度O(n3)代码简单是最大特色 1 #include<stdio.h> 2 #include<string.h> 3 4 const int maxn=105; 5 const int INF=0x3f3f3f3f; 6 int ma[maxn][maxn],n; 7 8 inline int min(int a,int b){return a<b?a:b;} 9 inline int max(int a,int b){return

[ACM] hdu 1217 Arbitrage (bellman_ford最短路,推断是否有正权回路或Floyed)

Arbitrage Problem 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 p

HDU 2066 最短路floyd算法+优化

http://acm.hdu.edu.cn/showproblem.php?pid=206 题意 从任意一个邻居家出发 到达任意一个终点的 最小距离 解析 求多源最短路 我想到的是Floyd算法 但是题目给出的n的大小不确定 所以图很稀疏 会有很多孤立点 会多跑很多没用的路径 需要优化一下 不然会超时 我看其他人很多都是用迪杰斯特拉写的,可以试试 AC代码 1 #include <stdio.h> 2 #include <math.h> 3 #include <string.

多源最短路Floyd 算法————matlab实现

弗洛伊德(Floyd)算法是一种用于寻找给定的加权图中顶点间最短路径的算法.该算法名称以创始人之一.1978年图灵奖获得者.斯坦福大学计算机科学系教授罗伯特·弗洛伊德命名. 基本思想 通过Floyd计算图G=(V,E)中各个顶点的最短路径时,需要引入一个矩阵S,矩阵S中的元素a[i][j]表示顶点i(第i个顶点)到顶点j(第j个顶点)的距离. 假设图G中顶点个数为N,则需要对矩阵S进行N次更新.初始时,矩阵S中顶点a[i][j]的距离为顶点i到顶点j的权值:如果i和j不相邻,则a[i][j]=∞

POJ1860 Currency Exchange【BellmanFord算法】【求正权回路】

Currency Exchange Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 20994 Accepted: 7522 Description Several currency exchange points are working in our city. Let us suppose that each point specializes in two particular currencies and perfor

多元最短路-floyd算法

要获得带权图中任意两点之间的最短距离,可以通过多次调用求解单源最短路径的算法来实现.但floyd算法来实现会更简单. 算法步骤: 假设图由邻接矩阵存放,通过二维数组dis[maxN][maxN]给出一张有向或者无向图,dis[i][j]代表i到j的距离,没有直接连通的路径的话就初始化为无限大,用代指无限大的极大数INF代替,但这里需要防止出现溢出的情况,否则在程序执行后将出现结果错误. 然后进行relax操作,任意三个节点i,j,k,若dis[i][k]+dis[k][j]<dis[i][j],