hdu5064 DLX可重复覆盖+二分

这题题意是 给了n个城市 在其中小于等于k个城市建立机场然后 使得最远的那个离机场的城市距离最短

二分答案 ,我们对于每次的mid 重新建图然后再来一次DLX,每个点可以覆盖的点建立一条联系就ok了

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <string.h>
#include <vector>
using namespace std;
const int maxn=61;
const int maxnode=61*61;
int K;
struct DLX
{
     int numCol,numRow,U[maxnode],D[maxnode],L[maxnode],R[maxnode],row[maxnode],col[maxnode];
     int S[maxn],H[maxn],siz;
     void init(int n,int m)
     {
         this->numCol=n; this->numRow=m;
         for(int i=0; i<=numCol; i++)
            {
                S[i]=0;
                U[i]=D[i]=i;
                L[i]=i-1; R[i]=i+1;
            }
          L[0]=n; R[n]=0;
          for(int i=0; i<=numRow; i++)H[i]=-1;
         siz=n+1;
     }
     void link(int r, int c)
     {
         S[c]++;
         row[siz]=r;
         col[siz]=c;
         U[siz]=U[c];
         D[siz]=c;
         D[U[siz]]=siz;
         U[D[siz]]=siz;
         if(H[r]==-1){
            H[r]=L[siz]=R[siz]=siz;
         }else {
            L[siz]=H[r];
            R[siz]=R[H[r]];
            R[L[siz]]=siz;
            L[R[siz]]=siz;
            H[r]=siz;
         }
         siz++;
     }
     bool v[maxnode];
     int AX()
     {
         for(int c=R[0]; c!=0; c=R[c])v[c]=true;
         int ret=0;
         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;
     }
     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 Dance(int d)
     {
         if(AX()+d>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;
     }

}g;
struct point
{
    int x,y;
    void input()
    {
        scanf("%d%d",&x,&y);
    }
}city[maxn];
long long dis(point a, point b)
{
    return (long long)abs(a.x-b.x)+(long long)abs(a.y-b.y);
}
int main()
{
    int cas;
    scanf("%d",&cas);
    for(int cc=1; cc<=cas; cc++)
        {
            int n;
            scanf("%d%d",&n,&K);
            for(int i=0; i<n; i++)
                city[i].input();
            long long L=0,R=100000000000LL;
            long long ans=0;
            while(L<=R)
                {
                    long long mid=(L+R)>>1;
                    g.init(n,n);
                    for(int i=0;i<n;i++)
                        for(int j=0; j<n;j++)
                        if(dis(city[i],city[j])<=mid)
                        g.link(i+1,j+1);
                    if(g.Dance(0)){R=mid-1;ans=mid; }
                    else L=mid+1;
                }
                printf("Case #%d: %I64d\n",cc,ans);
        }
    return 0;
}

时间: 2024-08-03 21:34:23

hdu5064 DLX可重复覆盖+二分的相关文章

(中等) 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 dlx重复覆盖+二分答案

题目大意: 有一堆雷达工作站,安放至多k个人在这些工作站中,找到一个最小的雷达监控半径可以使k个工作人所在的雷达工作站覆盖所有城市 二分半径的答案,每次利用dlx的重复覆盖来判断这个答案是否正确 1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <algorithm> 5 #include <queue> 6 #include <climits

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

(简单) FZU 1686 神龙的难题 , DLX+可重复覆盖。

Description 这是个剑与魔法的世界.英雄和魔物同在,动荡和安定并存.但总的来说,库尔特王国是个安宁的国家,人民安居乐业,魔物也比较少.但是.总有一些魔物不时会进入城市附近,干扰人民的生活.就要有一些人出来守护居民们不被魔物侵害.魔法使艾米莉就是这样的一个人.她骑着她的坐骑,神龙米格拉一起消灭干扰人类生存的魔物,维护王国的安定.艾米莉希望能够在损伤最小的前提下完成任务.每次战斗前,她都用时间停止魔法停住时间,然后米格拉他就可以发出火球烧死敌人.米格拉想知道,他如何以最快的速度消灭敌人,减

(中等) POJ 1084 Square Destroyer , DLX+可重复覆盖。

Description The left figure below shows a complete 3*3 grid made with 2*(3*4) (=24) matchsticks. The lengths of all matchsticks are one. You can find many squares of different sizes in the grid. The size of a square is the length of its side. In the

hdu2295(重复覆盖+二分)

题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=2295 题意::一个国家有n个城市,有m个地方可以建造雷达,最多可以建K个雷达(K>=1 && K<=m),问雷达最短的探测半径,才能使n个城市都能探测到. 分析:二分距离,然后再DLX重复覆盖来判断.n个城市排成n列,再将每个城市当成一行,在二分的距离内能到达的城市在该列标为1,然后问题就转换成选至多k行来覆盖所有列,并且是可重复覆盖. #include <cstdio>

hdu5046(重复覆盖+二分)

题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=5046 题意:要在n个城市里建造不超过k个机场覆盖所有城市,问机场城市之间最大距离最小为多少. 分析:二分距离+DLX判断,n个城市n列,然后n行,每行城市i在二分的距离内能到达列j就标为1,问题转化为选不超过k行来覆盖所有列(可重复覆盖). 注意:最大距离为4*1e9,爆int了,要用long long,不然会TLE... #include <cstdio> #include <cstrin

HDU 3335 Divisibility(DLX可重复覆盖)

Problem Description As we know,the fzu AekdyCoin is famous of math,especially in the field of number theory.So,many people call him "the descendant of Chen Jingrun",which brings him a good reputation. AekdyCoin also plays an important role in th

HDU5046 Airport dancing links 重复覆盖+二分

这一道题和HDU2295是一样 是一个dancing links重复覆盖解决最小支配集的问题 在给定长度下求一个最小支配集,只要小于k就行 然后就是二分答案,每次求最小支配集 只不过HDU2295是浮点,这里是整数 我写的一个比较暴力 #include<cstdio> #include<cstring> #include<queue> #include<cstdlib> #include<algorithm> #include<vector