Layout(POJ 3169)---线性约束 - 最短路模型 + 判负环

题目链接

题目描述

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.

输入格式

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.

输出格式

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

输出样例

27

分析

线性约束模板题,主要需要注意的点是①题目中明确给出A<B②有隐含条件d[i+1]-d[i]>=0,虽然数据水不加也能过,具体参考代码。
对于差分约束不太懂的同学可以参考这个大佬写的文章,写的贼好!!!—传送门

源程序

SPFA算法

//#include <bits/stdc++.h>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
#define MAXN 1005
#define INF 0x3f3f3f3f
using namespace std;	//链式前向星 
struct Edge{
	int v,w,next;
	Edge(){};
	Edge(int _v,int _w,int _next){
		v=_v,w=_w,next=_next;
	};
}edge[MAXN*20];
int EdgeCount,head[MAXN];
int n,ml,md,dis[MAXN],ven[MAXN],nums[MAXN];
void addEdge(int u,int v,int w)	//链式前向星建图 
{
	edge[++EdgeCount]=Edge(v,w,head[u]);
	head[u]=EdgeCount;
}
bool SPFA()
{
	queue<int> q;
	memset(dis,0x3f,sizeof(dis));
	memset(ven,0,sizeof(ven));
	memset(nums,0,sizeof(nums));
	dis[1]=0;
	ven[1]=nums[1]=1;
	q.push(1);
	while(!q.empty()){
		int u=q.front();q.pop();
		ven[u]=0;
		for(int i=head[u];i;i=edge[i].next){
			int v=edge[i].v,w=edge[i].w;
			if(dis[v]>dis[u]+w){
				dis[v]=dis[u]+w;
				if(!ven[v]){
					q.push(v);
					ven[v]=1;
					nums[v]++;
					if(nums[v]>n)return false;
				}
			}
		}
	}
	return true;
} 
int main()
{	
	memset(head,0,sizeof(head));
	EdgeCount=0;
	scanf("%d%d%d",&n,&ml,&md);
	for(int i=1;i<=ml;i++){	//最大距离建正边 
		int u,v,w;
		scanf("%d%d%d",&u,&v,&w);
		addEdge(u,v,w); 
	} 
	for(int i=1;i<=md;i++){	//最小距离建反边 
		int u,v,w;
		scanf("%d%d%d",&u,&v,&w);
		addEdge(v,u,-w); 
	} 
	for(int i=1;i<n;i++)	//按编号顺序排队 
		addEdge(i+1,i,0);
	if(SPFA()){
		if(dis[n]==INF)printf("%d\n",-2);	//可以任意分开 
		else printf("%d\n",dis[n]);			
	}
	else printf("%d\n",-1);		//无法进行排队 
} 

SPFA算法之SLF优化

//#include <bits/stdc++.h>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
#define MAXN 1005
#define INF 0x3f3f3f3f
using namespace std;	//链式前向星 
struct Edge{
	int v,w,next;
	Edge(){};
	Edge(int _v,int _w,int _next){
		v=_v,w=_w,next=_next;
	};
}edge[MAXN*20];
int EdgeCount,head[MAXN];
int n,ml,md,dis[MAXN],ven[MAXN],nums[MAXN];
void addEdge(int u,int v,int w)	//链式前向星建图 
{
	edge[++EdgeCount]=Edge(v,w,head[u]);
	head[u]=EdgeCount;
}
bool SPFA()
{
	deque<int> q;
	memset(dis,0x3f,sizeof(dis));
	memset(ven,0,sizeof(ven));
	memset(nums,0,sizeof(nums));
	dis[1]=0;
	ven[1]=nums[1]=1;
	q.push_back(1);
	int k;
	while(k=q.size()){
		int u=q.front();q.pop_front();
		ven[u]=0;
		for(int i=head[u];i;i=edge[i].next){
			int v=edge[i].v,w=edge[i].w;
			if(dis[v]>dis[u]+w){
				dis[v]=dis[u]+w;
				if(!ven[v]){
					if(k>1&&dis[v]<dis[q.front()]) q.push_front(v);
					else q.push_back(v);
					ven[v]=1;
					nums[v]++;
					if(nums[v]>n)return false;
				}
			}
		}
	}
	return true;
} 
int main()
{	
	memset(head,0,sizeof(head));
	EdgeCount=0;
	scanf("%d%d%d",&n,&ml,&md);
	for(int i=1;i<=ml;i++){	//最大距离建正边 
		int u,v,w;
		scanf("%d%d%d",&u,&v,&w);
		addEdge(u,v,w); 
	} 
	for(int i=1;i<=md;i++){	//最小距离建反边 
		int u,v,w;
		scanf("%d%d%d",&u,&v,&w);
		addEdge(v,u,-w); 
	} 
	for(int i=1;i<n;i++)	//按编号顺序排队 
		addEdge(i+1,i,0);
	if(SPFA()){
		if(dis[n]==INF)printf("%d\n",-2);	//可以任意分开 
		else printf("%d\n",dis[n]);			
	}
	else printf("%d\n",-1);		//无法进行排队 
} 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值