HDU1233 or smooth traffic project

topic:

Problem Description

A provincial survey of rural traffic, statistics obtained lists the distance between any two villages. Target provincial government "Smooth Traffic Project" is to make the province between any two villages can achieve road traffic (but not necessarily directly connected to the road, as long as you can reach by road indirectly) and the total length of roads paved requirements a minimum. Calculate the minimum total length of roads.
 

 

Input

Test input contains several test cases. Each test case is given row of the first number of villages N (<100); followed by N (N-1) / 2 row corresponding to the distance between the villages, each row is given a pair of positive integers, respectively, two villages number, and the distance between these two villages. For simplicity, the village numbered from 1 to N.
When N is 0, an input end, which is not treated cases.
 

 

Output

For each test case, the output of the minimum total length of the road in a row.
 

 

Sample Input

3
1 2 1
1 3 2
2 3 4
4
1 2 1
1 3 4
1 4 1
2 3 3
2 4 2
3 4 5
0

Sample Output

3
5
Hint
Hint
Huge input, scanf is recommended.  

Ideas:

  • Structure with two binding therebetween from the village and
  • And using disjoint-set minimum spanning tree, is used here kruskal minimum spanning tree algorithm.
  • When the need to use a minimum spanning tree sort sorted STL containers, and sorting and comparison function need to use cmp

Code:

. 1 #include <the iostream>
 2 #include <algorithm>
 . 3  #define MAXN 99 * 50
 . 4  the using  namespace STD;
 . 5  
. 6  int pre [ 105 ]; // store predecessor node of the current node
 . 7  int n-, m;
 . 8      
. 9  struct storage node {// edge structure
 10      int the begin;
 . 11      int End;
 12 is      int len; 
 13 is  } S [MAXN];
 14  
15  BOOL CMP (Node A, Node B)
 16  {
 . 17      return a.len <b.len; // front side of a small discharge
 18 is  }
 . 19  
20 is  void the init ( int n-) // initialized to the individual nodes
 21 is  {
 22 is      for ( int I = . 1 ; I <= n-; I ++ )
 23 is          pre [I] = I;
 24  }
 25  
26 is  int find ( int X) // find the root node of the current node
 27  {
 28      the while (X =! pre [X])
 29          X = pre [X];
 30      
31 is      return X;
 32  }
 33 is  
34  voidMerge ( int FX, int FY) // merge two different root node
 35  {
 36      IF (! FX = FY)
 37 [          pre [FX] = FY;
 38 is  }
 39  
40  int Kruskal ()
 41 is  {
 42 is      int the minlen = 0 ;
 43 is      Sort (S, S + m, CMP); // make the edge in ascending order
 44 is      for ( int I = 0 ; I <m; I ++ )
 45      {
 46 is          int FX = Find (S [I]. the begin);
 47          int FY =Find (S [I] .end);
 48          IF (! FX = FY)
 49          {
 50              Merge (FX, FY);
 51 is              the minlen + = S [I] .LEN; // merge the requirements and minimum path
 52          }
 53      } 
 54 is      return the minlen;
 55  } 
 56 is  
57 is  int main ()
 58  {
 59      the while (Scanf ( " % D " !, & n-) = the EOF && n-)
 60      {
 61 is          the init (n-);
 62 is          m = n-* (n-- . 1 ) / 2;
63         for(int i = 0; i < m; i++)
64         {
65             scanf("%d%d%d", &s[i].begin, &s[i].end, &s[i].len);
66         }
67         printf("%d\n", kruskal());
68     }
69     return 0;
70 }

to sum up:

 With the root node is not included in the side, otherwise it will constitute a loop.

 

Guess you like

Origin www.cnblogs.com/Anber82/p/11140981.html