Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 6718 | Accepted: 2263 |
Description
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
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
Sample Input
4 1 1 12750 28546 15361 32055 6706 3887 10754 8166 12668 19380 15788 16059 3 4 2 3
Sample Output
53246
自己AC的,思路 + 代码 半小时多点,过了测试数据提交后结果WA了,然后调试半个多小时才过。本来可以1A的,都怪自己WA的好天真,无语死了。一直认为某一个农场只会和一个农场有矛盾或者关系友好,最后才发现这个bug。看来是做前两个题做晕了。
题意:约翰有N个不相互连通的农场,为了方便各个农场里面的牛互相串门,他需要连通这N个农场。为了减少花销,他建立了两个流动站点S1和S2且增加了一条S1到S2的道路。对于每一个农场,要么它连着S1,要么它连着S2,不会存在都连的或者都不连的。但是现在有A个农场相互仇视,B个农场关系友好,约翰必须保证互相仇视的不能连着同一个流动站点,关系友好的必须连到同一个站点。每两个农场之间都有一个距离值,我们取所有距离值里面的最大值为DD,现在为了节约花销问你能否使DD足够小,若能输出这个结果,否则输出-1。
算法实现:二分查找 + tarjan求SCC //关于二分的实现这里就不说了
建图思路:对任意的农场i,用i表示它连S1,i + N表示它连S2。这里我用的是容器vector建图的
一:x,y相互仇视
1,x连S1,y连S2;G[x].push_back(y + N);
2,y连S1,x连S2;G[y].push_back(x + N);
3,x连S2,y连S1;G[x + N].push_back(y);
4,y连S2,x连S1;G[y + N].push_back(x);
二:x,y关系友好
1,x连S1,y连S1;G[x].push_back(y);
2,y连S1,x连S1;G[y].push_back(x);
3,x连S2,y连S2;G[x + N].push_back(y + N);
4,y连S2,x连S2;G[y + N].push_back(x + N);
三:对于每次查找的结果now 这里用D表示S1,S2间距离
1,i农场 和 j农场不能同时连S1 即 (i农场到S1距离 + j农场到S1距离 > now)
G[i].push_back(j + N);//i连S1 j连S2
G[j].push_back(i + N);//j连S1 i连S2
2,i农场连S1 和 j农场连S2 不能兼得 即 (i农场到S1距离 + j农场到S2距离 + D > now)
G[i].push_back(j);//i连S1 j连S1
G[j + N].push_back(i + N);//j连S2 i连S2
3,i农场连S2 和 j农场连S1 不能兼得 即 (i农场到S2距离 + j农场到S1距离 + D > now)
G[i + N].push_back(j + N);//i连S2 j连S2
G[j].push_back(i);//j连S1 i连S1
4,i农场 和 j农场不能同时连S2 即 (i农场到S2距离 + j农场到S2距离 > now)
G[i + N].push_back(j);//i连S2 j连S1
G[j + N].push_back(i);//j连S1 i连S2
AC代码:在hdoj也过了
#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#define MAXN 1000+10
#define INF 10000000
#define eps 1e-5
using namespace std;
struct rec
{
int d1, d2;//存储当前农场到 S1、S2的距离
}num[MAXN];
struct TT
{
int a, b;
}hate[MAXN], Friend[MAXN];//记录仇恨 和 朋友关系对
vector<int> G[MAXN];
int low[MAXN], dfn[MAXN];
int dfs_clock;
int sccno[MAXN], scc_cnt;
stack<int> S;
bool Instack[MAXN];
int sx1, sy1, sx2, sy2;//S1 S2坐标
int D;//S1 和 S2距离
int N, A, B;
//int hate[MAXN], Friend[MAXN];
void init()
{
for(int i = 1; i <= 2*N; i++) G[i].clear();
}
int dis(int x1, int y1, int x2, int y2)
{
return abs(x1 - x2) + abs(y1 - y2);
}
void input()
{
int x, y;
scanf("%d%d%d%d", &sx1, &sy1, &sx2, &sy2);
D = dis(sx1, sy1, sx2, sy2);
for(int i = 1; i <= N; i++)
{
scanf("%d%d", &x, &y);
num[i].d1 = dis(x, y, sx1, sy1);//到S1距离
num[i].d2 = dis(x, y, sx2, sy2);//到S2距离
}
memset(hate, -1, sizeof(hate));
memset(Friend, -1, sizeof(Friend));
for(int i = 0; i < A; i++)//仇人
scanf("%d%d", &hate[i].a, &hate[i].b);
for(int i = 0; i < B; i++)//朋友
scanf("%d%d", &Friend[i].a, &Friend[i].b);
}
bool judge(int a, int p1, int b, int p2, int now)//大于当前二分查找的 距离
{
if(p1 == 1 && p2 == 1)
return num[a].d1 + num[b].d1 > now;
if(p1 == 1 && p2 == 2)
return num[a].d1 + num[b].d2 + D > now;
if(p1 == 2 && p2 == 1)
return num[a].d2 + num[b].d1 + D > now;
if(p1 == 2 && p2 == 2)
return num[a].d2 + num[b].d2 > now;
}
void getMap(int now)
{
for(int i = 1; i <= N; i++)
{
for(int j = 1; j < i; j++)
{
if(judge(i, 1, j, 1, now))//i农场 和 j农场不能同时连S1
{
G[i].push_back(j + N);//i连S1 j连S2
G[j].push_back(i + N);//j连S1 i连S2
}
if(judge(i, 1, j, 2, now))//i农场连S1 和 j农场连S2 不能兼得
{
G[i].push_back(j);//i连S1 j连S1
G[j + N].push_back(i + N);//j连S2 i连S2
}
if(judge(i, 2, j, 1, now))//i农场连S2 和 j农场连S1 不能兼得
{
G[i + N].push_back(j + N);//i连S2 j连S2
G[j].push_back(i);//j连S1 i连S1
}
if(judge(i, 2, j, 2, now))//i农场 和 j农场不能同时连S2
{
G[i + N].push_back(j);//i连S2 j连S1
G[j + N].push_back(i);//j连S1 i连S2
}
}
}
//仇恨关系
for(int i = 0; i < A; i++)
{
int x = hate[i].a;
int y = hate[i].b;
G[x].push_back(y + N);
G[y].push_back(x + N);
G[x + N].push_back(y);
G[y + N].push_back(x);
}
//友好关系
for(int i = 0; i < B; i++)
{
int x = Friend[i].a;
int y = Friend[i].b;
G[x].push_back(y);
G[y].push_back(x);
G[x + N].push_back(y + N);
G[y + N].push_back(x + N);
}
}
void tarjan(int u, int fa)//求SCC
{
int v;
low[u] = dfn[u] = ++dfs_clock;
S.push(u);
Instack[u] = true;
for(int i = 0; i < G[u].size(); i++)
{
v = G[u][i];
if(!dfn[v])
{
tarjan(v, u);
low[u] = min(low[u], low[v]);
}
else if(Instack[v])
low[u] = min(low[u], dfn[v]);
}
if(low[u] == dfn[u])
{
scc_cnt++;
for(;;)
{
v = S.top(); S.pop();
Instack[v] = false;
sccno[v] = scc_cnt;
if(v == u) break;
}
}
}
void find_cut(int l, int r)
{
memset(low, 0, sizeof(low));
memset(dfn, 0, sizeof(dfn));
memset(sccno, 0, sizeof(sccno));
memset(Instack, false, sizeof(Instack));
dfs_clock = scc_cnt = 0;
for(int i = l; i <= r; i++)
if(!dfn[i]) tarjan(i, -1);
}
bool two_sat()//判断当前解是否可行
{
for(int i = 1; i <= N; i++)
{
if(sccno[i] == sccno[i + N])
return false;
}
return true;
}
void solve()
{
int left, right, mid, ans;
left = 0; ans = right = INF;
while(right >= left)
{
//printf("1\n");
mid = (left + right) / 2;
init();//每次初始化
getMap(mid);
//printf("%d\n", mid);
find_cut(1, 2*N);
if(two_sat())
{
ans = mid;
right = mid - 1;//一开始更新 反了 无语。。。
}
else left = mid + 1;
}
if(ans == INF)
printf("-1\n");
else
printf("%d\n", ans);
}
int main()
{
while(scanf("%d%d%d", &N, &A, &B) != EOF)
{
input();
solve();
}
return 0;
}