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

POJ 1860 Currency Exchange

时间:2015-01-26 08:46:46      阅读:238      评论:0      收藏:0      [点我收藏+]

标签:

 

 

/*
*POJ 1860 Currency Exchange
*判断回路是否能使初始值增长
*/

#include <cstdio>
#include <cstring>
#include <queue>

using namespace std;

#define INF 0x3f3f3f3f
#define MAXN 600

struct ArcNode {
	int to;
	double r;
	double c;
	ArcNode *next;
};
queue<int> q;
int N, M, S;
double V;
ArcNode *list[MAXN]; //每个顶点的边链表表头指针
bool visit[MAXN];  //每个顶点是否在队列
int count[MAXN]; //每个顶点入队列的次数
double dist[MAXN];

bool spfa(int src)
{
	int i, u, v;
	ArcNode *temp;
	for (i = 1 ; i <= N; i++) {
		dist[i] = 0;
	}
	dist[src] = V;
	q.push(src);
	while ( !q.empty()) {
		if(dist[src] > V)	return true;
		u = q.front();
		q.pop();
		temp = list[u];
		while (temp != NULL) {
			v = temp->to;
			if (dist[v] < (dist[u] - temp->c) * temp->r) {
				dist[v] = (dist[u] - temp->c) * temp->r;
				if (!visit[v]) {
					q.push(v);  visit[v] = 1;
				}
				count[v]++;
				if (count[v] > N) //存在回路
					return true;
			}
			temp = temp->next;
		}
		visit[u] = 0;
	}
	return false;
}

int main()
{
	int i, T;
	int u, v;
	double r, c;

	while (~scanf("%d %d %d %lf", &N, &M, &S, &V)) {
		memset(list, 0, sizeof(list));
		memset(visit, 0, sizeof(visit));
		memset(count, 0, sizeof(count));
		while (!q.empty()) {
			q.pop();
		}
		ArcNode *temp;
		for (i = 0; i < M; i++) {
			scanf("%d %d %lf %lf", &u, &v, &r, &c);
			temp = new ArcNode;
			temp->to = v;   temp->r = r;    temp->c = c;    temp->next = NULL;
			if (list[u] == NULL) {
				list[u] = temp;
			} else {
				temp->next = list[u];   list[u] = temp;
			}
			scanf("%lf %lf", &r, &c);
			temp = new ArcNode;
			temp->to = u;   temp->r = r;    temp->c = c;    temp->next = NULL;
			if (list[v] == NULL) {
				list[v] = temp;
			} else {
				temp->next = list[v];   list[v] = temp;
			}
		}
		if (spfa(S)) {
			printf("YES\n");
		} else {
			printf("NO\n");
		}
		for (i = 1; i <= N; i++) {
			temp = list[i];
			while (temp != NULL) {
				list[i] = temp->next;   delete temp;    temp = list[i];
			}
		}
	}
	return 0;
}

  

POJ 1860 Currency Exchange

标签:

原文地址:http://www.cnblogs.com/subrshk/p/4249346.html

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