hdu 3622 Bomb Game(二分+2-sat)

本文解析了 HDU 3622 题目的解题思路,利用二分查找结合2-SAT算法确定在给定平面上放置炸弹的最大安全半径,确保爆炸范围不相交。

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

题目:http://acm.hdu.edu.cn/showproblem.php?pid=3622
题意:

给n对炸弹可以放置的位置(每个位置为一个二维平面上的点),每次放置炸弹是时只能选择这一对中的其中一个点,每个炸弹爆炸的范围半径都一样,控制爆炸的半径使得所有的爆炸范围都不相交(可以相切),求解这个最大半径.

分析:

二分最大半径值r,然后2-sat构图判断其可行性.
对于两队位置(u,uu)和(v,vv),如果u和v之间的距离小于2*r,也就是说位置u和位置v处不能同时防止炸弹(两范围相交),那么这两个点不能同时选,非(A与B)==(非A)或(非B),然后套O(nm)的模板即可。

代码:


#include<cstdio>
#include<vector>
#include<cstring>
using namespace std;

const int maxn = 2000 + 10;

struct TwoSAT {
    int n;
    vector<int> G[maxn*2];
    bool mark[maxn*2];
    int S[maxn*2], c;

    bool dfs(int x) {
        if (mark[x^1]) return false;
        if (mark[x]) return true;
        mark[x] = true;
        S[c++] = x;
        for (int i = 0; i < G[x].size(); i++)
            if (!dfs(G[x][i])) return false;
        return true;
    }

    void init(int n) {
        this->n = n;
        for (int i = 0; i < n*2; i++) G[i].clear();
        memset(mark, 0, sizeof(mark));
    }

    // x = xval or y = yval
    void add_clause(int x, int xval, int y, int yval) {
        x = x * 2 + xval;
        y = y * 2 + yval;
        G[x^1].push_back(y);
        G[y^1].push_back(x);
    }

    bool solve() {
        for(int i = 0; i < n*2; i += 2)
            if(!mark[i] && !mark[i+1]) {
                c = 0;
                if(!dfs(i)) {
                    while(c > 0) mark[S[--c]] = false;
                    if(!dfs(i+1)) return false;
                }
            }
        return true;
    }
};

///////// 题目相关
#include<algorithm>
#include<cmath>
typedef pair<int,int>pii;
#define x first
#define y second

TwoSAT solver;

int n;
pii s[maxn][2];
double dis(pii a,pii b) {
    return sqrt((double)(a.x-b.x)*(a.x-b.x)+(double)(a.y-b.y)*(a.y-b.y));
}

bool test(double diff) {
    solver.init(n);
    for(int i = 0; i < n; i++) for(int a = 0; a < 2; a++)
            for(int j = i+1; j < n; j++) for(int b = 0; b < 2; b++)
                    if(dis(s[i][a],s[j][b]) < diff*2) solver.add_clause(i, a^1, j, b^1);
                    //如果两个点距离小于直径,那么两个点不能同时选
    return solver.solve();
}

int main() {
    //freopen("f.txt","r",stdin);
    while(scanf("%d", &n) == 1 && n) {
        for(int i=0; i<n; i++)
            for(int j=0; j<2; j++)scanf("%d%d",&s[i][j].x,&s[i][j].y);
        double L = 0, R = 1e5;
        for(int i=0; i<50; i++) {
            double M = L + (R-L+1)/2;
            if(test(M)) L = M;
            else R = M-1;
        }
        printf("%.2lf\n", L);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值