Building Roads_poj2749_2-sat+二分

来源:互联网 发布:软件停止运行怎么回事 编辑:程序博客网 时间:2024/06/06 16:39


Description

Farmer John's farm has N barns and there are some cows that live in each barn. The cows like to drop around so John wants to build some roads to connect these barns. If he builds roads for every pair of different barns then he must build N * (N - 1) / 2 roads which is so costly that cheapskate John will never do that though that's the best choice for the cows. 

Clever John just had another good idea. He first builds two transferring point S1 and S2 and then builds a road connecting S1 and S2 and N roads connecting each barn with S1 or S2 namely every barn will connect with S1 or S2 but not both. So that every pair of barns will be connected by the roads. To make the cows don't spend too much time while dropping around John wants to minimize the maximum of distances between every pair of barns. 

That's not the whole story because there is another troublesome problem. The cows of some barns hate each other and John can't connect their barns to the same transferring point. The cows of some barns are friends with each other and John must connect their barns to the same transferring point. What a headache! Now John turns to you for help. Your task is to find a feasible optimal road-building scheme to make the maximum of distances between every pair of barns as short as possible which means that you must decide which transferring point each barn should connect to. 

We have known the coordinates of S1 S2 and the N barns the pairs of barns in which the cows hate each other and the pairs of barns in which the cows are friends with each other. 

Note that John always builds roads vertically and horizontally so the length of road between two places is their Manhattan distance. For example saying two points with coordinates (x1 y1) and (x2 y2) the Manhattan distance between them is |x1 - x2| + |y1 - y2|. 

Input

The first line of input consists of 3 integers N A and B (2 <= N <= 500 0 <= A <= 1000 0 <= B <= 1000) which are the number of barns the number of pairs of barns in which the cows hate each other and the number of pairs of barns in which the cows are friends with each other. 

Next line contains 4 integer sx1 sy1 sx2 sy2 which are the coordinates of two different transferring point S1 and S2 respectively. 

Each of the following N line contains two integer x and y. They are coordinates of the barns from the first barn to the last one. 

Each of the following A lines contains two different integers i and j(1 <= i < j <= N) which represent the i-th and j-th barns in which the cows hate each other. 

The same pair of barns never appears more than once. 

Each of the following B lines contains two different integers i and j(1 <= i < j <= N) which represent the i-th and j-th barns in which the cows are friends with each other. The same pair of barns never appears more than once. 

You should note that all the coordinates are in the range [-1000000 1000000]. 

Output

You just need output a line containing a single integer which represents the maximum of the distances between every pair of barns if John selects the optimal road-building scheme. Note if there is no feasible solution just output -1.

Sample Input

4 1 1

12750 28546 15361 32055

6706 3887

10754 8166

12668 19380

15788 16059

3 4

2 3

Sample Output

53246

Source

POJ Monthly--2006.01.22zhucheng

题目大意:

给出n个牛棚、两个特殊点S1S2的坐标。S1S2直连。牛棚只能连S1S2。还有,某些牛棚只能连在同一个S,某些牛棚不能连在同一个S。求使最长的牛棚间距离最小

思路:

2-sat,二分查找可能的ansmid并构图:

  i表示连接s1i表示连接s2

  dis[i]表示从i点到s1的距离,dis[i]表示从i点到s2的距离,d表示两个特殊点的距离

  dis[i]+dis[j]>midi->j j->i

  dis[i]+dis[j]>midi->j  j->i

  dis[i]+dis[j]+d>midi->j  j->i

  dis[i]+dis[j]+d>midj->i  i->j

还有就是给定的关系构图

  x hate yx->yy->xx->yy->x

  x friend with yx->yy->xx->yy->x

Longint实测可以过数据,注意如果没有答案输出-1,略坑。

源代码/pas

 

type  edge=record    x,y,next:Longint;  end;var  a,b,maxE,n,d,t:longint;  comp,low,dfn,dis,ls,s:array[0..1000]of longint;  x1,x2,y1,y2:array[0..1000]of longint;  v:array[0..1000]of boolean;  e:array[0..2005000]of edge;function min(x,y:Longint):longint;begin  min:=x;  if y<x then  min:=y;end;procedure add(x,y:Longint);begin  inc(maxE);  e[maxE].x:=x;  e[maxE].y:=y;  e[maxE].next:=ls[x];  ls[x]:=maxE;End;procedure tarjan(x:longint);var  i,y:longint;begin  inc(t);  dfn[x]:=t;  low[x]:=t;  inc(s[0]);  s[s[0]]:=x;  v[x]:=true;  i:=ls[x];  while i>0 do  begin    y:=e[i].y;    if dfn[y]=0 then    begin      tarjan(y);      low[x]:=min(low[y],low[x]);    end    else    if (v[y]) then    low[x]:=min(low[x],dfn[y]);    i:=e[i].next;  end;  if dfn[x]=low[x] then  begin    inc(comp[0]);    repeat      y:=s[s[0]];      comp[y]:=comp[0];      dec(s[0]);      v[y]:=false;    until x=y;  end;end;procedure build(x:Longint);var  i,j:longint;begin  fillchar(ls,sizeof(ls),0);  maxE:=0;  for i:=1 to a do  begin    add(x1[i],y1[i]+n);    add(y1[i],x1[i]+n);    add(y1[i]+n,x1[i]);    add(x1[i]+n,y1[i]);  end;  for i:=1 to b do  begin    add(x2[i],y2[i]);    add(y2[i],x2[i]);    add(x2[i]+n,y2[i]+n);    add(y2[i]+n,x2[i]+n);  end;  for i:=1 to n do  for j:=i+1 to n do  if i<>j then  begin    if dis[i]+dis[j]>x then    begin      add(i,j+n);      add(j,i+n);    end;    if dis[i+n]+dis[j+n]>x then    begin      add(i+n,j);      add(j+n,i);    end;    if dis[i]+d+dis[j+n]>x then    begin      add(i,j);      add(j+n,i+n);    end;    if dis[i+n]+d+dis[j]>x then    begin      add(i+n,j+n);      add(j,i);    end;  end;end;function check:Boolean;var  i:longint;begin  check:=true;  t:=0;  fillchar(v,sizeof(v),false);  fillchar(dfn,sizeof(dfn),0);  fillchar(low,sizeof(low),0);  fillchar(comp,sizeof(comp),0);  fillchar(s,sizeof(s),0);  for i:=1 to n do  if dfn[i]=0 then tarjan(i);  for i:=1 to n do  if comp[i]=comp[i+n] then  exit(false);end;procedure main;var  i,j,k,l,r,mid:Longint;  x,y:array[0..1000]of longint;  ans,sx1,sx2,sy1,sy2:longint;begin  readln(n,a,b);  readln(sx1,sy1,sx2,sy2);  for i:=1 to n do readln(x[i],y[i]);  for i:=1 to a do readln(x1[i],y1[i]);  for i:=1 to b do readln(x2[i],y2[i]);  for i:=1 to n do dis[i]:=abs(x[i]-sx1)+abs(y[i]-sy1);  for i:=1 to n do dis[i+n]:=abs(x[i]-sx2)+abs(y[i]-sy2);  d:=abs(sx1-sx2)+abs(sy1-sy2);  ans:=-1;  l:=1;  r:=4000000;  repeat    mid:=(l+r)div 2;    build(mid);    if check then    begin      r:=mid-1;      ans:=mid;    end    else    l:=mid+1;  until l>r;  writeln(ans);end;begin  main;end.

0 0