POJ 2749|Building roads|2-SAT|二分答案

21 篇文章 0 订阅
11 篇文章 0 订阅

题目大意(下有中文翻译)

已知两点间距离,每个点只能连一条边到预设点s1或s2。其中某些点对不能连同一个预设点,某些点必须连到同一个预设点,最小化两点间距的最大值。

题解

注意到相容相斥关系,考虑2-SAT。注意到最小化最大值,考虑二分答案。

那么如果两点连到预设点后两点距离超过我们二分的答案,那么显然这么连是不行的,所以两点间距的条件也转化为了相容相斥的逻辑关系,同样可以用2-SAT解决。

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
#define rep(i,j,k) for(i=j;i<k;++i)
#define ms(i) memset(i,0,sizeof(i))
const int N = 20005, M = 600000;
struct Point{ int x, y; void read() { scanf("%d%d", &x, &y); } } s1, s2, barn[N], hate[N], like[N];
int dis(Point a, Point b) { return abs(a.x - b.x) + abs(a.y - b.y); }
int d[N], y[N], dfn[N], low[N], belong[N];
int h[N], p[M], v[M], vis[N], stk[N], top, ts, cnt, scc;
void add(int a, int b) {
    p[++cnt] = h[a]; v[cnt] = b; h[a] = cnt;
}
void add(int a, int x, int b, int y) {
    a = 2 * a + x; b = 2 * b + y;
    p[++cnt] = h[a ^ 1]; v[cnt] = b; h[a ^ 1] = cnt;
    p[++cnt] = h[b ^ 1]; v[cnt] = a; h[b ^ 1] = cnt;
}

void tarjan(int x) {
    int i;
    dfn[x] = low[x] = ++ts;
    vis[x] = 1; stk[++top] = x;
    for (i = h[x]; i; i = p[i])
        if (!dfn[v[i]]) {
            tarjan(v[i]);
            low[x] = min(low[v[i]], low[x]);
        } else if (vis[v[i]])
            low[x] = min(dfn[v[i]], low[x]);
    if (dfn[x] == low[x]) {
        ++scc;
        do {
            i = stk[top--];
            vis[i] = 0;
            belong[i] = scc;
        } while (i != x);
    }
}

int main() {
    int n, m, i, j, s, kase = 0, H, L, l, r, mid, ans;
    while (scanf("%d%d%d", &n, &H, &L) == 3) {
        s1.read(); s2.read(); s = dis(s1, s2);
        rep(i,0,n) barn[i].read(), d[i] = dis(barn[i], s1), d[i + n] = dis(barn[i], s2);
        rep(i,0,H) hate[i].read(), --hate[i].x, --hate[i].y;
        rep(i,0,L) like[i].read(), --like[i].x, --like[i].y;
        l = 0, r = 8000000;
        while (l <= r) {
            mid = l + r >> 1;
            ms(h); ms(dfn); scc = cnt = top = ts = 0;

            rep(i,0,n) rep(j,i+1,n) if (i != j) {
                if (d[i] + d[j] > mid) add(i, 0, j, 0);
                if (d[i] + d[j + n] + s > mid) add(i, 0, j, 1);
                if (d[i + n] + d[j] + s > mid) add(i, 1, j, 0);
                if (d[i + n] + d[j + n] > mid) add(i, 1, j, 1);
            }
            rep(i,0,H) add(hate[i].x, 0, hate[i].y, 0), add(hate[i].x, 1, hate[i].y, 1);
            rep(i,0,L) add(like[i].x, 0, like[i].y, 1), add(like[i].x, 1, like[i].y, 0);

            rep(i,0,2*n) if (!dfn[i]) tarjan(i);
            bool flag = true;
            rep(i,0,n) if (belong[2 * i] == belong[2 * i + 1])
            { flag = false; break; }
            if (flag) l = mid + 1, ans = mid;
            else r = mid - 1;
        }
        printf("%d\n", ans);
    }
    return 0;
}

Building roads

Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 6942 Accepted: 2349

Description

FJ的农场有N个牛舍,每个牛舍都有一些牛生活在里面。这些牛喜欢到处串门,故FJ想修建一些路连接这些牛舍。如果他给每对牛舍都修建道路(即 n(n1)2 条路),将会浪费太多的钱,吝啬的FJ是不会做的,虽然这样对牛们是最好的。

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.

聪明的FJ有另一个好点子。他先修建S1和S2两个转移点,然后修建一条道路连接S1和S2,N条路连接牛舍和S1或S2,显然每个牛舍都会连接到S1或S2,但不是两个都连。所以每对农场将会通过多条道路连接。为了不让牛花太多的时间在串门上,FJ希望最小化每对牛舍间距离的最大值。

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.

这不是完整的故事因为这里又有一个恼人的问题。一些牛舍的奶牛相互讨厌,而且FJ无法无法将他们的牛舍连接到同一个转移点。一些牛舍的奶牛互为朋友,而且FJ必须将他们的农场连接到同一个转移点。真是令人头疼!现在FJ向你求助,你的任务是找到一个可行的优化道路建筑方案最小化任意两对农场距离的最大值。这代表你需要考虑每个牛舍需要连接到的转移点。

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.

已知S1和S2和N个牛舍的坐标,哪些牛舍相互讨厌,哪些牛舍互为朋友。

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.

注意FJ总是横着或竖着建筑道路,故两地间距离为其曼哈顿距离。比如,(x1,y1)和(x2,y2)的曼哈顿距离为|x1-x2|+|y1-y2|。

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

第一行包含3个整数N、A和B,分别表示牛舍的数目,讨厌牛舍的对数,朋友牛舍的对数。

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.

第二行包含4个整数sx1,sy1,sx2,sy2,分别是s1和s2的坐标。

Next line contains 4 integer sx1, sy1, sx2, sy2, which are the coordinates of two different transferring point S1 and S2 respectively.

接下来N行,每行2个整数x和y,即对应牛舍的坐标。

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.

接下来A行,每行两个整数i和j,表示牛舍i和牛舍j相互讨厌,且不会重复。

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.

接下来B行,每行两个整数i和j,表示牛舍i和牛舍j互为朋友,且不会重复。

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.

坐标范围[-1000000, 1000000]
You should note that all the coordinates are in the range [-1000000, 1000000].

Output

输出一个整数,每对牛舍间距离的最大值的最小值,如果方案不存在,输出-1。
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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值