P1991 无线通讯网
题解
首先,考虑如何分配卫星电话使得 \(D\) 最小是比较困难的,所以我们考虑怎样的 D 可以使得卫星电话个数不小于联通块个数
由于 D 越小,联通块个数也就越小,所以具有单调性,考虑二分
优化:
最后的答案,一定是所有连通块内部,距离最长的树边(即失去该边之后,联通块变得不连通),由此我们想到了对边的长度进行排序,然后一个一个加入,统计树两边不属于同一联通块的个数(并查集,判断是否树边)
直至联通块个数为 \(S\) ,即树边个数为 \(p-s\)
code
#include<bits/stdc++.h>
#define ll long long
using namespace std;
double x[505],y[505];
double cal(int i,int j)
{
    return sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));
}
int fa[505];
int finds(int now){return fa[now]==now?now:fa[now]=finds(fa[now]);}
struct node
{
    int a,b;
    double v;
    bool operator<(const node &c)const
    {
        return c.v<v;
    }
};
void solve()
{
    int s,p;
    cin>>s>>p;
    for(int i=1;i<=p;i++) cin>>x[i]>>y[i];
    priority_queue<node> q;
    for(int i=1;i<=p;i++)
    {
        fa[i]=i;
        for(int j=i+1;j<=p;j++)
        {
            //printf("%.2lf\n",cal(i,j));
            q.push({i,j,cal(i,j)});
        }
    }
    double ans;
    int cnt=p-s;
    while(cnt)
    {
        auto [a,b,v]=q.top();
        q.pop();
        int fx=finds(a),fy=finds(b);
        if(fx==fy) continue;
        cnt--;//统计树边
        fa[fx]=fy;
        ans=v;
    }
    printf("%.2lf\n",ans);
}
int main()
{
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    int t=1;
    while(t--) solve();
    return 0;
}
 
                    
                
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号