Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 6091 | Accepted: 2046 |
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
Source
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <stack> using namespace std; const int MAX_N = ;
int N,M,A,B;
bool fri[MAX_N][MAX_N],hate[MAX_N][MAX_N];
int low[MAX_N * ],pre[MAX_N * ],cmp[MAX_N * ];
int dfs_clock,scc_cnt;
int x[MAX_N],y[MAX_N];
stack<int> S;
vector<int> G[ * MAX_N]; void dfs(int u) {
low[u] = pre[u] = ++dfs_clock;
S.push(u);
for(int i = ; i < G[u].size(); ++i) {
int v = G[u][i];
if(!pre[v]) {
dfs(v);
low[u] = min(low[u],low[v]);
} else if(!cmp[v]) {
low[u] = min(low[u],pre[v]);
}
} if(low[u] == pre[u]) {
++scc_cnt;
for(;;) {
int x = S.top(); S.pop();
cmp[x] = scc_cnt;
if(x == u) break;
}
}
} bool scc() {
dfs_clock = scc_cnt = ;
memset(cmp,,sizeof(cmp));
memset(pre,,sizeof(pre)); for(int i = ; i <= * N + ; ++i) if(!pre[i]) dfs(i); for(int i = ; i <= N + ; ++i) {
if(cmp[i] == cmp[N + i]) return false;
}
return true;
} int dis(int i,int j) {
return abs(x[i] - x[j]) + abs(y[i] - y[j]);
} void build(int x) {
for(int i = ; i <= * N + ; ++i) {
G[i].clear();
} for(int i = ; i <= N + ; ++i) {
for(int j = i + ; j <= N + ; ++j) {
if(fri[i][j]) {
G[i].push_back(j);
G[i + N].push_back(j + N);
G[j].push_back(i);
G[j + N].push_back(i + N);
}
if(hate[i][j]) {
G[i].push_back(j + N);
G[j].push_back(i + N);
G[j + N].push_back(i);
G[i + N].push_back(j);
} if(dis(i,) + dis(j,) > x) {
G[i].push_back(j + N);
G[j].push_back(i + N);
}
if(dis(i,) + dis(j,) > x) {
G[i + N].push_back(j);
G[j + N].push_back(i);
}
if(dis(i,) + dis(j,) + dis(,) > x) {
G[i + N].push_back(j + N);
G[j].push_back(i);
}
if(dis(i,) + dis(j,) + dis(,)> x) {
G[i].push_back(j);
G[j + N].push_back(i + N);
}
}
} }
void solve() {
int l = ,r = 12e6 + ; //printf("r = %d\n",r); while(l < r) {
int mid = (l + r) / ;
build(mid);
if(scc()) r = mid;
else l = mid + ;
}
build(l);
if(scc())
printf("%d\n",l);
else
printf("-1\n");
} int main()
{
//freopen("sw.in","r",stdin);
scanf("%d%d%d",&N,&A,&B);
for(int i = ; i <= N + ; ++i) {
scanf("%d%d",&x[i],&y[i]);
} for(int i = ; i <= A; ++i) {
int a,b;
scanf("%d%d",&a,&b);
hate[a + ][b + ] = ;
} for(int i = ; i <= B; ++i) {
int a,b;
scanf("%d%d",&a,&b);
fri[a + ][b + ] = ;
} solve(); return ;
}