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 3Sample Output
27 题意:给出n头牛,编号为1-n,他们按编号顺序排列,可以在同一个位置.接下来给出一些关系,求1到n的距离1.即x[i]<=x[i+1] 即建立i+1到i的有向边 权值为0
接下来给出一些关系,首先是m1个关系,两头牛a,b的距离不能超过距离c
2.即b-a<=c,即b<=a+c即可建立a到b的有向边权值为c
再给出m2个关系,两头牛a。b的距离不能小于c
3.即b-a>=c,即a<=b-c,即建立b到a的有向边权值为-c
根据上述三个建立有向边 然后spfa 因为存在负环 让他循环至少n次
最后判断dis【n】,即1到n的距离,如过为inf,则无距离限制输出-2,如果小于0,即负环,则不满足输出-1.如果为正整数,则直接输出
典型的差分约束问题
#include <iostream> #include<cstdio> #include<queue> #include<cstring> #include<algorithm> using namespace std; #define inf 1e9 #define maxn 1100 #define M 21000 struct node { int next,to,w; }edge[M]; int dis[maxn]; int head[maxn]; int n,m1,m2; bool inq[maxn]; void spfa() { queue<int>q; for(int i = 1;i<=n;i++) dis[i]=inf; memset(inq,0,sizeof(inq)); dis[1]=0; q.push(1); int t=1; while(!q.empty()) { t++; int u = q.front(); q.pop(); inq[u]=0; for(int i=head[u];i!=-1;i=edge[i].next) { int to=edge[i].to; int w=edge[i].w; if(dis[to]>dis[u]+w) { dis[to]=dis[u]+w; if(!inq[to]) { inq[to]=true; q.push(to); } } } if(t>maxn+10)break; } } int main() { int a,b,c; while(~scanf("%d%d%d",&n,&m1,&m2)) { int tot=0; memset(head,-1,sizeof head); for(int i=0;i<m1;i++) { scanf("%d%d%d",&a,&b,&c); edge[tot].to=b; edge[tot].w=c; edge[tot].next=head[a]; head[a]=tot; tot++; } for(int i=0;i<m2;i++) { scanf("%d%d%d",&a,&b,&c); edge[tot].to=a; edge[tot].w=-c; edge[tot].next=head[b]; head[b]=tot; tot++; } for(int i=1;i<n;i++) { edge[tot].to=i; edge[tot].w=0; edge[tot].next=head[i+1]; head[i+1]=tot; tot++; } spfa(); if(dis[n]==inf)printf("-2\n"); else if(dis[n]<0)printf("-1\n"); else printf("%d\n",dis[n]); } }
