2-sat->poj 2479 Building roads

来源:互联网 发布:mac上安装ipad应用 编辑:程序博客网 时间:2024/06/05 11:50
Building roads
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 5904 Accepted: 1984

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 112750 28546 15361 320556706 388710754 816612668 1938015788 160593 42 3

Sample Output

53246
#include <iostream>#include <cstdio>#include <cmath>#include <stack>#include <string.h>#include <algorithm>using namespace std;#define MAXN 4000000 + 10#define MAXV 10000 + 10#define INF 2000000000int n, a, b;struct Edge{int v, next;}edge[MAXN];struct Node{int x, y;}s1, s2, temp[MAXV];struct conflict{int u, v;}A[MAXV], B[MAXV];int head[MAXV], e, dis, cnt, ind;int dfn[MAXV], low[MAXV], belong[MAXV];bool ins[MAXV];void add(int u, int v){edge[e].v = v;edge[e].next = head[u];head[u] = e++;}void init(){e = cnt = ind = 0;dis = (int)(abs(s1.x - s2.x) + abs(s1.y - s2.y));memset(head, -1, sizeof(head));memset(dfn, 0, sizeof(dfn));memset(low, 0, sizeof(low));memset(ins, false, sizeof(ins));memset(belong, 0, sizeof(belong));}stack <int> s;void tarjan(int u){dfn[u] = low[u] = ++ind;ins[u] = true;s.push(u);for (int i = head[u]; i != -1; i = edge[i].next){int v = edge[i].v;if (!dfn[v]){tarjan(v);low[u] = min(low[u], low[v]);}else if (ins[v]){low[u] = min(low[u], dfn[v]);}}if (low[u] == dfn[u]){cnt++;int x;do{x = s.top();s.pop();ins[x] = false;belong[x] = cnt;}while (x != u);}}int distanceto(Node a, Node b){return (int)(abs(a.x - b.x) + abs(a.y - b.y));}bool is_true(){for (int i = 1; i <= 2 * n; i++){if (!dfn[i]){tarjan(i);}}for (int i = 1; i <= n; i++){if (belong[i] == belong[i + n]){return false;}}return true;}void solve(){int low = 0, high = INF;int ans = INF;bool flag = false;while (low <= high){int mid = low + (high - low) / 2;init();for (int i = 0; i < a; i++){add(A[i].u, A[i].v + n);add(A[i].v + n, A[i].u);add(A[i].u + n, A[i].v);add(A[i].v, A[i].u + n);}for (int i = 0; i < b; i++){add(B[i].u, B[i].v);add(B[i].v, B[i].u);add(B[i].u + n, B[i].v + n);add(B[i].v + n, B[i].u + n);}for (int i = 1; i <= n; i++){for (int j = i + 1; j <= n; j++){if (distanceto(temp[i], s1) + distanceto(s1, temp[j]) > mid){add(i, j + n);add(j, i + n);}if (distanceto(temp[i], s2) + distanceto(s2, temp[j]) > mid){add(i + n, j);add(j + n, i);}if (distanceto(temp[i], s1) + distanceto(s2, temp[j]) + dis > mid){add(i, j);add(j + n, i + n);}if (distanceto(temp[i], s2) + distanceto(s1, temp[j]) + dis > mid){add(i + n, j + n);add(j, i);}}}if (is_true()){flag = true;ans = min(mid, ans);high = mid - 1;}else{low = mid + 1;}}if (!flag){cout << -1 << endl;}else{cout << ans << endl;}}void input(){int u, v;init();cin >> n >> a >> b;cin >> s1.x >> s1.y >> s2.x >> s2.y;for (int i = 1; i <= n; i++){cin >> temp[i].x >> temp[i].y;}for (int i = 0; i < a; i++){cin >> u >> v;A[i].u = u;A[i].v = v;}for (int i = 0; i < b; i++){cin >> u >> v;B[i].u = u;B[i].v = v;}solve();}int main(){input();return 0;}


原创粉丝点击