Luo Gu P1991 wireless communication network

Thinking: Each post has a radio transceiver, it can be seen as a satellite telephone tree only for m, but in order to make the distance between the radio transceiver calls shortest post

        It is not considered a satellite phone, apparently the best minimum spanning tree, minimum edge distance minimum spanning tree

        Only the m satellite phone communication posts, the minimum spanning tree in the m-th post can see this as a super source, at this time becomes a minimum spanning tree n-m + 1 nodes, fewer edges m-1

        In order to make apparent the shortest distance, by deleting the longest edges m-1

        While the strip m-1 the longest edge connector post does not belong to the same communication block, but the longest m-1 post side end of the connection strip as Super P source, this edge is not required and p super child node communicates with other post source

       And loj10066 like a new beginning

Note: 1. The title m≤n Some otherwise be out of bounds array subscript nm <0

#include <bits / STDC ++ H.>
 the using  namespace STD;
 const  int N = 1.3e5 + . 5 ;
 struct P {
     int X, Y; 
} P [ 505 ];
 struct E {
     int X, Y; // int W 
    Double W ;
 / *     operator <(E a) const {// overload <reload a parameter () two parameter [Error] NO match for 'operator <' (the operand types are 'E' and 'E') 
        return W <AW; 
    } * /  
} E [N]; // the ISO C ++ type allowed sTATEMENT 'operator <' [- fpermissive] 
BOOL CMP (E a, E B) {
     return a.w<BW; 
} 
/ * struct {EDGE 
    int Next, to, W; 
    operator <(EDGE A) { 
        return W <AW; 
    } 
} Edge [N]; 
void the Add (int U, V int) { 
   Edge [++ TOT] = head .next [U]; 
   edge [TOT] .to = V; 
   edge [TOT] .W = dist (U, V); 
   // head [V] = U; origin edge set 
   head [u] = tot ; 
} * /  
int TOT, head [N], FA [ 505 ], Rank [ 505 ];
 // Double D [505] [505]; adjacency matrix nor can obtain the start and end of a given weight 
Double ANS [ N]; 
 Double dist ( int I, int J) {
     returnsqrt ((p [i] .xp [j] .x) * (p [i] .xp [j] .x) + (p [i] .yp [j] .y) * (p [i]. YP [J] .y)); // do not need to be P [i] .xp [i] .y double function type is defined as a double converter type automatically 
}
 int Find ( int X) {
     int R & lt = X, now ;
     the while (X = FA [X]!) X = FA [X]; 
    now = R & lt;
     the while (now =! X) { 
        R & lt = FA [now]; 
        FA [now] = X; 
        now = R & lt; 
    } 
    return X; // missed 
}
 void Unio ( int X, int Y) { //union是关键字 
    int fx=find(x),fy=find(y);
    if(rank[fx]<rank[fy])fa[fx]=fy;
    else{
        if(rank[fx]==rank[fy])rank[fx]++;
        fa[fy]=fx;
    }
}
int main(){//链式前向型 无法知道起点 
    int n,m,cnt=0;
    cin>>m>>n;
    for(int i=1;i<=n;i++)cin>>p[i].x>>p[i].y,fa[i]=i;
/*    for(int i=1;i<=n;i++)
     for(int j=i+1;j<=n;j++)
       add(i,j);*/
      for(int i=1;i<=n;i++)
        for(int j=i+1;j<=n;j++){
            e[++tot].x=i;
            e[tot].y=j;
            e[tot].w=dist(i,j);
        }
//    sort(e+1,e+1+tot);
    sort(e+1,e+1+tot,cmp);
    for(int i=1;i<=tot;i++){
        int fx=find(e[i].x),fy=find(e[i].y);
        if(fx==fy)continue;
        unio(fx,fy);
        ++cnt;
        ans[cnt]=e[i].w;
        if(cnt==n-1)break;
    }
    printf("%.2f",ans[cnt-m+1]);//ans[n-m]
    return 0;
}

Guess you like

Origin www.cnblogs.com/wyh447154317/p/12228682.html