洛谷P1991 无线通讯网

题目链接:无线通讯网 - 洛谷

题目大意是在最小生成树上求min(p-1,s+1)的边

最小生成树板子题, 开边的时候需要注意p<=500, 边数组要开(500+1)*500/2也就是125250, 不然可能会re

ac代码:

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <queue>
#include <cmath>
#include <cstring>
#include <string>
#include <stack>
#include <deque>
#include <map>
#include <set>
using namespace std;
#define ll long long
#define endl "\n"
#define rep(i, a, b) for (ll i = (a); i <= (b); i++)
#define repr(i, a, b) for (ll i = (a); i < (b); i++)
#define rrep(i, a, b) for (ll i = (b); i >= (a); i--)
#define rrepr(i, a, b) for (ll i = (b); i > (a); i--)
#define min(a,b) (a)<(b)?(a):(b)
#define max(a,b) (a)>(b)?(a):(b)

ll cnt,n,m,t,ant;
const int N=2e5+10;
ll arr[N];
ll x[N],y[N];
double ans[N];
string str;

inline ll read()
{
    char c = getchar();int x = 0,s = 1;
    while(c < '0' || c > '9') {if(c == '-') s = -1;c = getchar();}//是符号
    while(c >= '0' && c <= '9') {x = x*10 + c -'0';c = getchar();}//是数字
    return x*s;
}

ll find(ll x)
{
    if(x!=arr[x]) arr[x]=find(arr[x]);
    return arr[x];
}

double dis(ll a,ll b)
{
    return sqrt(pow(x[a]-x[b],2)+pow(y[a]-y[b],2));
}

struct Edge
{
    ll a,b;
    double w;
    bool operator<(const Edge &W)const
    {
        return w<W.w;
    }
}edge[N];

void kruskal()
{
    sort(edge,edge+cnt);
    // repr(i,0,cnt) cout<<edge[i].a<<" "<<edge[i].b<<" "<<edge[i].w<<endl;
    repr(i,0,cnt)
    {
        ll a=edge[i].a;
        ll b=edge[i].b;
        double w=edge[i].w;
        a=find(a);
        b=find(b);
        
        if(a!=b)
        {
            arr[a]=b;
            // cout<<a<<" "<<b<<" "<<w<<" "<<endl;
            ans[ant++]=w;
        }
        if(ant>=n-1) return;
    }
}

void solve()
{
    m=read();
    n=read();//点数
    rep(i,0,n) arr[i]=i;
    rep(i,1,n)
    {
        x[i]=read();
        y[i]=read();
    }
    cnt=0;
    rep(i,1,n)
        rep(j,i+1,n)
            edge[cnt++]={i,j,dis(i,j)};
    

    kruskal();
    printf("%.2lf",ans[ant-m]);
    return;
}

int main()
{
    solve();
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值