hdu 2295 &&hdu 5046

第一个题目的意思是有n个城市和m个雷达。你最多可以用k个雷达,问使用最小多少的半径可以使k个雷达覆盖n个城市。

第二个是九野要从n个城市选择k个城市建造机场,问最小的最大城市距离是多少


都是舞蹈链+剪枝+二分计算路径


贴第二题代码

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;

const int M = 110;
const int N = 30000;
const int inf = 0x3f3f3f3f;
const double ep = 1e-8;
int k;
struct DLX {
    int n , m,size;
    int U[N],D[N],R[N],L[N],row[N],col[N];
    int ans[M],S[M];
    int ansd;

    void init(int a,int b) {
        n = a;
        m = b;
        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++) ans[i] = -1;
    }

    void addRow(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(ans[r]<0)
            ans[r] =  L[size] = R[size] = size;
        else {
            R[size] = R[ans[r]];
            L[R[ans[r]]] = size;
            L[size] = ans[r];
            R[ans[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[N];

    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;
    }
} g;
struct node{
   int x;
   int y;
}point[N],radar[N];

 long long dis(node a,node b)
{
     return (long long)abs(a.x-b.x)+(long long)abs(a.y-b.y);
}

int T;
int n,m,p;

int main(){
    int cas = 0;
	scanf("%d",&T);
	while(T--){
		scanf("%d%d",&n,&k);

		for(int i=1;i<=n;i++){
			scanf("%d%d",&point[i].x,&point[i].y);
		}

       /*
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                printf("%f ",dis(point[i],radar[j]));
            }
            puts("");
        }
        */
        long long  ans = 0;
		long long  l= 0;
		long long  r = 100000000000LL;
		while(r >= l ){
            //puts("OK");
            g.init(n,n);
            long long mid = (l+r)/2;
            for(int i=1;i<=n;i++){
                    for(int j=1;j<=n;j++){
                    if(dis(point[i],point[j]) <= mid){
                        g.addRow(j,i);
                    }
                    }
            }
            if(g.dance(0)) {r = mid - 1 ; ans = mid; }
            else l = mid + 1 ;
		}

		printf("Case #%d: %I64d\n",++cas,ans);
	}
}


你可能感兴趣的:(hdu 2295 &&hdu 5046)