HDU 5046 Airport DLX多重覆盖

来源:互联网 发布:风云卫星遥感数据网 编辑:程序博客网 时间:2024/06/05 07:59

题目:

http://acm.hdu.edu.cn/showproblem.php?pid=5046

题意:

有n个城市在二维平面上,任意两个城市间的距离为曼哈顿距离,现在在n个城市中选出k个设置机场,要求覆盖全部n个城市,问覆盖半径最小为多少

思路:

跟hdu 3656基本一样,只需要变换一下计算距离的公式即可,注意会爆int

#include <bits/stdc++.h>using namespace std;typedef long long ll;const int X = 10000 + 10, N = 60 + 10, M = 60 + 10, INF = 0x3f3f3f3f;ll x[M], y[M], a[M][M], b[M*M];int cas = 0;struct DLX{    int U[X], D[X], L[X], R[X], row[X], col[X];    int H[N], S[M];    int head, sz, tot, n, m;    bool vis[M];    void init(int _n, int _m)    {        n = _n, m = _m;        for(int i = 0; i <= m; i++)            L[i] = i-1, R[i] = i+1, U[i] = D[i] = i, S[i] = 0;        head = 0, tot = 0, sz = m;        L[head] = m, R[m] = head;        for(int i = 1; i <= n; i++) H[i] = -1;    }    void link(int r, int c)    {        ++S[col[++sz]=c];        row[sz] = r;        D[sz] = D[c], U[D[c]] = sz;        U[sz] = c, D[c] = sz;        if(H[r] < 0) H[r] = L[sz] = R[sz] = sz;        else R[sz] = R[H[r]], L[R[H[r]]] = sz, L[sz] = H[r], R[H[r]] = sz;    }    void del(int x)    {        for(int i = D[x]; i != x; i = D[i])            R[L[i]] = R[i], L[R[i]] = L[i];    }    void recover(int x)    {        for(int i = U[x]; i != x; i = U[i])            R[L[i]] = L[R[i]] = i;    }    int fun_f()    {        memset(vis, 0, sizeof vis);        int num = 0;        for(int i = R[head]; i != head; i = R[i])            if(! vis[i])            {                vis[i] = true;                num++;                for(int j = D[i]; j != i; j = D[j])                    for(int k = R[j]; k != j; k = R[k])                        vis[col[k]] = true;            }        return num;    }    bool dance(int dep, int k)    {        if(R[head] == head)        {            if(dep-1 <= k) return true;            else return false;        }        if(dep-1 + fun_f() > k) return false;        int c = R[head];        for(int i = R[head]; i != head; i = R[i])            if(S[i] < S[c]) c = i;        for(int i = D[c]; i != c; i = D[i])        {            del(i);            for(int j = R[i]; j != i; j = R[j]) del(j);            if(dance(dep + 1, k)) return true;            for(int j = L[i]; j != i; j = L[j]) recover(j);            recover(i);        }        return false;    }}dlx;ll dis(ll x1, ll y1, ll x2, ll y2){    return abs(x1-x2) + abs(y1-y2);}int main(){    int t, n, m;    scanf("%d", &t);    while(t--)    {        scanf("%d%d", &n, &m);        for(int i = 1; i <= n; i++) scanf("%lld%lld", &x[i], &y[i]);        int k = 0;        for(int i = 1; i <= n; i++)            for(int j = 1; j <= n; j++)            {                a[i][j] = dis(x[i], y[i], x[j], y[j]);                b[++k] = a[i][j];            }        sort(b + 1, b + 1 + k);        k = unique(b + 1, b + 1 + k) - b - 1;        int l = 1, r = k, ans;        while(l <= r)        {            int mid = (l + r) / 2;            dlx.init(n, n);            for(int j = 1; j <= n; j++)                for(int k = 1; k <= n; k++)                    if(a[j][k] <= b[mid])                        dlx.link(j, k);            if(dlx.dance(1, m)) ans = mid, r = mid - 1;            else l = mid + 1;        }        printf("Case #%d: %lld\n", ++cas, b[ans]);    }    return 0;}
原创粉丝点击