Building roads
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 6123 | Accepted: 2051 |
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|.
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].
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
题意:
给出 N(2 ~ 500),A(0 ~ 1000),B(0 ~ 1000),代表有 N 个点,A 代表有厌恶关系,B 代表喜爱关系。还给出两个点s1,s2的坐标 x1,y1,x2,y2。现要把这 N 个点连接,要么连在 s1 上,要么连在 s2 上。喜爱关系代表这两个点必须连在同一个地方,厌恶关系代表必须连在不同的地方。现要使任意两点间距离最大值最小化。输出这个值。
思路:
2 - sat + 二分。二分枚举距离 d,若任意两点间距离大于 d,则可以建立一种确定关系。对于没关系的两点也要判断能不能建立一种确定关系。
AC:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <stack>
using namespace std;
const int NMAX = 505 * 2;
const int INF = 4000005 * 3;
int n, sx1, sy1, sx2, sy2;
int dd, a, b;
int x[NMAX], y[NMAX], like[1005][10], hate[1005][10];
vector<int> G[NMAX];
int dfs_clock, scc_cnt;
int pre[NMAX], cmp[NMAX], low[NMAX];
stack<int> s;
void dfs(int u) {
pre[u] = low[u] = ++dfs_clock;
s.push(u);
for (int i = 0; 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 (pre[u] == low[u]) {
++scc_cnt;
for( ;;) {
int x = s.top(); s.pop();
cmp[x] = scc_cnt;
if (x == u) break;
}
}
}
void scc() {
dfs_clock = scc_cnt = 0;
memset(pre, 0, sizeof(pre));
memset(cmp, 0, sizeof(cmp));
for (int i = 1; i <= 2 * n; ++i)
if (!pre[i]) dfs(i);
}
int dis(int xa, int ya, int xb, int yb) {
int xx = (xa > xb ? xa - xb : xb - xa);
int yy = (ya > yb ? ya - yb : yb - ya);
return (xx + yy);
}
bool test() {
for (int i = 1; i <= n; ++i)
if (cmp[i] == cmp[i + n]) return false;
return true;
}
void make(int d) {
for (int i = 1; i <= 2 * n; ++i)
G[i].clear();
for (int i = 1; i <= n - 1; ++i) {
for (int j = i + 1; j <= n; ++j) {
int is1 = dis(x[i], y[i], sx1, sy1);
int js1 = dis(x[j], y[j], sx1, sy1);
int is2 = dis(x[i], y[i], sx2, sy2);
int js2 = dis(x[j], y[j], sx2, sy2);
if (is1 + js1 > d) {
G[i].push_back(j + n);
G[j].push_back(i + n);
}
if (is2 + js2 > d) {
G[i + n].push_back(j);
G[j + n].push_back(i);
}
if (is1 + dd + js2 > d) {
G[i].push_back(j);
G[j + n].push_back(i + n);
}
if (is2 + dd + js1 > d) {
G[i + n].push_back(j + n);
G[j].push_back(i);
}
}
}
for (int i = 1; i <= a; ++i) {
int p = hate[i][1], q = hate[i][2];
G[p + n].push_back(q);
G[q + n].push_back(p);
G[q].push_back(p + n);
G[p].push_back(q + n);
}
for (int i = 1; i <= b; ++i) {
int p = like[i][1], q = like[i][2];
G[p + n].push_back(q + n);
G[q + n].push_back(p + n);
G[p].push_back(q);
G[q].push_back(p);
}
}
bool C(int d) {
make(d);
scc();
if ( !test() ) return false;
return true;
}
int main() {
scanf("%d%d%d", &n, &a, &b);
scanf("%d%d%d%d", &sx1, &sy1, &sx2, &sy2);
for (int i = 1; i <= n; ++i)
scanf("%d%d", &x[i], &y[i]);
for (int i = 1; i <= a; ++i)
scanf("%d%d", &hate[i][1], &hate[i][2]);
for (int i = 1; i <= b; ++i)
scanf("%d%d", &like[i][1], &like[i][2]);
dd = dis(sx1, sy1, sx2, sy2);
int l = 0, r = INF;
while (r - l > 1) {
int mid = l + (r - l) / 2;
if (C(mid)) r = mid;
else l = mid;
}
if (C(r)) printf("%d\n", r);
else printf("-1\n");
return 0;
}