hdu 5046 Airport(Dancing Links重复覆盖)

Airport

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1310    Accepted Submission(s): 405


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 d ij = |x i - x j| + |y i - y j|. jiuye want to setup airport in K cities among N cities. So he need your help to choose these K cities, to minimize the maximum distance to the nearest airport of each city. That is , if we define d i(1 ≤ i ≤ N ) as the distance from city i to the nearest city with airport. Your aim is to minimize the value max{d i|1 ≤ i ≤ N }. You just output the minimum.
 

Input
The first line of the input is T (1 ≤ T ≤ 100), which stands for the number of test cases you need to solve.

The first line of each case contains two integers N ,K (1 ≤ N ≤ 60,1 ≤ K ≤ N ),as mentioned above.

The next N lines, each lines contains two integer x i and y i (-10 9 ≤ x i, y i ≤ 10 9), denote the coordinates of city i.
 

Output
For each test case, print a line “Case #t: ”(without quotes, t means the index of the test case) at the beginning. Then a single integer means the minimum.
 

Sample Input
  
  
2 3 2 0 0 4 0 5 1 4 2 0 3 1 0 3 0 8 9
 

Sample Output
  
  
Case #1: 2 Case #2: 4
 

n个城市  找出k个城市建立机场 每个城市与距离自己最近的机场之间有距离 

求出这些距离中最大值的最小值


与hdu 2295十分类似 只是距离的表达方式不同 

同样二分距离 如果两个城市之间的距离小于二分的值就建边 跑Dancing Links重复覆盖 

#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string.h>
#include <string>
#include <vector>
#include <queue>

#define MEM(a,x) memset(a,x,sizeof a)
#define eps 1e-8
#define MOD 10009
#define INF 99999999
#define ll __int64
#define bug cout<<"here"<<endl
#define fread freopen("ceshi.txt","r",stdin)
#define fwrite freopen("out.txt","w",stdout)

using namespace std;


const int MAXM=100;
const int MAXN=100;
const int maxnode=MAXN*MAXM;
int K;

struct DLX
{
    int n,m,size;
    int U[maxnode],D[maxnode],R[maxnode],L[maxnode],Row[maxnode],Col[maxnode];
    int H[MAXN],S[MAXM];
    int ansd;
    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[MAXM];
    int f()
    {
        int ret=0;
        for(int c=R[0];c!=0;c=R[c]) v[c]=1;
        for(int c=R[0];c!=0;c=R[c])
        {
            if(v[c])
            {
                ret++;
                v[c]=0;
                for(int i=D[c];i!=c;i=D[i])
                    for(int j=R[i];j!=i;j=R[j])
                        v[Col[j]]=0;
            }
        }
        return ret;
    }
    bool Dance(int d)
    {
        if(d+f()>K) return 0;
        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 1;
            for(int j=L[i];j!=i;j=L[j]) resume(j);
            resume(i);
        }
        return 0;
    }
};
DLX dlx;

struct City
{
    ll x,y;
    void input()
    {
        scanf("%I64d%I64d",&x,&y);
    }
}city[MAXN];

ll ABS(ll a,ll b)
{
    if(a>b) return a-b;
    return b-a;
}

ll dis(City a,City b)
{
    return ABS(a.x,b.x)+ABS(a.y,b.y);
}

int main()
{
//    fread;
    int tc;
    scanf("%d",&tc);
    int cs=1;
    while(tc--)
    {
        int n;
        scanf("%d%d",&n,&K);
        for(int i=1;i<=n;i++)
            city[i].input();
//        for(int i=1;i<=n;i++)
//            cout<<city[i].x<<" "<<city[i].y<<endl;
        ll mx=0;
        for(int i=1;i<=n;i++)
            for(int j=i+1;j<=n;j++)
        {
            ll len=dis(city[i],city[j]);
            if(mx<len) mx=len;
        }
        ll l=0,r=mx;
        ll mid;
        ll ans;
        while(r-l>0)
        {
            mid=(r+l)/2;
            dlx.init(n,n);
            for(int i=1;i<=n;i++)
                for(int j=1;j<=n;j++)
                {
                    if(dis(city[i],city[j])<=mid)
                    {
                        dlx.Link(i,j);
                    }
                }
            if(dlx.Dance(0)) r=mid,ans=mid;
            else l=mid+1;
        }
        printf("Case #%d: %I64d\n",cs++,r);
    }
    return 0;
}






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值