B: minimum cost

B: minimum cost

 

 

Solution: First seek the minimum cost ans Unicom all points with minimum spanning tree

In the seeking of 1 when the maximum weight point mx

ans-mx is the answer

 

#include <the iostream> 
#include <algorithm> 
#include <Vector> 
#include <math.h>
 #define LL Long Long
 the using  namespace STD;
 int P [ 100005 ], R & lt [ 100005 ];
 int n-, m; 
LL ANS = 0 ; 
Vector < int > du [ 100005 ]; // calculation of 
struct Node 
{ 
  int X; // X, Y coordinates, v is weight 
  int Y;
   int V; 
} A [ 100005 ];
 BOOLCMP (Node B, Node C) 
{ 
  return BV < CV; 
} 
int Find ( int x) // find the owner who element x 
{
     IF (x == P [x])
         return x;
     the else 
        return P [x] = Find (P [X]); 
} 

void the Join ( int X, int Y) // path compressor merge two sets 
{
     int xRoot = Find (X);
     int yRoot = Find (Y); 

    IF (xRoot == yRoot ) // same boss, do not merge 
        return ;
     //. 1-CNT = CNT; 
    IF (R & lt [xRoot] <R & lt [yRoot]) // R & lt [i] where i is the height of the element tree, shrub recognize the root node of the tree root boss high 
        p [xRoot] = yRoot;
     the else  IF (R & lt [xRoot]> R & lt [yRoot]) 
        P [yRoot] = xRoot;
     the else 
    { 
        P [yRoot] = xRoot; // same high trees, tree height boss to add a 
        r [xRoot] + + ; 
    } 
} 
void Kruskal () 
{ 
  for ( int I = . 1 ; I <= n-; I ++) // initializes the root node 
    P [I] = I; 
  Sort (A + . 1 , A + m + . 1,cmp);
  for(int i=1;i<=m;i++)
  {
    if(find(a[i].x)!=find(a[i].y))
    {
      join(a[i].x,a[i].y);
      ans=ans+a[i].v;
      du[a[i].x].push_back(a[i].v);
      du[a[i].y].push_back(a[i].v);
    }
  }
}
int main()
{
    cin>>n>>m;
    for(int i=1;i<=m;i++)
        cin>>a[i].x>>a[i].y>>a[i].v;
    kruskal();
    int  mx=0 ;
     for ( int I = . 1 ; I <= n-; I ++ )
         IF (du [I] .size () == . 1 ) // find a degree of weight and a maximum value of the point 
            mx = max (mx, du [ I] [ 0 ]); 
    COUT << ANS-MX << endl;
     return  0 ; 
}

 

Guess you like

Origin www.cnblogs.com/-citywall123/p/11928407.html