POJ3169_Layout(差分约束)

题目描述:

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.

样例输入:

4 2 1
1 3 10
2 4 20
2 3 3

样例输出:

27

题目大意:

一共有n头牛,有ml个关系好的牛的信息,有md个关系不好的牛的信息,对应输入的第一行的三个元素,接下来ml行,每行三个元素A,B,D,表示A牛和B牛相距不希望超过D,接下来md行,每行三个元素A,B,D表示A牛和B牛的相距至少要有D才行。求1号牛和n号牛的最大距离,如果距离无限大输出-2,如果无解输出-1。

思路:
简单的差分约束
求解max
n-1<=max
以1为起点,n为终点跑一下最短路就可以了,求出的d[n]就是答案

code:

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<queue>
using namespace std;
typedef pair<int,int> P;
int head[30050];
int visit[30050];
const int inf=99999999;
int l[30050];
int n,m;
struct node{
	int v,w,next;
}edge[150005];
int cnt;
int d[30050];
int flag=0;
void add(int u,int v,int w)
{
	edge[cnt].v=v;
	edge[cnt].w=w;
	edge[cnt].next=head[u];
	head[u]=cnt++;
}
int dijkstra()
{
	priority_queue<P,vector<P> ,greater<P>> que;
	memset(visit,0,sizeof(visit));
	que.push(P(0,1));
	fill(d,d+n+5,inf);
	d[1]=0;
	while(!que.empty())
	{
		P p=que.top();
		que.pop();
		int v=p.second;
		if(d[v]<p.first)continue;
		for(int i=head[v];i!=-1;i=edge[i].next)
		{
			int vv=edge[i].v;
			if(d[vv]>d[v]+edge[i].w)
			{
				d[vv]=d[v]+edge[i].w;
				l[vv]++;
				if(l[vv]>n)
				{
					flag=1;
					break;
				}
				que.push(P(d[vv],vv));
			}
		}
	}
}
int main()
{
	int dd;
	scanf("%d%d%d",&n,&m,&dd);
	memset(head,-1,sizeof(head));
	cnt=0;
	for(int i=0;i<m;i++)
	{
		int a,b,c;
		scanf("%d%d%d",&a,&b,&c);
		add(a,b,c);
	}
	for(int i=0;i<dd;i++)
	{
		int a,b,c;
		scanf("%d%d%d",&a,&b,&c);
		add(b,a,-c);
	}
	dijkstra();
	if(flag)
	{
        printf("-1\n");
    }
    else if(d[n]==inf)
    {
       printf("-2\n");
    }
    else printf("%d\n",d[n]);
	return 0;
 } 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值