poj 3169 Layout 差分约束

来源:互联网 发布:宁波市政府数据服务网 编辑:程序博客网 时间:2024/05/13 15:25
Layout
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 6712 Accepted: 3235

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 11 3 102 4 202 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.

Source

USACO 2005 December Gold

题意:n头牛(编号1到n)在排成一队,编号大的不能站在编号较小者前方。有一些限制条件,分为两种:
(1)(x,y,maxd)表示牛x和牛y的距离不能超过maxd,x < y;
(2)(x,y,maxd)表示牛x和牛y的距离不能小于mind,x < y;
求方案使牛1和牛n距离最大。

查分约束系统。
设dx为第x头牛的位置,则有若干个不等式,共分为以下3种:
(1) dx <= d(x+1) -> dx <= d(x+1) + 0从x+1向x连长度为0的边
(2) dy - dx <= maxd -> dy <= dx + maxd从x向y连长度为maxd的边。
(2) dy - dx >= mind -> dx <= dy - mind从y向x连长度为-mind的边。
用bellmanford求最短路。若没有从1到n的路说明牛1和牛n的距离可以取无穷大(-2);若存在负环说明无解;其余情况下1到n的最短路长度就是牛1和牛n的最大距离。

#include <iostream>#include <algorithm>#include <cstdio>#include <string>#include <cstring>#include <cmath>#include <vector>#include <list>#include <map>#include <set>#include <deque>#include <queue>#include <stack>#include <bitset>#include <functional>#include <sstream>#include <iomanip>#include <cmath>#include <cstdlib>#include <ctime>typedef long long ll;//#pragma comment(linker, "/STACK:1024000000,1024000000")  //手动扩栈#define INF 1e9#define maxn 1100#define maxm 100086+10#define mod 1000000007#define eps 1e-7#define PI acos(-1.0)#define rep(i,n) for(int i=0;i<n;i++)#define rep1(i,n) for(int i=1;i<=n;i++)#define scan(n) scanf("%d",&n)#define scanll(n) scanf("%I64d",&n)#define scan2(n,m) scanf("%d%d",&n,&m)#define scans(s) scanf("%s",s);#define ini(a) memset(a,0,sizeof(a))#define out(n) printf("%d\n",n)using namespace std;#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1struct Edge{int from,to,dist;Edge(int f,int t,int d):from(f),to(t),dist(d){}Edge(){}};struct BellmanFord{int n,m;vector<int> G[maxn];vector<Edge> edges;bool inq[maxn];int d[maxn];//int p[maxn];int cnt[maxn];void init(int n){this->n = n;rep1(i,n) G[i].clear();edges.clear();}void Addedge(int from,int to,int dist){edges.push_back(Edge(from,to,dist));m = edges.size();G[from].push_back(m-1);}bool negativeCycle(){queue<int> q;ini(inq);ini(cnt);rep1(i,n) d[i] = INF;d[1] = 0;inq[1] = 1;q.push(1);while(!q.empty()){int u = q.front();q.pop();inq[u] = false;rep(i,(int)G[u].size()){Edge &e = edges[G[u][i]];if(d[e.to] > d[u] + e.dist){d[e.to] = d[u] + e.dist;if(!inq[e.to]){q.push(e.to);inq[e.to] = true;if(++cnt[e.to] > n) return true;}}}}return false;}}bel;int main(){#ifndef ONLINE_JUDGEfreopen("in.txt","r",stdin);//  freopen("out.txt","w",stdout);#endifint n,m1,m2;while(~scanf("%d%d%d",&n,&m1,&m2)){bel.init(n);rep1(i,n-1){bel.Addedge(i+1,i,0);}int f,t,d;rep1(i,m1){scanf("%d%d%d",&f,&t,&d);bel.Addedge(f,t,d);}rep1(i,m2){scanf("%d%d%d",&f,&t,&d);bel.Addedge(t,f,-d);}bool flag = bel.negativeCycle();if(flag) puts("-1");else{if(bel.d[n] == INF) puts("-2");else out(bel.d[n]);}}return 0;}



0 0
原创粉丝点击