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
典型的差分约束,包含a-b<=d,那么b+d>=a,构造权值为d的b->a有向边。a-b>=d,那么b-a<=-d,a+(-d)>=b,构造权值为-d的a->b有向边。
输出-2的情形说明1和n之间没有不等式的约束,反映到图中就是两点之间没有·通路
#include <iostream>
#include <cstring>
#include <string>
#include <vector>
#include <queue>
#include <cstdio>
#include <set>
#include <cmath>
#include <algorithm>
#define INF 0x3f3f3f3f
#define MAXN 200010
#define Mod 10001
using namespace std;
struct Edge
{
int v,w,next;
};
Edge edge1[MAXN<<1];
int head1[MAXN],n,m,e,vis[MAXN],dis[MAXN];
int q[MAXN],p[MAXN];
void add(Edge *edge,int *head,int u,int v,int w)
{
edge[e].v=v;
edge[e].w=w;
edge[e].next=head[u];
head[u]=e;
e++;
}
void spfa(Edge *edge,int *head,int u)
{
memset(vis,0,sizeof(vis));
memset(p,0,sizeof(p));
for(int i=1; i<=n; ++i)
dis[i]=INF;
dis[u]=0;
queue<int> q;
q.push(u);
while(!q.empty())
{
u=q.front();
q.pop();
vis[u]=0;
p[u]++;
for(int i=head[u]; i!=-1; i=edge[i].next)
{
int v=edge[i].v,w=edge[i].w;
if(w+dis[u]<dis[v])
{
dis[v]=w+dis[u];
if(!vis[v]&&p[v]<=n)
{
vis[v]=1;
q.push(v);
}
}
}
}
}
int main()
{
int ml,md,a,b,d;
while(~scanf("%d%d%d",&n,&ml,&md))
{
e=0;
memset(head1,-1,sizeof(head1));
while(ml--)
{
scanf("%d%d%d",&a,&b,&d);
if(a<b)
swap(a,b);
add(edge1,head1,b,a,d);
//a-b<=d
}
while(md--)
{
scanf("%d%d%d",&a,&b,&d);
if(a<b)
swap(a,b);
add(edge1,head1,a,b,-d);
}
spfa(edge1,head1,1);
if(p[n]>n)
printf("-1\n");
else if(dis[n]==INF)
printf("-2\n");
else
printf("%d\n",dis[n]);
}
return 0;
}