题目链接: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()); } }