BF的数据结构题单-提高组——P1783 海滩防御

题解:

二分+并查集超时了,然后想了下可以用生成树来做,答案一定是每个点的左右距离和两两距离和之内产生。

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
const double esp=1e-3;
int n,m,pre[N];
typedef pair<int, int> pp;
map<pp,int> mp;
vector<pp> g;
struct Node
{
    int u,v;
    double w;
};
vector<Node> vec;
double cal(pp i,pp j)
{
    return sqrt((1.0*abs(i.first-j.first)*abs(i.first-j.first))+(1.0*abs(i.second-j.second)*abs(i.second-j.second)));
}
bool cmp(pp a,pp b)
{
    return a.second<b.second;
}
double res=0;
int cmp1(Node a,Node b)
{
    return a.w<b.w;
}
int find(int x)
{
    if(pre[x]!=x) pre[x]=find(pre[x]);
    return pre[x];
}
void kru()
{
    sort(vec.begin(),vec.end(),cmp1);
    for(auto it:vec){
        int fx=find(it.u),fy=find(it.v);
        if(fx!=fy){
            res=max(res,it.w);
            pre[fx]=fy;
        }
        if(find(0)==find(m+1)) {
            break;
        }
    }
    printf("%.2lf\n",res);
}
void solve()
{
    int cnt=0;
    scanf("%d%d",&n,&m);
    if(m==0) {
        puts("0");
        return;
    }
    for(int i=0;i<=m+1;i++) pre[i]=i;
    for(int i=1;i<=m;i++){
        int a,b; scanf("%d%d",&b,&a);
        g.push_back({a,b});
        if(!mp[{a,b}]) mp[{a,b}]=++cnt;
    }
    for(auto i:g){
        for(auto j:g){
            if(i.first==j.first&&i.second==j.second) continue;
            vec.push_back({mp[i],mp[j],cal(i,j)/2.0});
//            cout<<"res : "<<cal(i,j)<<endl;
        }
    }
    for(auto it:g){
        vec.push_back({0,mp[it],it.second*1.0});
        vec.push_back({mp[it],m+1,1.0*n-it.second*1.0});
//        cout<<it.second<<' '<<n-it.second<<endl;
    }
    kru();
}
signed main()
{
    solve();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值