POJ:Arbitrage (搜索,汇率换算是否赚?)

Posted riotian

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ:Arbitrage (搜索,汇率换算是否赚?)相关的知识,希望对你有一定的参考价值。

POJ 2240 http://poj.org/problem?id=2240
题意:判断是否存在使得汇率增多的环

【任意一个点的汇率增多都可以】

Floyd 简单变形

(w[i][j] = max(w[i][j], w[i][k]*w[k][j]))

#ifndef ONLINE_JUDGE
#pragma warning(disalbe : 4996)
#endif
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<string>
#include<map>
#include<cstring>
using namespace std;
const int maxn = 40;
double d[maxn];
int n, m;

double w[maxn][maxn];
map<string, int>mp;

void floyd() {
	for (int k = 1; k <= n; ++k)
		for (int i = 1; i <= n; ++i)
			for (int j = 1; j <= n; ++j)
				w[i][j] = max(w[i][j], w[i][k] * w[k][j]);
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
#ifndef ONLINE_JUDGE
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
#endif // !ONLINE_JUDGE
	int kcase = 0;
	while (cin >> n && n) {
		mp.clear();

		string s;
		for (int i = 1; i <= n; ++i) {
			cin >> s;
			mp[s] = i;
			w[i][i] = 1;//自己换自己
		}

		cin >> m;
		string s1, s2;
		double rat;
		for (int i = 0; i < m; ++i) {
			cin >> s1 >> rat >> s2;
			w[mp[s1]][mp[s2]] = rat;
		}

		floyd();

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

		printf("Case %d: ", ++kcase);
		if (flag) printf("Yes
");
		else printf("No
");
	}
#ifndef ONLINE_JUDGE
	fclose(stdin);
	fclose(stdout);
	system("out.txt");
#endif // !ONLINE_JUDGE
	return 0;
}

bellman_ford 判断正环

#ifndef ONLINE_JUDGE
#pragma warning(disalbe : 4996)
#endif
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<string>
#include<map>
#include<cstring>
using namespace std;
const int maxn = 40;
double d[maxn];
int n, m;

struct Edge {
	int u, v;
	double r;
}edge[maxn * maxn];
map<string, int>mp;

bool bellman_ford(int s) {
	memset(d, 0, sizeof(d));
	d[s] = 1;
	for(int i = 1;i <=n; ++i)
		for (int j = 0; j < m; ++j) {
			int u = edge[j].u;
			int v = edge[j].v;
			double r = edge[j].r;
			if (d[v] < d[u] * r)
				d[v] = d[u] * r;
		}
	if (d[s] > 1.0)return true;
	return false;
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
#ifndef ONLINE_JUDGE
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
#endif // !ONLINE_JUDGE
	int kcase = 0;
	while (cin >> n && n) {
		mp.clear();

		string s;
		for (int i = 1; i <= n; ++i) {
			cin >> s;
			mp[s] = i;
		}

		cin >> m;
		string s1, s2;
		double rat;
		for (int i = 0; i < m; ++i) {
			cin >> s1 >> rat >> s2;
			edge[i].u = mp[s1];
			edge[i].v = mp[s2];
			edge[i].r = rat;
		}

		bool flag = false;
		for(int i = 1;i <=n; ++i)
			if (bellman_ford(i)) {
				flag = true;
				break;
			}

		printf("Case %d: ", ++kcase);
		if (flag) printf("Yes
");
		else printf("No
");
	}
#ifndef ONLINE_JUDGE
	fclose(stdin);
	fclose(stdout);
	system("out.txt");
#endif // !ONLINE_JUDGE
	return 0;
}

以上是关于POJ:Arbitrage (搜索,汇率换算是否赚?)的主要内容,如果未能解决你的问题,请参考以下文章

POJ 1217 Arbitrage(floyd)

POJ-2240 Arbitrage---判断正环+枚举

POJ - 2240 Arbitrage(Bellman-Ford)

POJ 2240 -- Arbitrage(Bellman-Ford)

[HDOJ1217]Arbitrage(floyd)

HDOJ 1217 Arbitrage (最短路)