POJ 3169 Layout 差分约束

一、内容

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

二、思路

  • 差分约束

  • 差分约束条件:

    • b <= a + L 奶牛 A 和奶牛 B 至多相隔 L 的距离。
    • a <= b - D 奶牛 A 和奶牛 B 至少相隔 D 的距离。
    • si <= si+1 后一头奶牛的距离大于前一头
    • 建立超级源点0, xi <= x0 这样从0出发就能遍历所有的边
  • 首先从0出发判断有无负环,若有负环输出 -1

  • 若无负环则求1 和 n 的最大距离差。 若最大距离差是无穷,输出-2。

    • 最大距离差: xn - x1 <= c1 + c2 + … + cn 后面的一坨就是1到n的最短距离
    • xn <= x1 + c1 + c2 + … + cn 代表1到n的边距离为后面,那么求出后面的最小值(1到n的最短距离)就是1和n的最大差距差了。

三、代码

#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int N = 1e3 + 5, M = 4e4 + 5, INF = 0x3f3f3f3f;
struct E {
	int v, w, next;
} e[M];
int n, m1, m2, a, b, D, len, d[N], h[N], cnt[N];
bool vis[N];
void add(int u, int v, int w) {
	e[++len].v = v; e[len].w = w; e[len].next = h[u]; h[u] = len;
}
bool spfa(int s) {
	memset(d, 0x3f, sizeof(d));
	memset(vis, false, sizeof(vis));
	memset(cnt, 0, sizeof(cnt));
	d[s] = 0;
	queue<int> q;
	q.push(s);
	while (!q.empty()) {
		int u = q.front(); q.pop();
		vis[u] = false;
		for (int j = h[u]; j; j = e[j].next) {
			int v = e[j].v;
			int w = d[u] + e[j].w;
			if (w < d[v]) {
				d[v] = w;
				cnt[v] = cnt[u] + 1;
				if (cnt[u] >= n + 1) return 1;
				if (!vis[v]) q.push(v), vis[v] = true; 
			}
		}
	}
	return 0;
}
int main() {
	scanf("%d%d%d", &n, &m1, &m2);
	for (int i = 1; i <= m1; i++) {
		scanf("%d%d%d", &a, &b, &D);
		add(a, b, D);
	}
	for (int i = 1; i <= m2; i++) {
		scanf("%d%d%d", &a, &b, &D);
		add(b, a, -D);
	}
	for (int i = 1; i < n; i++) add(i + 1, i, 0); 
	//让所有的边小于0号点 默认所有点小于0 
	for (int i = 1; i <= n; i++) add(0, i, 0);
	if (spfa(0)) printf("-1");
	else {
		//判断最大值
		spfa(1);
		if (d[n] > INF / 2) printf("-2");
		else printf("%d", d[n]); 
	}
	return 0;
} 
发布了414 篇原创文章 · 获赞 380 · 访问量 6万+

猜你喜欢

转载自blog.csdn.net/qq_41280600/article/details/104216970