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

HDU 5046 Airport(DLX可重复覆盖)

时间:2014-12-25 23:41:42      阅读:349      评论:0      收藏:0      [点我收藏+]

标签:

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 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 di(1 ≤ i ≤ N ) as the distance from city i to the nearest city with airport. Your aim is to minimize the value max{di|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 xi and yi (-109 ≤ xi, yi ≤ 109), 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个机场,问机场城市之间最大距离最小为多少。
DLX做法:跟 

HDU 2295 Radar(DLX可重复覆盖)差不多,我们的做法就是

保存n个城市之间的距离,sort一下,二分结果,对满足条件的DLX求覆盖程度,

求出最大距离最小值。此题二分0~INF也可解决。只不过我的方法快点。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<string>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<bitset>
using namespace std;
#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )
#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )
#define CLEAR( a , x ) memset ( a , x , sizeof a )
typedef long long LL;
typedef pair<int,int>pil;
const int maxn = 60+5;
const int maxnnode=maxn*maxn;
const int mod = 1000000007;
int K;
struct DLX{
    int n,m,size;
    int U[maxnnode],D[maxnnode],L[maxnnode],R[maxnnode],Row[maxnnode],Col[maxnnode];
    int H[maxn],S[maxn];//H[i]位置,S[i]个数
    int ansd;
    void init(int a,int b)
    {
        n=a;  m=b;
        REPF(i,0,m)
        {
            S[i]=0;
            U[i]=D[i]=i;
            L[i]=i-1;
            R[i]=i+1;
        }
        R[m]=0; L[0]=m;
        size=m;
        REPF(i,1,n)
           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[maxn];
    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;
    }
};
struct point{
    LL x,y;
}X[maxn];
LL d[maxn][maxn];
LL dd[maxn*maxn];
DLX L;
int t,n,cnt;
int cas=1;
LL dis(point a,point b)
{
    return abs(a.x-b.x)+abs(a.y-b.y);
}
void solve()
{
    int l=0,r=cnt-1;
//    int ans;
    while(l<=r)
    {
        int mid=(l+r)>>1;
        L.init(n,n);
        REPF(i,1,n)
        {
            REPF(j,1,n)
              if(d[i][j]<=dd[mid])  L.link(i,j);
        }
        if(L.Dance(0))  r=mid-1;
        else  l=mid+1;
    }
    printf("Case #%d: %I64d\n",cas++,dd[l]);
}
int main()
{
    LL x,y;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&K);
        REPF(i,1,n)
        {
            scanf("%I64d%I64d",&x,&y);
            X[i].x=x;X[i].y=y;
        }
        cnt=0;
        REPF(i,1,n)
        {
            REPF(j,1,n)
            {
               d[i][j]=dis(X[i],X[j]);
               dd[cnt++]=d[i][j];
            }
        }
        sort(dd,dd+cnt);
        solve();
    }
    return 0;
}


HDU 5046 Airport(DLX可重复覆盖)

标签:

原文地址:http://blog.csdn.net/u013582254/article/details/42155301

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