码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2020-05-19 18:09:56      阅读:55      评论:0      收藏:0      [点我收藏+]

标签:return   ons   algorithm   man   clu   ios   out   false   else   

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\n");
		else printf("No\n");
	}
#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\n");
		else printf("No\n");
	}
#ifndef ONLINE_JUDGE
	fclose(stdin);
	fclose(stdout);
	system("out.txt");
#endif // !ONLINE_JUDGE
	return 0;
}

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

标签:return   ons   algorithm   man   clu   ios   out   false   else   

原文地址:https://www.cnblogs.com/RioTian/p/12918264.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!