hdu 5046 Airport

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

2014 ACM/ICPC Asia Regional Shanghai Online的题,DLX重复覆盖。。 虽然不放在DLX专题我都看不出来。。

二分距离,判断条件就是K个城市能不能满足条件。

#include <iostream>
#include <cstring>
#include <set>
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;
int num;

typedef long long ll;
struct node
{
    ll x,y;
};
node a[80];

const int MaxN = 70;
const int MaxM = 70;
const int maxnode = 4000;
int n,k;

struct DLX
{
    int n,m,size;
    int U[maxnode],D[maxnode],R[maxnode],L[maxnode],Row[maxnode],Col[maxnode];
    int H[MaxN],S[MaxN];
    int ansd,ans[MaxN];
    void init(int _n,int _m)
    {
        n = _n;
        m = _m;
        for(int i = 0;i <= m;i++)
        {
            S[i] = 0;
            U[i] = D[i] = i;
            L[i] = i-1;
            R[i] = i+1;
        }
        R[m] = 0; L[0] = m;
        size = m;
        for(int i = 1;i <= n;i++)
            H[i] = -1;
    }
    void Link(int r,int c)
    {
        ++S[Col[++size]=c];
        Row[size] = r;
        D[size] = D[c];
        U[D[c]] = size;
        U[size] = c;
        D[c] = size;
        if(H[r] < 0)H[r] = L[size] = R[size] = size;
        else
        {
            R[size] = R[H[r]];
            L[R[H[r]]] = size;
            L[size] = H[r];
            R[H[r]] = size;
        }
    }
    void remove(int c)
    {
        for(int i = D[c];i != c;i = D[i])
            L[R[i]] = L[i], R[L[i]] = R[i];
    }
    void resume(int c)
    {
        for(int i = U[c];i != c;i = U[i])
            L[R[i]]=R[L[i]]=i;
    }
    bool v[maxnode];
    int f()
    {
        int ret = 0;
        for(int c = R[0];c != 0;c = R[c])v[c] = true;
        for(int c = R[0];c != 0;c = R[c])
            if(v[c])
            {
                ret++;
                v[c] = false;
                for(int i = D[c];i != c;i = D[i])
                    for(int j = R[i];j != i;j = R[j])
                        v[Col[j]] = false;
            }
        return ret;

    }
    bool Dance(int d)
    {
        if(d + f() > k) return false;
        if(R[0] == 0) return d <= k;
        int c = R[0];
        for(int i = R[0];i != 0;i = R[i])
            if(S[i] < S[c])
                c = i;
        for(int i = D[c];i != c;i = D[i])
        {
            remove(i);
            for(int j = R[i];j != i;j = R[j])remove(j);
            if(Dance(d+1))return true;
            for(int j = L[i];j != i;j = L[j])resume(j);
            resume(i);
        }
        return false;
    }
};

DLX g;

bool solve(ll x)
{
    g.init(n,n);
    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=n; j++)
        {
            if((ll)abs(a[i].x - a[j].x) + (ll)abs(a[i].y - a[j].y) <= x)
                g.Link(i, j);
        }
    }
    return g.Dance(0);
}

int main()
{
    cin>>num;
    for(int ca=1; ca<=num; ca++)
    {
        scanf("%d %d", &n, &k);
        for(int i=1; i<=n; i++)
        {
            scanf("%lld %lld", &a[i].x, &a[i].y);
        }
        ll l = 0,r = 100000000000LL;
        ll mid;
        ll res;
        while(l<=r)
        {
            mid = (l+r)/2;
            if(solve(mid))
            {
                r = mid-1;
                res = mid;
            }
            else
                l = mid+1;
        }

        printf("Case #%d: %lld\n", ca, res);
    }
    return 0;
}
时间: 2024-12-12 10:14:16

hdu 5046 Airport的相关文章

HDU 5046 Airport(DLX反复覆盖)

HDU 5046 Airport 题目链接 题意:给定一些机场.要求选出K个机场,使得其它机场到其它机场的最大值最小 思路:二分+DLX反复覆盖去推断就可以 代码: #include <cstdio> #include <cstring> using namespace std; const int MAXNODE = 4005; const int MAXM = 65; const int MAXN = 65; const int INF = 0x3f3f3f3f; int K;

HDU 5046 Airport(DLX重复覆盖)

HDU 5046 Airport 题目链接 题意:给定一些机场,要求选出K个机场,使得其他机场到其他机场的最大值最小 思路:二分+DLX重复覆盖去判断即可 代码: #include <cstdio> #include <cstring> using namespace std; const int MAXNODE = 4005; const int MAXM = 65; const int MAXN = 65; const int INF = 0x3f3f3f3f; int K;

HDU 5046 Airport(DLX可重复覆盖)

Problem Description The country of jiuye composed by N cites. Each city can be viewed as a point in a two- dimensional plane with integer coordinates (x,y). The distance between city i and city j is defined by dij = |xi - xj| + |yi - yj|. jiuye want

HDU 5046 Airport(dlx)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5046 题意:n个城市修建m个机场,使得每个城市到最近进场的最大值最小. 思路:二分+dlx搜索判定. const int INF=1000000005; const int N=4444; int m; struct node { int L[N],R[N],D[N],U[N],e; int col[N]; int H[N],num[N]; int visit[N],KK; void init(in

HDU 5046 Airport ( Dancing Links 重复覆盖 )

今年上海网络赛的一道题目 , 跟 HDU 2295 如出一辙 , 就是距离的计算一个是欧几里得距离 , 一个是曼哈顿距离 学完DLX感觉这题好水 ,就是一个裸的重复覆盖 注意下别溢出就行了 #include <stdio.h> #include <string.h> #include <algorithm> #include <vector> #include <math.h> #include <stdlib.h> using na

HDU 5046 Airport ( Dancing Links 反复覆盖 )

今年上海网络赛的一道题目 , 跟 HDU 2295 如出一辙 . 就是距离的计算一个是欧几里得距离 , 一个是曼哈顿距离 学完DLX感觉这题好水 ,就是一个裸的反复覆盖 注意下别溢出即可了 #include <stdio.h> #include <string.h> #include <algorithm> #include <vector> #include <math.h> #include <stdlib.h> using na

(中等) HDU 5046 Airport ,DLX+可重复覆盖+二分。

Description The country of jiuye composed by N cites. Each city can be viewed as a point in a two- dimensional plane with integer coordinates (x,y). The distance between city i and city j is defined by d ij = |x i - x j| + |y i - y j|. jiuye want to

hdu 2295 &amp;&amp;hdu 5046

第一个题目的意思是有n个城市和m个雷达.你最多可以用k个雷达,问使用最小多少的半径可以使k个雷达覆盖n个城市. 第二个是九野要从n个城市选择k个城市建造机场,问最小的最大城市距离是多少 都是舞蹈链+剪枝+二分计算路径 贴第二题代码 #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> using namespace std; const int M = 110;

HDU 5046

同样是二分+DLX即可. #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #define LL __int64 using namespace std; const int maxn=3800; const int maxc=65; const int maxr=65; const int inf=0x3f