Building roads
Time Limit: 10000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 620 Accepted Submission(s): 174
Problem 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
题意:给出n个牛棚,两个特殊点坐标s1,s2,牛棚只能连s1或s2,有一些约束关系,一些牛不能在一起,一些一定要在,问能不能满足约束条件,不满足输出-1,否则输出时两个牛栏之间距离的最大值的最小情况。。
读完这题,看到那句拗口的话直觉就是要用二分了。。。
这题用2-SAT解,二分距离的最大值,牛栏经过s1或s2的距离不能大于这个最大值。
所以如果大于了这个最大值就变成了一个2-sat的问题了(这两个点不能同时连)。 A代表连s1,A‘连s2..
所以如果A连s1,B连s1,距离大于最大值 A-B',B-A'.
A:S2 ,B:S2 A'-B,B'-A A:S1,B:S2 A-B,B'-A' A:S2,B:S1 A'-B',B-A..
讨厌和喜欢也按照2-SAT的理论连一下。。然后tarjan跑一遍,看A和A’是否在一个强连通分量里,如果在的话代表同时选了不符合,所以要怎个二分值。否则减小。。
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stack>
#include<vector>
#include<cmath>
using namespace std;
const int MAXN=2010;
const int MAXE=800010;
int head[MAXN],size;
struct EDGE
{
int v,next;
}edge[MAXE];
struct Node
{
int x,y;
}p[MAXN],hate[MAXN],like[MAXN];
int pre[MAXN],low[MAXN],sccno[MAXN],dfs_clock,scc_cnt;
stack<int> S;
void init()
{
memset(head,-1,sizeof(head));
memset(pre,0,sizeof(pre));
memset(sccno,0,sizeof(sccno));
dfs_clock=scc_cnt=size=0;
}
void add_edge(int u,int v)
{
edge[size].v=v;
edge[size].next=head[u];
head[u]=size++;
}
void tarjan(int u)
{
pre[u]=low[u]=++dfs_clock;
S.push(u);
for(int i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].v;
if(!pre[v])
{
tarjan(v);
low[u]=min(low[u],low[v]);
}
else if(!sccno[v])
low[u]=min(low[u],pre[v]);
}
if(low[u]==pre[u])
{
scc_cnt++;
while(1)
{
int x=S.top();
S.pop();
sccno[x]=scc_cnt;
if(x==u)
break;
}
}
}
int n,a,b,dis[MAXN],slen;
int dist(Node aa,Node bb)
{
return abs(aa.x-bb.x)+abs(aa.y-bb.y);
}
void build(int mid)
{
init();
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
if(dis[i]+dis[j]>mid)
{
add_edge(i,j+n);
add_edge(j,i+n);
}
if(dis[i+n]+dis[j+n]>mid)
{
add_edge(i+n,j);
add_edge(j+n,i);
}
if(dis[i]+slen+dis[j+n]>mid)
{
add_edge(i,j);
add_edge(j+n,i+n);
}
if(dis[i+n]+slen+dis[j]>mid)
{
add_edge(i+n,j+n);
add_edge(j,i);
}
}
}
for(int i=0;i<a;i++)
{
add_edge(hate[i].x,hate[i].y+n);
add_edge(hate[i].y+n,hate[i].x);
add_edge(hate[i].x+n,hate[i].y);
add_edge(hate[i].y,hate[i].x+n);
}
for(int i=0;i<b;i++)
{
add_edge(like[i].x,like[i].y);
add_edge(like[i].y,like[i].x);
add_edge(like[i].x+n,like[i].y+n);
add_edge(like[i].y+n,like[i].x+n);
}
}
bool test()
{
int u,v,i;
for(i=0;i<2*n;i++)
if(!pre[i])
tarjan(i);
for(u=0;u<n;u++)
{
if(sccno[u]==sccno[u+n])
return 0;
}
return 1;
}
int main()
{
int i,j;
while(scanf("%d%d%d",&n,&a,&b)!=EOF)
{
Node s1,s2,tmp;
scanf("%d%d%d%d",&s1.x,&s1.y,&s2.x,&s2.y);
slen=dist(s1,s2);
for(i=0;i<n;i++)
{
scanf("%d%d",&tmp.x,&tmp.y);
dis[i]=dist(tmp,s1);
dis[i+n]=dist(tmp,s2);
}
for(i=0;i<a;i++)
{
scanf("%d%d",&hate[i].x,&hate[i].y);
hate[i].x--;
hate[i].y--;
}
for(i=0;i<b;i++)
{
scanf("%d%d",&like[i].x,&like[i].y);
like[i].x--;
like[i].y--;
}
int l=0,r=8000000;
int ans=-1;
while(l<=r)
{
int mid=(l+r)>>1;
build(mid);
if(test())
{
ans=mid;
r=mid-1;
}
else
l=mid+1;
}
printf("%d\n",ans);
}
return 0;
}