zoukankan      html  css  js  c++  java
  • 差分约束:布局

    题目描述 
    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.


  • 相关阅读:
    CF263E Rhombus
    AtCoder Grand Contest 034
    Docker C/S 架构逻辑图
    使用filledeat modules配置
    filebeat分别收集多个类型日志
    ELK部署收集日志
    ES界面
    Django下的post请求访问出现403错误
    Django配置(urls.py)
    Day-1 python
  • 原文地址:https://www.cnblogs.com/spiderKK/p/4890269.html
Copyright © 2011-2022 走看看