POJ 3169 Layout(差分约束+SPFA)详解

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 14244ke 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 cow   Accepted: 6844

Description

Lis 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

Hint

Explanation of the sample: 

There are 4 cows. Cows #1 and #3 must be no more than 10 units apart, cows #2 and #4 must be no more than 20 units apart, and cows #2 and #3 dislike each other and must be no fewer than 3 units apart. 

The best layout, in terms of coordinates on a number line, is to put cow #1 at 0, cow #2 at 7, cow #3 at 10, and cow #4 at 27.

题意:有N头牛,编号1~N。按照编号顺序排成一排。在他们之间有些牛关系比较好,所以希望彼此之间不超过一定距离,给出ML表示有ML对(A,B,D),表示A与B间距离最大是D。也有一些牛关系非常不好,它们之间的距离也必须要大于一定距离。给出DL表示有DL对(A,B,D),表示A与B间距离最小是D。此外可能有多头牛站在同一个位置。求1号牛到N号牛之间的距离。如果不存在任何一种满足条件的排列方式则输出-1。无限大的情况输出-2。 

按照题意我们可以列出以下的不等式 :

对于Ml而言:d(B)-d(A)<D 

对于MD而言:d(A)-d(B)<D

有了不等式组,我们就可以利用差分约束和SPFA来解决这个问题了

AC代码

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<queue>
#define inf 0xfffffff
#define MAX 10005
using namespace std;
int head[MAX],dis[MAX],vis[MAX],cnt[MAX];
int n;
int tol=1;
struct edge 
{
	int to;
	int next;
	int cost;
}go[MAX];
void add(int u,int v,int c)
{
	go[tol].cost=c;//给结构体的cost和to赋值 
	go[tol].to=v;
	go[tol].next=head[u];//如果两条边是同一个起点,那么后面那条边的.next指向的head[u]就是前面边的tol值,即在后面 
	head[u]=tol;         //的spfa函数中可以连接同一个起点的所有相关的边 
	tol++; 
}
int spfa()
{
	int i;
	for(i=2;i<=n;i++) //dis数组初始化 
	{
		dis[i]=inf;
	}
	queue<int> q;
	vis[1]=1;
	cnt[1]++; //cnt数组记录是否出现环 
	q.push(1);
	while(!q.empty())
	{
		int u=q.front();//从队列的首开始处理 
		q.pop();  //给u赋值后,就把队首往后移动 
		vis[u]=0; //已经处理的就出队 
		for(i=head[u];i!=0;i=go[i].next) //遍历完以u为起点的所有边 
		{
			int v= go[i].to;
			int c= go[i].cost;
			if(dis[v]>dis[u]+c) //判断是否松弛成功 
			{
				dis[v]=dis[u]+c;
				if(!vis[v]) //如果v没在队列中,就让它入队 
				{
						q.push(v); //入队 
					vis[v]=1; //标记已入队 
					cnt[v]++;
					if(cnt[v]>n) //如果进队大于n次,则存在负权环,注意是进队n次不是更新n次 
					{
						printf("-1");
						return 1;
					}
				}
			}
		}
	}
	if(dis[n]==inf) printf("-2");//最短路仍为无穷大,可以随便排 
	else printf("%d",dis[n]);
}
int main()
{
	int ml,md;
	int u,v,c,i;
	scanf("%d%d%d",&n,&ml,&md);
	for(i=1;i<=ml;i++)
	{
	//	printf("i=%d\n",i);
		scanf("%d%d%d",&u,&v,&c);
		add(u,v,c);//构建从v-u<c的路径,注意我们调用函数时不等式符号都是<号 
	}
	for(i=1;i<=md;i++)
	{
		scanf("%d%d%d",&u,&v,&c);
		add(v,u,-c); //构建v-u>c的路径,移项u-v<-c,调用函数时-c表示c,u和v调换 
	}
//这里不加也可以
//        for(int i = 1; i < n; ++i)
//            add(i+1,i,0);
	spfa();
	return 0;
 } 

猜你喜欢

转载自blog.csdn.net/zvenWang/article/details/81392606