poj2749 Building roads

网友投稿 319 2022-09-10


poj2749 Building roads

Building roads
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 7824   Accepted: 2659

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

分析:每个点要么和1号传送点连,要么和2号传送点连,要求任意两个点的最大距离最小,这就是典型的二分+2-SAT.二分一下最大长度,这就是限制条件了,根据限制条件来加边,最后tarjan判断是否可行。我们用i表示连1号点,i + n表示连2号点,为什么这么连边看代码注释:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <stack>

using namespace std;

const int maxn = 110010, maxm = 1100010;

int n, a, b,pre[maxn],low[maxn],scc[maxn],dfs_clock,top;
int hate[maxn][2], like[maxn][2], head[maxn], to[maxm], nextt[maxm], tot = 1;
int sx1, sy1, sx2, sy2,d,d1[maxn],ans,d2[maxn],top1,top2,r = 0,l = 0;

void add(int x, int y)
{
    to[tot] = y;
    nextt[tot] = head[x];
    head[x] = tot++;
}

int dis(int x1, int y1, int x2, int y2)
{
    return abs(x2 - x1) + abs(y2 - y1);
}

stack <int> s;

void tarjan(int u)
{
    pre[u] = low[u] = ++dfs_clock;
    s.push(u);
    for (int i = head[u]; i; i = nextt[i])
    {
        int v = to[i];
        if (!pre[v])
        {
            tarjan(v);
            low[u] = min(low[u], low[v]);
        }
        else
            if (!scc[v])
                low[u] = min(low[u], pre[v]);
    }
    if (pre[u] == low[u])
    {
        top++;
        while (1)
        {
            int t = s.top();
            s.pop();
            scc[t] = top;
            if (t == u)
                break;
        }
    }
}

bool check(int maxx)
{
    memset(head, 0, sizeof(head));
    tot = 1;
    memset(pre, 0, sizeof(pre));
    memset(scc, 0, sizeof(scc));
    memset(low, 0, sizeof(low));
    dfs_clock = 0;
    top = 0;
    for (int i = 1; i <= n; i++)
        for (int j = i + 1; j <= n; j++)
        {
        if (d1[i] + d1[j] > maxx)
        {
            add(i, j + n);  //i在1号点j就必须在2号点
            add(j, i + n);
        }
        if (d2[i] + d2[j] > maxx)
        {
            add(i + n, j); //i在2号点j就必须在1号点
            add(j + n, i);
        }
        if (d1[i] + d2[j] + d > maxx)
        {
            add(i, j);   //i在1,j在2的时候不满足要求,那么当i在1的时候,j必须在1
            add(j + n, i + n);
        }
        if (d1[j] + d2[i] + d > maxx)
        {
            add(j, i); 
            add(i + n, j + n);
        }
        }
    for (int i = 1; i <= a; i++)
    {
        add(hate[i][0], hate[i][1] + n);
        add(hate[i][1] + n, hate[i][0]);
        add(hate[i][1], hate[i][0] + n);
        add(hate[i][0] + n, hate[i][1]);
    }
    for (int i = 1; i <= b; i++)
    {
        add(like[i][0], like[i][1]);
        add(like[i][1], like[i][0]);
        add(like[i][1] + n, like[i][0] + n);
        add(like[i][0] + n, like[i][1] + n);
    }
    for (int i = 1; i <= 2 * n; i++)
        if (!pre[i])
            tarjan(i);
    for (int i = 1; i <= n; i++)
        if (scc[i] == scc[i + n])
            return false;
    return true;
}

int main()
{
    while (~scanf("%d%d%d", &n, &a, &b))
    {
        scanf("%d%d%d%d", &sx1, &sy1, &sx2, &sy2);
        d = dis(sx1, sy1, sx2, sy2);
        for (int i = 1; i <= n; i++)
        {
            int x, y;
            scanf("%d%d", &x, &y);
            d1[i] = dis(sx1, sy1, x, y);
            d2[i] = dis(sx2, sy2, x, y);
            r = max(r, max(d1[i], d2[i]));
        }
        r = r * 2 + d;
        for (int i = 1; i <= a; i++)
            scanf("%d%d", &hate[i][0], &hate[i][1]);
        for (int i = 1; i <= b; i++)
            scanf("%d%d", &like[i][0], &like[i][1]);
        ans = -1;
        while (l <= r)
        {
            int mid = (l + r) >> 1;
            if (check(mid))
            {
                r = mid - 1;
                ans = mid;
            }
            else
                l = mid + 1;
        }
        printf("%d\n", ans);
    }

    return 0;
}

 


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:poj3678 Katu Puzzle
下一篇:JPA&nbsp;like&nbsp;模糊查询&nbsp;语法格式解析
相关文章

 发表评论

暂时没有评论,来抢沙发吧~