HDU2295 Radar —— Dancing Links 可重复覆盖

本文介绍了一个优化问题,即如何利用有限数量的雷达站覆盖所有城市,同时最小化雷达的覆盖半径。通过对比两种不同的算法实现,展示了如何采用二分搜索结合舞动链接列表的方法来高效解决该问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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


Radar

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4106    Accepted Submission(s): 1576


Problem Description
N cities of the Java Kingdom need to be covered by radars for being in a state of war. Since the kingdom has M radar stations but only K operators, we can at most operate K radars. All radars have the same circular coverage with a radius of R. Our goal is to minimize R while covering the entire city with no more than K radars.
 

Input
The input consists of several test cases. The first line of the input consists of an integer T, indicating the number of test cases. The first line of each test case consists of 3 integers: N, M, K, representing the number of cities, the number of radar stations and the number of operators. Each of the following N lines consists of the coordinate of a city.
Each of the last M lines consists of the coordinate of a radar station.

All coordinates are separated by one space.
Technical Specification

1. 1 ≤ T ≤ 20
2. 1 ≤ N, M ≤ 50
3. 1 ≤ K ≤ M
4. 0 ≤ X, Y ≤ 1000
 

Output
For each test case, output the radius on a single line, rounded to six fractional digits.
 

Sample Input
  
1 3 3 2 3 4 3 1 5 4 1 1 2 2 3 3
 

Sample Output
  
2.236068
 

Source




题解:

超时方法:

1.对于DLX的矩阵:行代表着雷达与城市的距离, 列代表着城市。矩阵大小250*50。

2.Dancing跳起来,当R[0]==0时, 取当前所选行中,距离的最大值dis(这样才能覆盖掉所有城市),然后再更新答案ans,ans = min(ans, dis)。

3.结果矩阵有点大, 超时了。

4.错误思想分析:把雷达与城市的距离作为行,实际上是太明智的。因为题目说明了每个雷达的接收半径是相同的,而以上方法选出来的每个雷达的接收半径是相异的,然后又再取最大值,那为何不每次都取最大值(相同值)呢? 如果取相同值,那么行就是雷达,列就是城市,矩阵的大小就减少了。但是又怎么确定雷达的接收半径呢?如下:


正确方法:

1.雷达作为行, 城市作为列。

2.二分雷达的接收范围,每次二分都:根据接收半径更新矩阵中所含的元素,然后再进行一次Dance(),如果能覆盖掉所有城市,则缩小半径,否则扩大半径。



超时方法:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
#define ms(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long LL;
const int INF = 2e9;
const int MAXN = 50+10;
const int MAXM = 50+10;
const int maxnode = 1e5+10;

double city[MAXN][2], radar[MAXN][2];
double r[MAXN*MAXM];
int k;

struct DLX
{
    int n, m, size;
    int U[maxnode], D[maxnode], L[maxnode], R[maxnode], Row[maxnode], Col[maxnode];
    int H[MAXN*MAXM], S[MAXN*MAXM];
    double ansd, ans[MAXN*MAXM];

    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)
    {
        size++;
        Row[size] = r;
        Col[size] = c;
        S[Col[size]]++;
        D[size] = D[c];
        U[D[c]] = size;
        U[size] = c;
        D[c] = size;
        if(H[r]==-1) 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)
    {
        for(int i = D[c]; i!=c; i = D[i])
            L[R[i]] = L[i], R[L[i]] = R[i];
    }

    bool v[MAXM];
    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;
    }

    void resume(int c)
    {
        for(int i = U[c]; i!=c; i = U[i])
            L[R[i]] = R[L[i]] = i;
    }

    void Dance(int d)
    {
        if(d+f()>k) return;
        if(R[0]==0)
        {
            double tmp = -1.0;
            for(int i = 0; i<d; i++)
                tmp = max(tmp, ans[i]);
            ansd = min(tmp, ansd);
            return;
        }

        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])
        {
            ans[d] = r[Row[i]];
            remove(i);
            for(int j = R[i]; j!=i; j = R[j]) remove(j);
            Dance(d+1);
            for(int j = L[i]; j!=i; j = L[j]) resume(j);
            resume(i);
        }
    }
};

double dis(double x1, double y1, double x2, double y2)
{
    return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}

DLX dlx;
int main()
{
    int T;
    int n, m;
    scanf("%d", &T);
    while(T--)
    {
        scanf("%d%d%d", &n, &m, &k);
        dlx.init(n*m, n);
        for(int i = 1; i<=n; i++)
            scanf("%lf%lf",&city[i][0], &city[i][1]);
        for(int i = 1; i<=m; i++)
            scanf("%lf%lf",&radar[i][0], &radar[i][1]);

        for(int i = 1; i<=m; i++)
        for(int j = 1; j<=n; j++)
        {
            double tmp = dis(radar[i][0], radar[i][1], city[j][0], city[j][1]);
            r[(i-1)*n+j] = tmp;
            for(int t = 1; t<=n; t++)
                if(dis(radar[i][0], radar[i][1], city[t][0], city[t][1])<=tmp)
                    dlx.Link((i-1)*n+j, t);
        }
        dlx.ansd = 1.0*INF;
        dlx.Dance(0);
        printf("%.6f\n", dlx.ansd);
    }
    return 0;
}



正确方法:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
#define ms(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long LL;
const int INF = 2e9;
const double EPS = 1e-8;
const int MAXN = 50+10;
const int MAXM = 50+10;
const int maxnode = 1e5+10;

double city[MAXN][2], radar[MAXN][2];
double r[MAXN*MAXM];
int k;

struct DLX
{
    int n, m, size;
    int U[maxnode], D[maxnode], L[maxnode], R[maxnode], Row[maxnode], Col[maxnode];
    int H[MAXN*MAXM], S[MAXN*MAXM];
    double ansd, ans[MAXN*MAXM];

    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)
    {
        size++;
        Row[size] = r;
        Col[size] = c;
        S[Col[size]]++;
        D[size] = D[c];
        U[D[c]] = size;
        U[size] = c;
        D[c] = size;
        if(H[r]==-1) 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)
    {
        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[MAXM];
    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 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])
        {
            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;
    }
};

double dis(double x1, double y1, double x2, double y2)
{
    return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}

DLX dlx;
int main()
{
    int T;
    int n, m;
    scanf("%d", &T);
    while(T--)
    {
        scanf("%d%d%d", &n, &m, &k);
        for(int i = 1; i<=n; i++)
            scanf("%lf%lf",&city[i][0], &city[i][1]);
        for(int i = 1; i<=m; i++)
            scanf("%lf%lf",&radar[i][0], &radar[i][1]);

        double l = 0.0, r = 2000.0;
        while(l+EPS<=r)
        {
            double mid = (l+r)/2;
            dlx.init(m, n);
            for(int i = 1; i<=m; i++)
            for(int j = 1; j<=n; j++)
                if(dis(radar[i][0], radar[i][1], city[j][0], city[j][1])<=mid)
                    dlx.Link(i, j);
            if(dlx.Dance(0))
                r = mid - EPS;
            else
                l = mid + EPS;
        }
        printf("%.6lf\n",l);
    }
    return 0;
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值