http://www.elijahqi.win/archives/3083
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.22,zhucheng
仍然是二分+2-sat 的题目 然而我的语文太差太差了 读题读了很久也漏了很多条件导致写错了
题意:如果两头牛去一个地方则是同时到达这个地方的权值和加起来 否则就是两个权值各取一下然后加上s1,s2之间的距离 注意读入顺序hate先
每次二分一个答案 然后验证四种情况即都到s1,s2 先s1再s2 先s2再s1 如果这样的距离大于我二分的答案 那么用2-sat限制一下避免这种情况的出现即可
2-sat建图 自己yy啦
#include<cmath>
#include<cstdio>
#include<cctype>
#include<cstring>
#include<algorithm>
#define ll long long
using namespace std;
const int M=2e6;
const int N=1010;
int nn,dis1[N],dis2[N],dis,dfn[N],low[N],q[N],top,id1[N],id2[N];
bool stackf[N];int s1x,s1y,s2x,s2y,num1,num,h[N],h1[N],b[N],s,n,A,B;
inline char gc(){
static char now[1<<16],*S,*T;
if (T==S){T=(S=now)+fread(now,1,1<<16,stdin);if(T==S) return EOF;}
return *S++;
}
inline int read(){
int x=0,f=1;char ch=gc();
while(!isdigit(ch)) {if (ch=='-') f=-1;ch=gc();}
while(isdigit(ch)) x=x*10+ch-'0',ch=gc();
return x*f;
}
struct node{int y,next;}data[M];
inline void insert1(int x,int y){
data[++num].y=y;data[num].next=h[x];h[x]=num;
}
inline void tarjan(int x){
dfn[x]=low[x]=++num;stackf[x]=1;q[++top]=x;
for (int i=h[x];i;i=data[i].next){
int y=data[i].y;
if (!dfn[y]) tarjan(y),low[x]=min(low[x],low[y]);else if(stackf[y]) low[x]=min(low[x],dfn[y]);
}
if(dfn[x]==low[x]){
int y;++s;
do{y=q[top--];stackf[y]=0;b[y]=s;
}while(y!=x);
}
}
inline bool check(int md){
memset(dfn,0,sizeof(dfn));memcpy(h,h1,sizeof(h1));num=num1;s=0;
for (int i=1;i<=n;++i){
for(int j=i+1;j<=n;++j){
if (dis1[i]+dis1[j]>md) insert1(id1[i],id2[j]),insert1(id1[j],id2[i]);
if (dis2[i]+dis2[j]>md) insert1(id2[i],id1[j]),insert1(id2[j],id1[i]);
if (dis1[i]+dis+dis2[j]>md) insert1(id1[i],id1[j]),insert1(id2[j],id2[i]);
if (dis2[i]+dis+dis1[j]>md) insert1(id2[i],id2[j]),insert1(id1[j],id1[i]);
}
}num=0;
for (int i=0;i<n<<1;++i) if (!dfn[i]) tarjan(i);
for (int i=1;i<=n;++i) if (b[id1[i]]==b[id2[i]]) return 0;
return 1;
}
inline int calc(int x,int x2,int y,int y2){return abs(x-x2)+abs(y-y2);}
int main(){
//freopen("poj2749.in","r",stdin);
n=read();A=read();B=read();int l=1,r=0;
for (int i=0;i<n;++i) id1[i+1]=i<<1,id2[i+1]=i<<1|1;
s1x=read();s1y=read();s2x=read();s2y=read();dis=calc(s1x,s2x,s1y,s2y);
for (int i=1;i<=n;++i){static int x,y;
x=read();y=read();
dis1[i]=calc(s1x,x,s1y,y);
dis2[i]=calc(s2x,x,s2y,y);r=max(max(dis1[i],dis2[i])*2+dis,r);
}
for (int i=1;i<=A;++i){static int x,y;x=read();y=read();
insert1(id1[x],id2[y]);insert1(id1[y],id2[x]);
insert1(id2[x],id1[y]);insert1(id2[y],id1[x]);
}
for (int i=1;i<=B;++i){static int x,y;x=read();y=read();
insert1(id1[x],id1[y]);insert1(id2[x],id2[y]);
insert1(id1[y],id1[x]);insert1(id2[y],id2[x]);
}int ans=-1;
num1=num;memcpy(h1,h,sizeof(h));
while(l<=r){
int mid=l+r>>1;
if(check(mid)) ans=mid,r=mid-1;else l=mid+1;
}printf("%d\n",ans);
return 0;
}