1. 程式人生 > >【NOIP校內模擬】T2 飛越行星帶(kruskal)

【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;
}