【差分约束系统】Layout POJ3169

本文介绍如何使用SPFA算法解决一道经典的算法题——奶牛排队问题。该问题涉及多个约束条件,包括奶牛之间的最大和最小距离限制。通过建立图模型,并运用SPFA算法求解最短路径,最终确定奶牛排队的最大可能距离。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.youkuaiyun.com/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.youkuaiyun.com/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;
}






内容概要:本文档详细介绍了基于MATLAB实现多目标差分进化(MODE)算法进行无人机三维路径规划的项目实例。项目旨在提升无人机在复杂三维环境中路径规划的精度、实时性、多目标协调处理能力、障碍物避让能力和路径平滑性。通过引入多目标差分进化算法,项目解决了传统路径规划算法在动态环境和多目标优化中的不足,实现了路径长度、飞行安全距离、能耗等多个目标的协调优化。文档涵盖了环境建模、路径编码、多目标优化策略、障碍物检测与避让、路径平滑处理等关键技术模块,并提供了部分MATLAB代码示例。 适合人群:具备一定编程基础,对无人机路径规划和多目标优化算法感兴趣的科研人员、工程师和研究生。 使用场景及目标:①适用于无人机在军事侦察、环境监测、灾害救援、物流运输、城市管理等领域的三维路径规划;②通过多目标差分进化算法,优化路径长度、飞行安全距离、能耗等多目标,提升无人机任务执行效率和安全性;③解决动态环境变化、实时路径调整和复杂障碍物避让等问题。 其他说明:项目采用模块化设计,便于集成不同的优化目标和动态环境因素,支持后续算法升级与功能扩展。通过系统实现和仿真实验验证,项目不仅提升了理论研究的实用价值,还为无人机智能自主飞行提供了技术基础。文档提供了详细的代码示例,有助于读者深入理解和实践该项目。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值