# [BZOJ1731]&&[Usaco2005 dec] Layout 排队布局

## 描述 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.

## 输入格式 InputFormat

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.

## 输出格式 OutputFormat

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 11
2 4 27
2 3 3

## 样例输出 SampleOutput

35

BZOJ 1731

``````#include <stdio.h>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;
const int inf=0x7fffffff/27.11;
int i,j,t,n,m,l,r,k,z,y,x;
int ml,dl,cnt;
struct edge
{
int to,next,val;
}e;
int head,dis,inq;
bool used;
deque <int> q;
inline void insert(int u,int v,int w)
{
e[++cnt].to=v;e[cnt].next=head[u];e[cnt].val=w;
head[u]=cnt;
}
inline bool spfa(int s)
{
int i,j,t,u,v,w;
memset(used,false,sizeof(used));
memset(inq,0,sizeof(inq));
for (i=1;i<=n;i++) dis[i]=inf;
while (!q.empty()) q.pop_front();
q.push_back(s);dis[s]=0;inq[s]=1;used[s]=true;
while (!q.empty())
{
u=q.front();q.pop_front();used[u]=false;
for (i=head[u];i;i=e[i].next)
{
v=e[i].to;w=e[i].val;
if (dis[v]>dis[u]+w)
{
dis[v]=dis[u]+w;
if (!used[v])
{
inq[v]++;
if (inq[v]>n) return false;
if (!q.empty() && dis[q.front()]>dis[v]) q.push_front(v);
else q.push_back(v);
}
}
}
}
return true;
}
int main()
{
scanf("%d%d%d",&n,&ml,&dl);
cnt=0;
for (i=1;i<=ml;i++)
{
scanf("%d%d%d",&x,&y,&z);
if (x>y) swap(x,y);
insert(x,y,z);
}
for (i=1;i<=dl;i++)
{
scanf("%d%d%d",&x,&y,&z);
if (x>y) swap(x,y);
insert(y,x,-z);
}
if (!spfa(1)) printf("-1\n");
else if (dis[n]==inf) printf("-2\n");
else printf("%d\n",dis[n]);
return 0;
}``````