poj 3169 Layout
Layout
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 10880 | Accepted: 5233 |
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.
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.
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
Hint
Explanation of the sample:
There are 4 cows. Cows #1 and #3 must be no more than 10 units apart, cows #2 and #4 must be no more than 20 units apart, and cows #2 and #3 dislike each other and must be no fewer than 3 units apart.
The best layout, in terms of coordinates on a number line, is to put cow #1 at 0, cow #2 at 7, cow #3 at 10, and cow #4 at 27.
There are 4 cows. Cows #1 and #3 must be no more than 10 units apart, cows #2 and #4 must be no more than 20 units apart, and cows #2 and #3 dislike each other and must be no fewer than 3 units apart.
The best layout, in terms of coordinates on a number line, is to put cow #1 at 0, cow #2 at 7, cow #3 at 10, and cow #4 at 27.
Source
题目大意:
n头牛排队,编号小的在编号大的前面,可以有几头牛站在同一个位置,有ml对牛彼此喜欢,距离必须<=d,有md对牛彼此讨厌,距离必须>=d,求队伍最长长度
无法排成队伍输出-1,队伍长度无限输出-2
注意点儿本题多组数据
差分约束
若d[x]-d[y]<=/>= c ,则由y向x连一条权值为c的边
假设牛排队序号递增,d[i]表示i号牛所在位置
那么我们的目的是 d[n]-d[1]=T,最大化T
那么可以表示为 d[n]-d[1]<=T,即d[n]<=T+d[1],所以spfa跑最短路
若spfa出现环,说明不能排成一队,若1余n不连通,说明队伍长度无限
设两头牛为x,y且x<=y
若x,y彼此喜欢,约束条件1:d[y]-d[x]<=Di
若x,y彼此讨厌,约束条件2:d[y]-d[x]>=Di
隐藏条件,约束条件3:d[i]-d[i-1]>=0
把运算符与d[n]<=T+d[1]统一
即:
d[y]-d[x]<=Di
d[x]-d[y]<=-Di
d[i-1]-d[i]<=0
#include<cstdio> #include<queue> #include<cstring> #include<algorithm> using namespace std; int n,ml,md,tot; queue<int>q; int sum[1001],front[1001]; long long dis[1001]; bool v[1001]; struct node { int to,next,w; }e[20001]; void add(int u,int v,int w) { e[++tot].to=v;e[tot].next=front[u];e[tot].w=w;front[u]=tot; } void spfa() { for(int i=1;i<=n;i++) dis[i]=1e15; memset(sum,0,sizeof(sum)); memset(v,false,sizeof(v)); while(!q.empty()) q.pop(); q.push(1);dis[1]=0;v[1]=true;sum[1]++; while(!q.empty()) { int now=q.front();q.pop();v[now]=false; for(int i=front[now];i;i=e[i].next) { int to=e[i].to; if(dis[to]>dis[now]+1ll*e[i].w) { dis[to]=dis[now]+1ll*e[i].w; if(!v[to]) { v[to]=true; q.push(to); sum[to]++; if(sum[to]==n) { printf("-1\n"); return; } } } } } printf("%lld\n",dis[n]==1e15 ? -2:dis[n]); } int main() { while(scanf("%d%d%d",&n,&ml,&md)!=EOF) { int u,v,w; for(int i=1;i<=n;i++) front[i]=0; for(int i=1;i<=ml;i++) { scanf("%d%d%d",&u,&v,&w); if(v>u) swap(u,v);//u>v,d[u]-d[v]<=w add(v,u,w); } for(int i=1;i<=md;i++) { scanf("%d%d%d",&u,&v,&w); if(v>u) swap(u,v);//u>v d[v]-d[u]<=-w add(u,v,-w); } for(int i=2;i<=n;i++) add(i,i-1,-1); spfa(); } }