Building roads
Time Limit: 10000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 208 Accepted Submission(s): 66
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
Source
POJ Monthly - 2006.01.22 - zhucheng
Recommend
威士忌
题目: http://acm.hdu.edu.cn/showproblem.php?pid=1815
题意:给你n个牧场,现在有两个传送门,每个牧场必须连接到一个传送门,也只能连一个,问怎么连使最大的距离最小,有些牧场不能连同一个,有的则必须连同一个,所有距离都是哈密顿距离。。。
分析:看到这题首先想到的就是2-sat,对于不能在一起的建对边,在一起的建同边,然后用拓扑排序枚举所有情况,取最小值。。。。
不过这样做太烦了,其实我们可以二分答案,然后对于两个不可达的点,再建相应的边就行了。。。
PS:每次敲完代码,然后本地就wa了,我就会乱了阵脚,这种情况下,只有从新思考方法的正确性才是王道啊,查模板的错误就是个错误= =
这题中间细节没注意导致我查模板查半天T_T
代码:
#include
#include
using namespace std;
const int mm=3333333;
const int mn=1111;
int ver[mm],next[mm];
int head[mn],dfn[mn],low[mn],q[mn],id[mn];
int d[mn],ua[mn],va[mn],ub[mn],vb[mn];
int i,j,n,l,r,m,a,b,idx,top,cnt,edge,x,y,x1,y1,x2,y2,ans,dd;
void add(int u,int v)
{
ver[edge]=v,next[edge]=head[u],head[u]=edge++;
}
void dfs(int u)
{
dfn[u]=low[u]=++idx;
q[top++]=u;
for(int i=head[u],v;i>=0;i=next[i])
if(!dfn[v=ver[i]])
dfs(v),low[u]=min(low[u],low[v]);
else if(!id[v])low[u]=min(low[u],dfn[v]);
if(dfn[u]==low[u])
{
id[u]=++cnt;
while(q[--top]!=u)id[q[top]]=cnt;
}
}
void Tarjan()
{
for(idx=cnt=top=i=0;i<=n+n;++i)dfn[i]=id[i]=0;
for(i=1;i<=n+n;++i)
if(!dfn[i])dfs(i);
}
bool ok()
{
Tarjan();
for(i=1;i<=n+n;i+=2)
if(id[i]==id[i+1])return 0;
return 1;
}
int abc(int x)
{
return x<0?-x:x;
}
int main()
{
while(~scanf("%d%d%d",&n,&a,&b))
{
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
for(i=1;i<=n;++i)
{
scanf("%d%d",&x,&y);
d[i*2-1]=abc(x1-x)+abc(y1-y);
d[i*2]=abc(x2-x)+abc(y2-y);
}
for(i=0;i>1;
for(edge=i=0;i<=n+n;++i)head[i]=-1;
for(i=0;im)
{
add(i*2-1,j*2);
add(j*2-1,i*2);
}
if(d[i*2-1]+d[j*2]+dd>m)
{
add(i*2-1,j*2-1);
add(j*2,i*2);
}
if(d[i*2]+d[j*2-1]+dd>m)
{
add(i*2,j*2);
add(j*2-1,i*2-1);
}
if(d[i*2]+d[j*2]>m)
{
add(i*2,j*2-1);
add(j*2,i*2-1);
}
}
if(ok())ans=m,r=m-1;
else l=m+1;
}
if(ans>=1e8)ans=-1;
printf("%d\n",ans);
}
return 0;
}