POJ2749 Building roads

来源:互联网 发布:loadrunner11 java 编辑:程序博客网 时间:2024/06/10 17:20

Building roads

Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 7953 Accepted: 2697

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

53246

Source

POJ Monthly–2006.01.22,zhucheng


题目意思是给出2个谷仓坐标和n头牛坐标,每头牛要选择一个个谷仓,然后牛之间有喜好关系,朋友必须分在一个仓,敌人必须分开,求两头牛到经过谷仓相连的最远曼哈顿距离的最小值
思路:首先二分最远距离然后用2-sat判断可行性

#include <iostream>  #include <cstdio>  #include <cstring>  #include <string>  #include <algorithm>  #include <cmath>  #include <map>  #include <set>  #include <stack>  #include <queue>  #include <vector>  #include <bitset>  using namespace std;#define LL long long  const int INF = 0x3f3f3f3f;#define MAXN 100100  #define MAXM 5000100  struct node{    int u, v, next;} edge[MAXM];int dfn[MAXN], low[MAXN], s[MAXN], Stack[MAXN], in[MAXN], block[MAXN], dis[MAXN];int cnt, tot, index, bnum, ctt;void init(){    memset(s, -1, sizeof s);    memset(dfn, 0, sizeof dfn);    memset(low, 0, sizeof low);    memset(Stack, 0, sizeof Stack);    memset(in, 0, sizeof in);    memset(block, 0, sizeof block);    cnt = tot = index = bnum = 0;}void add(int u, int v){    edge[cnt].u = u;    edge[cnt].v = v;    edge[cnt].next = s[u];    s[u] = cnt++;}void tarjan(int x){    dfn[x] = low[x] = ++tot;    Stack[++index] = x;    in[x] = 1;    for (int i = s[x]; ~i; i = edge[i].next)    {        int v = edge[i].v;        if (!dfn[v])        {            tarjan(v);            low[x] = min(low[x], low[v]);        }        else if (in[v])        {            low[x] = min(low[x], low[v]);        }    }    if (dfn[x] == low[x])    {        bnum++;        do        {            //printf("%d ",Stack[index]);              block[Stack[index]] = bnum;            in[Stack[index--]] = 0;        } while (Stack[index + 1] != x);        // printf("\n");      }}bool solve(){    for (int i = 0; i < ctt; i++)        if (!dfn[i])            tarjan(i);    for (int i = 0; i < ctt; i += 2)    {        if (block[i] == block[i ^ 1])            return 0;    }    return 1;}int calcdis(int x, int y, int xx, int yy){    return fabs(xx - x) + fabs(yy - y);}int main(){    int  n, m1, m2, a, b;    int xx1, xx2, yy1, yy2;    int x[100005], y[100005];    while (~scanf("%d%d%d", &n, &m1, &m2))    {        scanf("%d%d%d%d", &xx1, &yy1, &xx2, &yy2);        ctt = 2 * n;        for (int i = 0; i < n; i++)        {            scanf("%d%d", &a, &b);            dis[2 * i] = calcdis(a, b, xx1, yy1);            dis[2 * i + 1] = calcdis(a, b, xx2, yy2);        }        for (int i = 0; i < m1; i++)        {            scanf("%d%d", &x[i], &y[i]);            x[i]--,y[i]--;        }        for (int i = m1; i < m1+m2; i++)        {            scanf("%d%d", &x[i], &y[i]);            x[i]--,y[i]--;        }        int dss=calcdis(xx1,yy1,xx2,yy2);        int l = 1, r = 100000000, ans = -1;        while (l <= r)        {            init();            int mid = (l + r) / 2;            for(int i=0;i<n;i++)                for (int j = i + 1; j < n; j++)                {                    int nx = i*2, ny = j*2;                    if (dis[nx] + dis[ny] > mid)                    {                        add(nx, ny ^ 1);                        add(ny, nx ^ 1);                    }                    if(dis[nx^1]+dis[ny^1]>mid)                    {                        add(nx^1,ny);                        add(ny^1,nx);                    }                    if(dis[nx]+dis[ny^1]+dss>mid)                    {                    add(nx,ny);                    add(ny^1,nx^1);                    }                    if(dis[nx^1]+dis[ny]+dss>mid)                    {                        add(ny,nx);                        add(nx^1,ny^1);                    }                }                for (int i = 0; i < m1; i++)                {                    int a=x[i],b=y[i];                    add(2 * a, 2 * b + 1);                    add(2 * b, 2 * a + 1);                    add(2 * a + 1, 2 * b);                    add(2 * b + 1, 2 * a);                }                for (int i = m1; i < m2+m1; i++)                {                    int a=x[i],b=y[i];                    add(2 * a, 2 * b);                    add(2 * b, 2 * a);                    add(2 * a + 1, 2 * b + 1);                    add(2 * b + 1, 2 * a + 1);                }                if (solve()) r = mid - 1, ans = mid;                else l = mid + 1;        }        printf("%d\n", ans);    }    return 0;}
原创粉丝点击