HDU-3592 SPFA 差分约束

版权声明:本文为博主原创文章,若需要引用、转载,只需注明来源及原文链接,若有错误,欢迎纠正。 https://blog.csdn.net/u014137295/article/details/87996188

Problem 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
解题思路:
用边的松弛代表不等式
代码:

#include<bits/stdc++.h>
#define maxn 1005
using namespace std;
inline int read(){
    int x=0,f=0;char ch=getchar();
    while(ch>'9'||ch<'0')f|=ch=='-',ch=getchar();
    while(ch>='0'&&ch<='9')x=(x<<3)+(x<<1)+(ch^48),ch=getchar();
    return f?-x:x;
}
int allroad,n,x,y,vis[maxn],head[maxn],d[maxn],in[maxn];
struct r{
	int e,d,nextroad;
}road[maxn*maxn>>1];
void add(int a,int b,int d){
	road[allroad].e=b;
	road[allroad].d=d;
	road[allroad].nextroad=head[a];
	head[a]=allroad++;
}
int SPFA(int s){
	memset(vis,0,sizeof(vis));
	memset(in,0,sizeof(in));
	memset(d,0x3f,sizeof(d));
	d[s]=0,vis[s]=in[s]=1;
	queue<int>q;
	q.push(s);
	while(!q.empty()){
		int now=q.front();
		if(in[now]>n)return -1;//无解
		q.pop(),vis[now]=0;
		for(int i=head[now];~i;i=road[i].nextroad){
			int to=road[i].e,dd=road[i].d;
			if(d[now]+dd<d[to]){
				d[to]=d[now]+dd;
				if(!vis[to]){
					q.push(to),vis[to]=1;
					++in[to];
				}
			}
		}
	}
	if(d[n]==0x3f3f3f3f)return -2;//任意解
	else return d[n];
}
int main(){
	int t=read();
	while(t--){
		allroad=0;
		memset(head,-1,sizeof(head));
		n=read(),x=read(),y=read();
		while(x--){
			int u=read(),v=read(),d=read();
			add(u,v,d);
		}
		while(y--){
			int u=read(),v=read(),d=read();
			add(v,u,-d);
		}
		printf("%d\n",SPFA(1));
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/u014137295/article/details/87996188