POJ 3169--Layout【差分约束】

Layout
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 8402 Accepted: 4043

Description

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

题意:有n头排成一条线的的牛 和 ML + MD个限制条件,ML个条件表示 a , b两头牛之间距离最多为d,
MD个条件表示a, b头牛之间距离最少为d。 让你求出第一头到第n头牛的最大距离,
若不存在最大距离(存在负环)输出-1,若距离可以任意输出-2,否则输出最大距离。 
题目允许一个位置有多头牛。

明显的差分约束

思路:首先对于任一两头相邻的牛,它们之间距离大于或等于0; 其次,由ML个条件得到 S[b] - S[a] <= d ,
最后由MD个条件得到S[a] - S[b] <= -d。
根据这三个条件建图 并让1为源点求到n的最短路即可。若存在负环跳出并输出-1,
若最后的最短路为无穷大(初始化都是无穷大)输出-2,反之输出最短路。


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <iostream>
#define INF 0x3f3f3f3f
#define maxn  1100
using namespace std;

struct node {
	int u, v, w, next;
};

node edge[1100000];
int dist[maxn], head[maxn], cnt;
int used[maxn], n, ML, MD;
bool vis[maxn];

void init(){
	cnt = 0;
	memset(head, -1, sizeof(head));
	memset(used, 0, sizeof(used));
	memset(vis, 0 ,sizeof(vis));
}

void add(int u, int v, int w){
	edge[cnt] = {u, v, w, head[u]};
	head[u] = cnt++;
}


void getmap(){
		int a, b, d;
		for(int i = 1; i <= n; ++i)
			add(i + 1, i, 0);
		while(ML--){
			scanf("%d%d%d", &a, &b, &d);
			add(a, b, d);
		}
		while(MD--){
			scanf("%d%d%d", &a, &b, &d);
			add(b, a, -d);
		}	
}

void SPFA(){
	for(int i = 1; i <= n; ++i)
		dist[i] = INF;
	dist[1] = 0;
	queue<int>q;
	vis[1] = 1;
	used[1] = 1;
	q.push(1);
	while(!q.empty()){
		int u = q.front();
		q.pop();
		if(used[u] > n){
			printf("-1\n");
			return ;
		}
		vis[u]  = 0;
		for(int i = head[u]; i != -1; i = edge[i].next){
			int v = edge[i].v;
			int w = edge[i].w;
			if(dist[v] > dist[u] + w){
				dist[v] = dist[u] + w;
				if(!vis[v]){
					vis[v] = 1;
					used[v]++;
					q.push(v);
				}
			}
		}
	}
	if(dist[n] == INF)
		printf("-2\n");
	else 
		printf("%d\n", dist[n]);
	return ;
}

int main (){
	while(scanf("%d%d%d", &n, &ML, &MD)!=EOF){
		init();
		getmap();
		SPFA();
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值