Layout POJ - 3169 (差分约束+最短路)

本文探讨了在牛群排队等待喂食时,如何通过算法解决牛之间的距离约束问题,确保朋友间的最大接近距离和敌对牛之间的最小分离距离。采用差分约束系统将不等式转化为有向图,并运用Bellman算法求解最短路径,从而得出牛群中首尾牛的最大可能距离。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Like everyone else, cows like to stand close to their friends when queuing for feed. FJ has N (2 <= N <= 1,000) cows numbered 1..N standing along a straight line waiting for feed. The cows are standing in the same order as they are numbered, and since they can be rather pushy, it is possible that two or more cows can line up at exactly the same location (that is, if we think of each cow as being located at some coordinate on a number line, then it is possible for two or more cows to share the same coordinate). 

Some cows like each other and want to be within a certain distance of each other in line. Some really dislike each other and want to be separated by at least a certain distance. A list of ML (1 <= ML <= 10,000) constraints describes which cows like each other and the maximum distance by which they may be separated; a subsequent list of MD constraints (1 <= MD <= 10,000) tells which cows dislike each other and the minimum distance by which they must be separated. 

Your job is to compute, if possible, the maximum possible distance between cow 1 and cow N that satisfies the distance constraints.

Input

Line 1: Three space-separated integers: N, ML, and MD. 

Lines 2..ML+1: Each line contains three space-separated positive integers: A, B, and D, with 1 <= A < B <= N. Cows A and B must be at most D (1 <= D <= 1,000,000) apart. 

Lines ML+2..ML+MD+1: Each line contains three space-separated positive integers: A, B, and D, with 1 <= A < B <= N. Cows A and B must be at least D (1 <= D <= 1,000,000) apart.

Output

Line 1: A single integer. If no line-up is possible, output -1. If cows 1 and N can be arbitrarily far apart, output -2. Otherwise output the greatest possible distance between cows 1 and N.

Sample Input

4 2 1
1 3 10
2 4 20
2 3 3

Sample Output

27

解题思路:

利用差分约束系统把不等式化成有向图,利用Bellman算法求解最短路就可以得到最大长度

关于差分约束算法详见:https://blog.youkuaiyun.com/whereisherofrom/article/details/78922648

上面得测试用例可以把不等式列为

X3-X1 <= 10

X4-X2 <= 20

X2-X3 <= -3

注意判断负环,如果存在负环,则输出-1

#include <iostream>
#include <algorithm>
#include <queue>
#include <vector>
#include <stdio.h>
using namespace std;
typedef long long ll;
const int INF = 0x3fffffff;
const int MAXN = 1010;
const int MAXE = 20010;
struct belEdge {
	int from, to, ccost;
}belEdges[MAXE]; 

int E = 0;
ll bD[MAXN];
int N, ML, MD;

//最短路
void belShortPath(int s) {
	fill(bD, bD + MAXN, INF);
	bD[s] = 0;
	while (true) {
		bool updated = false;
		for (int i = 0; i < E; ++i) {
			belEdge e = belEdges[i];
			if (bD[e.from] != INF && bD[e.to] > bD[e.from] + e.ccost) {
				bD[e.to] = bD[e.from] + e.ccost;
				updated = true;
			}
		}
		if(!updated) break;
	}
}
//判断负环
bool findNegeLoop() {
	fill(bD, bD + MAXN, 0);
	for (int i = 0; i < N; ++i) {
		for (int j = 0; j < E; ++j) {
			belEdge e = belEdges[j];
			if (bD[e.to] > bD[e.from] + e.ccost) {
				bD[e.to] = bD[e.from] + e.ccost;
				if (i == N - 1) return true;  //存在负圈
			}
		}
	}
	return false;
}

int main() {
	scanf("%d %d %d", &N, &ML, &MD); 
	int x, y, cs;
	for (int i = 0; i < ML; ++i) {
		scanf("%d %d %d", &x, &y, &cs);
		belEdges[E].from = x;
		belEdges[E].to = y;
		belEdges[E++].ccost = cs;
	}
	for (int j = 0; j < MD; ++j) {
		scanf("%d %d %d", &x, &y, &cs);
		belEdges[E].from = y;
		belEdges[E].to = x;
		belEdges[E++].ccost = -cs;  //取反
	}
	
	bool isnega = findNegeLoop();
	if (isnega) {
		printf("-1\n");
		return 0;
	}
	//求点1到点N得最短路
	belShortPath(1);
	if (bD[N] == INF) {
		printf("-2\n");
	}
	else {
		printf("%lld\n", bD[N]);
	}
	system("PAUSE");
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值