码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 5046 Airport

时间:2014-12-29 19:52:52      阅读:191      评论:0      收藏:0      [点我收藏+]

标签:

链接: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;
}

 

hdu 5046 Airport

标签:

原文地址:http://www.cnblogs.com/ltwy/p/4192281.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!