Arbitrage POJ - 2240 最短路径(Bellman-Ford算法变形,正环判断)

  • Arbitrage

题目链接:http://poj.org/problem?id=2240

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

题意:给出一些货币之间的兑换率,问是否可以使某种货币经过一些列兑换之后,使货币值增加。举例说就是1美元经过一些兑换之后,超过1美元。可以输出Yes,否则输出No。

思路:此题与poj1860相似。要找变大环,即求正环,想到将原来的bellman-ford算法稍作修改来实现正环的判断。

本题需要将每个点依次作为起点进行判断(因为题目中未说明起点是那个点,只是要求图中是否存在正环),只要出现正环就输出yes,否则no。本题中未说明起始金钱的具体数目,所以全部定为1。

AC代码:

204ms

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<queue>
#include<vector>
#include<map>
#define ll long long
const int INF = 0x3f3f3f3f;
const int MAXN = 100000 + 5;
using namespace std;
int n, m;
double dist[MAXN];
map<string,int>mapx;
struct edge {
	int st, end;
	double cost;
}e[MAXN];
bool Bellman_Ford(int cnt);
int main() {
	int k = 1;
	while (~scanf("%d", &n)) {
		mapx.clear();
		if (n == 0)
			break;
		for (int i = 1; i <= n; ++i) {
			char str[110];
			scanf("%s",str);
			mapx[str] = i;
		}
		scanf("%d", &m);
		for (int i = 1; i <= m; ++i) {
			double a;
			char s1[110], s2[110];
			scanf("%s %lf %s", s1, &a, s2);
			e[i].st = mapx[s1];
			e[i].end = mapx[s2];
			e[i].cost = a;
		}
		for (int i = 1; i <= n; ++i) {
			if (Bellman_Ford(i)) {
				printf("Case %d: Yes\n", k);
				break;
			}
			else if (i == n)
				printf("Case %d: No\n", k);
		}
		k++;
	}
	return 0;
}

bool Bellman_Ford(int cnt) {
	for (int i = 1; i <= n; ++i) {
		dist[i] = 0;
	}
	dist[cnt] = 1;
	bool flag;
	for (int j = 1; j < n; ++j) {
		flag = false;
		for (int i = 1; i <= m; ++i) {
			int x = e[i].st, y = e[i].end;
			if (dist[y] < dist[x] * e[i].cost) {
				dist[y] = dist[x] * e[i].cost;
				flag = true;
			}
		}
		if (!flag)
			return false;
	}
	for (int i = 1; i <= m; ++i) {
		int x = e[i].st, y = e[i].end;
		if (dist[y] < dist[x] * e[i].cost)
			return true;
	}
	return false;
}

相似题型:poj 1860

                  http://poj.org/problem?id=1860

猜你喜欢

转载自blog.csdn.net/weixin_43821265/article/details/86763151