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

题意:n个城市修建m个机场,使得每个城市到最近进场的最大值最小。

思路:二分+dlx搜索判定。

#include <iostream>
#include <map>
#include <string>
#include <stdio.h>
#include <vector>
#include <set>
#include <algorithm>
#include <string.h>
#include <fstream>
#include <tchar.h>
#include <sstream>
using namespace std;

];
int KK;
int m;

class CDancingLinks
{
protected:
    struct DancingLinksNode
    {
        DancingLinksNode* left;
        DancingLinksNode* right;
        DancingLinksNode* down;
        DancingLinksNode* up;
        int col;
        int row;
    };

    typedef DancingLinksNode Node;

    int *m_columnEleNumbers;
    int m_colNumber;
    int m_rowNumber;
    Node* m_pool;
    Node** m_head;
    int m_curUsePoolIndex;

    void _Remove(Node* cur)
    {
        --m_columnEleNumbers[cur->col];
        for(Node* p=cur->down;p!=cur;p=p->down)
        {
            p->left->right=p->right;
            p->right->left=p->left;
        }
    }

    void _Resume(Node* cur)
    {
        ++m_columnEleNumbers[cur->col];
        for(Node* p=cur->up;p!=cur;p=p->up)
        {
            p->left->right=p;
            p->right->left=p;
        }
    }

    int astar()
    {
        Node* p=_GetNode();

        ++KK;
        ;

        for(Node* q=p->left;q!=p;q=q->left) if(KK!=visit[q->col])
        {
            visit[q->col]=KK;
            ++ans;
            for(Node* r=q->up;r!=q;r=r->up) for(Node* t=r->left;t!=r;t=t->left)
            {
                visit[t->col]=KK;
            }
        }
        return ans;
    }

    bool _SearchSolution(const int depth,std::vector<int> &solution)
    {
        Node* p=_GetNode();
        if(p->left==p) return true;
        if(depth+astar()>m) return false;

        ;
        ;
        for(Node* q=p->left;q!=p;q=q->left)
        {
            if(m_columnEleNumbers[q->col]<Min)
            {
                Min=m_columnEleNumbers[q->col];
                MinColumnIndex=q->col;
            }
        }

        for(Node* q=_GetNode(MinColumnIndex)->down;q!=_GetNode(MinColumnIndex);q=q->down)
        {
            _Remove(q);
            solution.push_back(q->row);
            for(Node* rr=q->right;rr!=q;rr=rr->right) _Remove(rr);
            ,solution)) return true;
            for(Node* rr=q->left;rr!=q;rr=rr->left) _Resume(rr);
            solution.pop_back();
            _Resume(q);
        }

        return false;
    }

    Node* _GetNode(int id) { return m_pool+id; }

    void _ReleaseMemory()
    {
         if(m_columnEleNumbers)
        {
            delete[] m_columnEleNumbers;
            m_columnEleNumbers=nullptr;
        }

        if(m_pool)
        {
            delete[] m_pool;
            m_pool=nullptr;
        }
        if(m_head)
        {
            delete[] m_head;
            m_head=nullptr;
        }
    }

public:

    CDancingLinks():m_colNumber(-),m_rowNumber(-),
        m_columnEleNumbers(nullptr),m_pool(nullptr),m_head(nullptr) {}

    /***
      列下标为[1,Column]
    ***/
    CDancingLinks(const int Column,const int Row):
        m_columnEleNumbers(nullptr),m_pool(nullptr),m_head(nullptr)
    {
        SetSize(Column,Row);
    }

    /***
      列下标为[1,Column]
    ***/
    void SetSize(const int Column,const int Row)
    {
        m_colNumber=Column;
        m_rowNumber=Row;

        _ReleaseMemory();

        m_columnEleNumbers=];
        m_pool=)+];
        m_head=];
        Clear();
    }

    void Clear()
    {
        ;i<=m_colNumber;++i)
        {
            Node* cur=_GetNode(i);
            cur->left=((i==m_colNumber)?_GetNode():_GetNode(i+));
            cur->right=((==i)?_GetNode(m_colNumber):_GetNode(i-));
            m_columnEleNumbers[i]=;

            cur->up=cur->down=_GetNode(i);
            cur->col=i;
            cur->row=;
        }
        ;i<=m_rowNumber;++i) m_head[i]=NULL;
        m_curUsePoolIndex=m_colNumber+;
    }

    ~CDancingLinks()
    {
        _ReleaseMemory();
    }

    void AddElement(const int row,const int col)
    {

        Node* cur=m_pool+(m_curUsePoolIndex++);

        cur->up=_GetNode(col);
        cur->down=_GetNode(col)->down;
        m_pool[col].down->up=cur;
        m_pool[col].down=cur;

        if(m_head[row]==NULL)
        {
            m_head[row]=cur->left=cur->right=cur;
        }
        else
        {
            cur->left=m_head[row]->left;
            cur->right=m_head[row];
            m_head[row]->left->right=cur;
            m_head[row]->left=cur;
        }
        ++m_columnEleNumbers[col];
        cur->col=col;
        cur->row=row;
    }

    bool GetSolution(std::vector<int> &Solution)
    {
        ,Solution);
    }
};

][];
][];
int n;
];

CDancingLinks A;

inline long long dist(int i,int j)
{
    ]-a[j][]))+abs(a[i][]-a[j][]);
}

int ok(long long M)
{
    A.Clear();
    ;i<=n;i++) ;j<=n;j++) if(dis[i][j]<=M)
    {
        A.AddElement(i,j);
    }
    vector<int> ans;
    return A.GetSolution(ans);
}

long long cal()
{
    A.SetSize(n,n);

    ;
    int i,j;
    ;i<=n;i++) ;j<=n;j++) dis[i][j]=dist(i,j),d[num++]=dis[i][j];
    sort(d,d+num);
    int M=unique(d,d+num)-d;
    ,high=M-;
    long long ans=d[high];

    while(low<=high)
    {
        ;
        ;
        ;
    }
    return ans;
}

int main()
{
    int T;
    scanf("%d",&T);
    ;i<=T;++i)
    {
        scanf("%d%d",&n,&m);
        ;j<=n;++j) scanf(],&a[j][]);
        printf("Case #%d: %I64d\n",i,cal());
    }
}

HDU 5046 Airport(dlx)的更多相关文章

  1. HDU 5046 Airport(DLX反复覆盖)

    HDU 5046 Airport 题目链接 题意:给定一些机场.要求选出K个机场,使得其它机场到其它机场的最大值最小 思路:二分+DLX反复覆盖去推断就可以 代码: #include <cstd ...

  2. HDU 5046 Airport【DLX重复覆盖】

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5046 题意: 给定n个城市的坐标,要在城市中建k个飞机场,使城市距离最近的飞机场的最长距离最小,求这 ...

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

    Description The country of jiuye composed by N cites. Each city can be viewed as a point in a two- d ...

  4. hdu 5046 二分+DLX模板

    http://acm.hdu.edu.cn/showproblem.php?pid=5046 n城市建k机场使得,是每个城市最近机场的距离的最大值最小化 二分+DLX 模板题 #include < ...

  5. HDU 5046 Airport ( Dancing Links 反复覆盖 )

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

  6. hdu 5046 Airport 二分+重复覆盖

    题目链接 给n个点, 定义两点之间距离为|x1-x2|+|y1-y2|. 然后要选出k个城市建机场, 每个机场可以覆盖一个半径的距离. 求在选出点数不大于k的情况下, 这个半径距离的最大值. 二分半径 ...

  7. HDU 3335 Divisibility (DLX)

    Divisibility Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit ...

  8. 【HDOJ】5046 Airport

    DLX简单题目. /* 5046 */ #include <iostream> #include <string> #include <map> #include ...

  9. HDU 2295.Radar (DLX重复覆盖)

    2分答案+DLX判断可行 不使用的估计函数的可重复覆盖的搜索树将十分庞大 #include <iostream> #include <cstring> #include < ...

随机推荐

  1. Trace Sys

    ARM片上调试和跟踪解决方案(包括CoreSight体系结构,嵌入式跟踪宏单元(ETM),程序流程跟踪(PTM),ARM调试接口(ADI), 跟踪缓冲器(ETB),嵌入式交叉触发器(CTM)) Cor ...

  2. SqlServer nvarchar中的中文字符匹配,更改SqlServer实例和数据库排序规则的办法

    我们都知道在SqlServer中的nvarchar类型可以完美的存储诸如中文这种unicode字符,但是我们会发现有时候查询语句去查询nvarchar列的时候查不出来. 为什么nvarchar类型有时 ...

  3. OpenStack 界面开发中response.body的中文编码问题

    Contents [hide] 1 问题的引入= 1.1 解决办法 2 用户限制输入中文 3 不限制用户输入,呈现上修改 问题的引入= G在我们创建虚拟机的时候,会设置虚拟机的名称,描述,如果没有限制 ...

  4. 电脑配置 eclipse 环境变量

    先找到电脑环境变量 我的电脑——属性——高级系统设置——环境变量 path里面需要两个环境 第一个Java 环境 C:\Program Files\Java\jdk1.8.0_40\bin; 第二个a ...

  5. js正则函数match、exec、test、search、replace、split使用介绍集合

    match 方法 使用正则表达式模式对字符串执行查找,并将包含查找的结果作为数组返回. stringObj.match(rgExp) 参数 stringObj 必选项.对其进行查找的 String 对 ...

  6. dubbo 学习

    1. dubbo Can not lock the registry cache file: 当本地同时启动服务端和客户端的时候就可能产生这个问题. 解决方案 Dubbo通过注册中心发现服务,发现的服 ...

  7. Sensor信号输出YUV、RGB、RAW DATA、JPEG【转】

    本文转载自:http://blog.csdn.net/southcamel/article/details/8305873 简单来说,YUV: luma (Y) + chroma (UV) 格式, 一 ...

  8. Hadoop集群管理之配置文件

    一.配置文件列表如下: [hadoop@node1 conf]$ pwd /app/hadoop/conf [hadoop@node1 conf]$ echo $HADOOP_HOME /app/ha ...

  9. 快速稳定的维护PHP

    Just to recap, previously we'd have this sort of thing: namespace me\adamcameron\testApp; use Guzzle ...

  10. PHP数组的一些常用函数

    [数组排序]sort()低到高,rsort()高到低.保持键值对应关系使用 asort()和arsort().对键排序ksort()和krsort().随机排序 shuffle(). [数组key相关 ...