现在的位置: 首页 > 综合 > 正文

hdu-3592-World Exhibition-差分约束

2014年01月27日 ⁄ 综合 ⁄ 共 2946字 ⁄ 字号 评论关闭

Description

Nowadays, many people want to go to Shanghai to visit the World Exhibition. So there are always a lot of people who are standing along a straight line waiting for entering. Assume that there are N (2 <= N <= 1,000) people numbered
1..N who are standing in the same order as they are numbered. It is possible that two or more person line up at exactly the same location in the condition that those visit it in a group.

There is something interesting. Some 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 X (1 <= X <= 10,000) constraints describes
which person like each other and the maximum distance by which they may be separated; a subsequent list of Y constraints (1 <= Y <= 10,000) tells which person 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 person 1 and person N that satisfies the distance constraints.

 

Input

First line: An integer T represents the case of test.

The next line: Three space-separated integers: N, X, and Y. 

The next X lines: Each line contains three space-separated positive integers: A, B, and C, with 1 <= A < B <= N. Person A and B must be at most C (1 <= C <= 1,000,000) apart.

The next Y lines: Each line contains three space-separated positive integers: A, B, and C, with 1 <= A < B <= C. Person A and B must be at least C (1 <= C <= 1,000,000) apart.

 

Output

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

Sample Input

1 4 2 1 1 3 8 2 4 15 2 3 4
 

Sample Output

19

题目意思:

X组,A,B两人互相喜欢,那么距离分开最多C

Y组,A,  B两人互相讨厌,那么距离至少为C

求从编号1到编号N最远分开多少???

如果不存在这样的排序,则输出-1 ,如果1和N可以相距任意的距离,则输出-2, 否则输出最长的距离。

思路:差分约束。这题差分约束很好想,图的边只要有两类:X+Y个题目的约束,N个前面两个人的约束(因为顺序是1 -- N)。还有需要注意的是输出:若原图有负环,则输出-1,若最后的距离是inf,则输出-2 , 否则输出距离。


代码:

#include <stdio.h>
#include <math.h>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <string>
#include <cstring> 
#include <cmath>
#define setbit(x,y) x|=(1<<(y)) //将X的第Y位置1
#define clrbit(x,y) x&=~(1<<(y)) //将X的第Y位清0
#define sf scanf
#define pf printf
#define INF 1 << 29
#define eps 1e-6
const double PI = acos(-1.0); 
#define lint __int64
#define LL long long 
//#define MAXN 1e9 + 7
#define maxn 10005
//101^110=011 异或
#define ULLint unsigned long long //2^64-1>1.8*10^19 
#define clr(x) memset(x, 0, sizeof(x))
#define Clr(x) memset(x, -1, sizeof(x))

using namespace std;

const int MAXN=1005;
const int MAXM=20005;
struct Edge {
	int u, v, w;  
}e[MAXM];
int m, n, d[MAXN];
void addedge(int from,int to,int weight) {
	e[m].u=from;
	e[m].v=to;
	e[m].w=weight;
	m ++;
}
bool Bellman_Ford(int s) { 
	int i, j;
	bool flag=false;
	for(i = 1; i <= n; i ++)
		d[i] = INF;
	d[s] = 0;
	for(i = 1; i <= n; i ++){
		flag = false;
		for(j = 0; j < m; j ++) {
			if(d[e[j].v] > d[e[j].u] + e[j].w) {
				d[e[j].v] = d[e[j].u] + e[j].w;
				flag = true;
			}
		}
		if(flag == 0)  break;
	}
	for(i = 0; i < m; i ++)
		if(d[e[i].v] > d[e[i].u] + e[i].w)
			return false;
	return true;
}
int main() {
	int u, v, w;
	int a, b, t;
	scanf("%d", &t);
	while(t --) {
		scanf("%d%d%d", &n, &a, &b);
		m = 0;
		while(a --) {
			scanf("%d%d%d", &u, &v, &w);
			addedge(u,v,w);
		}
		while(b --) { 
			scanf("%d%d%d", &u, &v, &w);
			addedge(v, u, -w);
		}
		if(Bellman_Ford(1)) { 
			if(d[n] != INF) 
				printf("%d\n", d[n]);
			else printf("-2\n");
		}
		else printf("-1\n"); 
	}
	return 0;
}

抱歉!评论已关闭.