【NOIP校内模拟】T2 飞越行星带(kruskal)

啥玩意儿啊 题都没读懂

飞船要飞过这个行星带 就必须穿过每个行星形成的瓶颈

于是我们把每个行星想象成一个点 形成的瓶颈就是与其他点相连的边

相当于一个最小生成树了 直到s t联通

当然 这样做有点难理解 还可以类似的二分+并查集做

#include<bits/stdc++.h>
#define N 805
#define eps 1e-6
using namespace std;
int n,father[N],s,t;
double L;
struct Edge
{
    int from,to;
    double val;
}edge[N*N];
struct Point
{
    double x,y;
}p[N];
int tot;
inline void addedge(int x,int y,double z)
{
    tot++;
    edge[tot].from=x; edge[tot].to=y; edge[tot].val=z;
}
inline int getfather(int x)
{
    if(father[x]==x)    return x;
    father[x]=getfather(father[x]);
    return father[x];
}
inline double dis(Point a,Point b)
{
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
inline bool cmp(const Edge &a,const Edge &b)
{
    return a.val<b.val;
}
inline bool Kruskal()
{
    father[n+1]=n+1,father[n+2]=n+2;
    sort(edge+1,edge+tot+1,cmp);
    for(int i=1;i<=tot;i++)
    {
        int fx=getfather(edge[i].from); int fy=getfather(edge[i].to);
        if(fx==fy)  continue;
        father[fx]=fy;  //
        if(getfather(n+1)==getfather(n+2))
        {
            cout<<fixed<<setprecision(3)<<edge[i].val;
            exit(0);
        }
    }
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);
    cin>>n>>L;
    s=n+1,t=n+2;
    for(int i=1;i<=n;i++)
    {
        father[i]=i;
        cin>>p[i].x>>p[i].y;
        for(int j=1;j<i;j++)    addedge(i,j,dis(p[i],p[j]));
        addedge(i,t,L-p[i].y);
        addedge(i,s,p[i].y);
    }
    Kruskal();
    return 0;
}

转载于:https://www.cnblogs.com/Patrickpwq/p/9896659.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值