HDU 2295 Radar (DLX求重复覆盖, A*搜索)

题目大意:

就是在M个站中选择至多K个使得N个点被覆盖, 为需要的最小半径


大致思路:

二分答案R, 然后建立N*M的01矩阵判断重复覆盖是否可行

重复覆盖和精确覆盖不同, 每次只会删掉每一列所有相关的1, 而不会将有相关1的行删去, 所以矩阵在减少的速度上没有精确覆盖快, 需要进行剪枝

这里使用一个估价函数f(), 表示单签状况下最好情况需要多少步才能走完, 进行剪枝


代码如下:

Result  :  Accepted     Memory  :  1656 KB     Time  :  62 ms

/*
 * Author: Gatevin
 * Created Time:  2015/10/4 18:10:46
 * File Name: Sakura_Chiyo.cpp
 */
#include<iostream>
#include<sstream>
#include<fstream>
#include<vector>
#include<list>
#include<deque>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cctype>
#include<cmath>
#include<ctime>
#include<iomanip>
using namespace std;
const double eps(1e-8);
typedef long long lint;

#define maxnode 4000
#define maxn 60
#define maxm 60

pair<int, int> city[60];
pair<int, int> sta[60];

struct DLX
{
    int n, m, size;
    int U[maxnode], D[maxnode], R[maxnode], L[maxnode], Row[maxnode], Col[maxnode];
    int H[maxn], S[maxm];
    int ansd, ans[maxn];
    void init(int _n, int _m)
    {
        n = _n;
        m = _m;
        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++) 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)//因为是重复覆盖每次讲某一列的所有1纳入不考虑范围
    {
        //L[R[c]] = L[c]; R[L[c]] = R[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;
        //L[R[c]] = R[L[c]] = c;
    }
    bool v[60];
    int f()//A*搜索估价函数
    {
        int ret = 0;
        for(int i = R[0]; i != 0; i = R[i]) v[i] = 1;
        for(int i = R[0]; i != 0; i = R[i])
            if(v[i])
            {
                ret++;
                v[i] = 0;
                for(int j = D[i]; j != i; j = D[j])
                    for(int k = R[j]; k != j; k = R[k])
                        v[Col[k]] = 0;
            }
        return ret;
    }
    
    int Dance(int dep, int K)
    {
        if(dep + f() > K) return false;
        if(R[0] == 0)
        {
            return true;
        }
        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])
        {
            for(int j = R[i]; j != i; j = R[j]) remove(j);
            remove(i);
            if(Dance(dep + 1, K)) return true;
            resume(i);
            for(int j = L[i]; j != i; j = L[j]) resume(j);
        }
        return false;
    }
    void solve()
    {
        int N, M, K;
        scanf("%d %d %d", &N, &M, &K);
        for(int i = 1; i <= N; i++)
            scanf("%d %d", &city[i].first, &city[i].second);
        for(int j = 1; j <= M; j++)
            scanf("%d %d", &sta[j].first, &sta[j].second);
        
        double L = 0, R = 1500, mid = -1, ans = -1;
        int cnt = 0;
        int times = 40;
        while(L + eps < R && cnt <= times)
        {
            cnt++;
            mid = (L + R) / 2.;
            init(M, N);
            for(int i = 1; i <= M; i++)
                for(int j = 1; j <= N; j++)
                    if((sta[i].first - city[j].first)*(sta[i].first - city[j].first)*1.
                            + (sta[i].second - city[j].second)*(sta[i].second - city[j].second)*1. < mid*mid)
                        Link(i, j);
            if(Dance(0, K))
            {
                ans = mid;
                R = mid;
            }
            else L = mid;
        }
        printf("%.6f\n", ans);
        return;
    }
};

DLX dlx;

int main()
{
    int T;
    scanf("%d", &T);
    while(T--)
        dlx.solve();
    return 0;
}


你可能感兴趣的:(HDU,radar,dlx,2295,重复覆盖)