差分约束:布局

题目描述 

Description

当排队等候喂食时,奶牛喜欢和它们的朋友站得靠近些。FJ有N(2<=N<=1000)头奶牛,编号从1到N,沿一条直线站着等候喂食。奶牛排在队伍中的顺序和它们的编号是相同的。因为奶牛相当苗条,所以可能有两头或者更多奶牛站在同一位置上。即使说,如果我们想象奶牛是站在一条数轴上的话,允许有两头或更多奶牛拥有相同的横坐标。

一些奶牛相互间存有好感,它们希望两者之间的距离不超过一个给定的数L。另一方面,一些奶牛相互间非常反感,它们希望两者间的距离不小于一个给定的数D。给出ML条关于两头奶牛间有好感的描述,再给出MD条关于两头奶牛间存有反感的描述。(1<=ML,MD<=10000,1<=L,D<=1000000)

你的工作是:如果不存在满足要求的方案,输出-1;如果1号奶牛和N号

奶牛间的距离可以任意大,输出-2;否则,计算出在满足所有要求的情况下,1号奶牛和N号奶牛间可能的最大距离。

输入描述 Input Description

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 Description

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

首回学差分约束,一定要搞懂。//努力努力努力。。。。。。差分约束是一种通过图论的方法求最大值or最小值or最大差值or最小差值的方法

最大值:if dist[a]>dist[b]+map[a,b]          then dist[a]:=dist[b]+map[a,b];       即:dist[a]<=dist[b]+map[a,b]       即:dist[a]-dist[b]<=map[a,b]       a-b<=c  就从b向a连一条权值为c的边       得到边,通过跑最短路
最小值:if dist[a]<dist[b]+map[a,b]          then dist[a]:=dist[b]+map[a,b];       即:dist[a]>=dist[b]+map[a,b]       即:dist[a]-dist[b]>=map[a,b]       a-b>=c  就从b向a连一条权值为c的边       得到边,通过跑最长路

此题考察差分中边的转化,和最大差值题中有ml,md两种类型的边因为dist[1]=0已被设定,dist[n]值最大,差值最大转化为了求最大值最大值要求   a-b<=c 的边应该把md的边左右两边乘-1  变号还有,i-(i-1)>=0即:(i-1)-i<=0从i向(i-1)连一条权值为0的边再跑最短路。

code:
var n,ml,md:longint;    x,y,z:longint;    map:array[1..1000,1..1000]of longint;    i,j,k:longint;    mark:array[1..1000]of boolean;    queue:array[1..20000]of longint;    w:array[1..2000]of longint;    head,tail:longint;    renewal:array[1..1000]of longint;
function max(x,y:longint):longint;         begin if x>y                  then exit(x)                  else exit(y);         end;
function min(x,y:longint):longint;         begin if x>y                  then exit(y)                  else exit(x);         end;
begin readln(n,ml,md);
      for i:=1 to n do          for j:=1 to n do              map[i,j]:=maxint*500;      for i:=2 to n do          map[i,i-1]:=0;      for i:=1 to ml do          begin readln(x,y,z);                map[min(x,y),max(x,y)]:=z;          end;      for i:=1 to md do          begin readln(x,y,z);                map[max(x,y),min(x,y)]:=-1*z;          end;
      for i:=1 to n do          w[i]:=maxint*500;
      fillchar(mark,sizeof(mark),true);      fillchar(queue,sizeof(queue),0);      fillchar(renewal,sizeof(renewal),0);      head:=1; tail:=n;      for i:=1 to n do          begin queue[i]:=i;                mark[i]:=false;          end;      w[1]:=0;
      while head<=tail do            begin for i:=1 to n do                      begin if w[i]>w[queue[head]]+map[queue[head],i]                               then begin w[i]:=w[queue[head]]+map[queue[head],i];                                          inc(renewal[i]);                                          if renewal[i]>n                                             then begin writeln(‘-1‘);                                                        halt;                                                  end;                                          if mark[i]                                             then begin inc(tail);                                                        queue[tail]:=i;                                                        mark[i]:=false;                                                  end;                                    end;                      end;                  mark[queue[head]]:=true;                  inc(head);            end;      if w[n]-w[1]=maxint*500         then writeln(‘-2‘)         else writeln(w[n]-w[1]);end.
				
时间: 2024-08-05 11:16:59

差分约束:布局的相关文章

差分约束

1.bzoj3436 思路: 差分约束根据限制条件建图,注意要有一个超级源点向所有点连一条边权为0的边建图看代码. 然后spfa判负环,写bfs会超时的......实测n遍. #include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #define inf 0x7fffffff #define ll long long #define N 100007 using na

bzoj2788 festival 差分约束

填坑中--链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2788 题意: 有$n$个正整数$X1,X2,...,Xn$,再给出$m1+m2$个限制条件,限制分为两类:1. 给出$a,b(1<=a,b<=n)$,要求满足$Xa + 1 = Xb$2. 给出$c,d (1<=c,d<=n)$,要求满足$Xc <= Xd$在满足所有限制的条件下,求集合${Xi}$大小的最大值. 首先看情况我们也知道是差分约束-- 但是这个差分

POJ 1201 Intervals 差分约束

http://poj.org/problem?id=1201 TLE了很久,因为用了cin..... 思路和其他差分约束差不多,http://www.cppblog.com/menjitianya/archive/2015/11/19/212292.html 如果区间[a, b]中至少有c个元素,如果用上面的博客,那么说明xa - xb >= c,但是注意这里是闭区间,xa - xb是不包括b这个点的, 就比如用了[a, b]有c个元素,[b, d]有x个,那么ans = c + x - 1个,

【bzoj2330】: [SCOI2011]糖果 图论-差分约束-SPFA

[bzoj2330]: [SCOI2011]糖果 恩..就是裸的差分约束.. x=1 -> (A,B,0) (B,A,0) x=2 -> (A,B,1)  [这个情况加个A==B无解的要特判] x=3 -> (B,A,0)  [恩这个是不少于一开始zz建反了] x=4 -> (B,A,1) x=5 -> (A,B,0) 然后源点到所有点建1的边[恩据说有条链所以要反着连]跑最长路就好了 1 /* http://www.cnblogs.com/karl07/ */ 2 #inc

POJ1275出纳员的雇佣【差分约束】

出纳员的雇佣 Tehran的一家每天24小时营业的超市,需要一批出纳员来满足它的需要.超市经理雇佣你来帮他解决问题:超市在每天的不同时段需要不同数目的出纳员(例如:午夜时只需一小批,而下午则需要很多)来为顾客提供优质服务.他希望雇佣最少数目的出纳员.经理已经提供你一天的每一小时需要出纳员的最少数量--R(0), R(1), ..., R(23).R(0)表示从午夜到上午1:00需要出纳员的最少数目,R(1)表示上午1:00到2:00之间需要的,等等.每一天,这些数据都是相同的.有N人申请这项工作

【POJ3169 】Layout (认真的做差分约束)

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 ar

POJ 3169 Layout (差分约束)

题意:给定一些母牛,要求一个排列,有的母牛距离不能超过w,有的距离不能小于w,问你第一个和第n个最远距离是多少. 析:以前只是听说过个算法,从来没用过,差分约束. 对于第 i 个母牛和第 i+1 个,D[i] - D[i+1] <= 0,  D[j] -D[i ]<= k, D[i] - D[j] <= - k,那么这个题就可以用差分约束来求这个不等式组了. 1.对于差分不等式,a - b <= c ,建一条 b 到 a 的权值为 c 的边,求的是最短路,得到的是最大值(本题求的就

hdu 1364(差分约束)

King Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12056   Accepted: 4397 Description Once, in one kingdom, there was a queen and that queen was expecting a baby. The queen prayed: ``If my child was a son and if only he was a sound kin

BZOJ 3436: 小K的农场 差分约束

题目链接: http://www.lydsy.com/JudgeOnline/problem.php?id=3436 题解: 裸的差分约束: 1.a>=b+c  ->  b<=a-c  ->  d[v]<=d[u]+w  ->  建一条边从a到b,权值为-c 2.a<=b+c  ->  d[v]<=d[u]+w  -> 建一条边从b到a,权值为c 3.a==b  ->  d[v]<=d[u]+0&&d[u]<=d