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

【差分约束系统】Layout POJ3169

2014年01月30日 ⁄ 综合 ⁄ 共 4129字 ⁄ 字号 评论关闭

Layout

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 5190   Accepted: 2491

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

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.

题目:有些ML对AB奶牛的距离最多为D,又有MD对AB奶牛的距离至少为D,求最后最长距离,无解输出-1,如果所有方案均可以输出-2

根据题目,第一次读入的a,b,d表示b-d最大为d,即b-a<=d

第二次读入a,b,d表示b-a最小为d,即b-a>=d

把两式统一一下为:

 a - b > = - d

 b - a > = - ( - d )

所以入边求最短路即可【至于为什么是最短路,我们看上面的式子,以第一个为例,a-b>=-d,移项得b<=a+d,即为最短路】

一开始用栈写(因为要判环,栈要快些),WA哭了。。。后来无奈之下改成队列,在改的过程中猛然发现写栈的时候又手残了,变量名有一次打错。。。队列该完后交AC,然后紧接着把栈的手残也改了,AC

不过很纳闷的是队列的居然还要快些。。。。。不应该啊。。。。。。

测评情况(POJ)

C++ AC Code(栈)

/*http://blog.csdn.net/jiangzh7
By Jiangzh*/
#include<cstdio>
#include<cstring>
const int N=1000+10;
int n;
struct link{int y,z;link *next;}*head[N];
int dist[N],inst[N],st[N],top=0;
int relax[N];

void inlink(int x,int y,int z)
{
	link *tmp=new link;
	tmp->y=y;tmp->z=z;
	tmp->next=head[x];
	head[x]=tmp;
}

int spfa()
{
	memset(dist,0x3f,sizeof(dist));
	memset(inst,0,sizeof(inst));
	dist[1]=0;
	st[top++]=1;inst[1]=1;
	++relax[1];
	while(top)
	{
		int x=st[--top];inst[x]=0;
		for(link *node=head[x];node;node=node->next)
			if(dist[node->y]>dist[x]+node->z)
			{
				dist[node->y]=dist[x]+node->z;
				if(!inst[node->y])
				{
					inst[node->y]=1;
					st[top++]=node->y;
					if(++relax[node->y]>n) return -1;
				}
			}
	}
	if(dist[n]==0x3f3f3f3f) return -2;
	return dist[n];
}

int main()
{
	freopen("layout.in","r",stdin);
	freopen("layout.out","w",stdout);
	int m,q;
	scanf("%d%d%d",&n,&m,&q);
	while(m--)
	{
		int a,b,d;scanf("%d%d%d",&a,&b,&d);
		inlink(a,b,d);  // a - b >= -d
	}
	while(q--)
	{
		int a,b,d;scanf("%d%d%d",&a,&b,&d);
		inlink(b,a,-d); // b - a >= d
	}
	for(int i=2;i<=n;i++) inlink(i,i-1,0);// i - (i-1) >= 0
	printf("%d\n",spfa());
	return 0;
}

C++ AC Code(队列)

/*http://blog.csdn.net/jiangzh7
By Jiangzh*/
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int N=1000+10;
int n;
struct link{int y,z;link *next;}*head[N];
int dist[N],inQ[N];
queue<int> q;
int relax[N];

void inlink(int x,int y,int z)
{
	link *tmp=new link;
	tmp->y=y;tmp->z=z;
	tmp->next=head[x];
	head[x]=tmp;
}

int spfa()
{
	memset(dist,0x3f,sizeof(dist));
	dist[1]=0;q.push(1);
	while(!q.empty())
	{
		int x=q.front();
		q.pop();inQ[x]=0;
		for(link *node=head[x];node;node=node->next)
			if(dist[node->y]>dist[x]+node->z)
			{
				dist[node->y]=dist[x]+node->z;
				if(!inQ[node->y])
				{
					inQ[node->y]=1;
					q.push(node->y);
					if(++relax[node->y]>n) return -1;
				}
			}
	}
	if(dist[n]==0x3f3f3f3f) return -2;
	return dist[n];
}

int main()
{
	freopen("layout.in","r",stdin);
	freopen("layout.out","w",stdout);
	int m,q;
	scanf("%d%d%d",&n,&m,&q);
	while(m--)
	{
		int a,b,d;scanf("%d%d%d",&a,&b,&d);
		inlink(a,b,d);  // a - b >= -d
	}
	while(q--)
	{
		int a,b,d;scanf("%d%d%d",&a,&b,&d);
		inlink(b,a,-d); // b - a >= d
	}
	for(int i=2;i<=n;i++) inlink(i,i-1,0);// i - (i-1) >= 0
	printf("%d\n",spfa());
	return 0;
}

抱歉!评论已关闭.