P2330 [SCOI2005] busy city [MST]

Title Description

C is a very busy city metropolis, the city road is very crowded, so the mayor decided to transform the way in which. Urban road C are distributed such that: there are n urban intersections, there are some road between intersections is connected, is connected up to a road between two intersections. The roads are two-way, and all the intersection directly or indirectly up. Each road has a value, the smaller the score the more the busy road, the need for reform. But the government's limited financial resources, the mayor want to road reconstruction, the better, so he made the following request:

1. Those road reconstruction can put all the intersections direct or indirect communication with them. 2. In the case of 1 to meet the requirements of the transformation of the road as little as possible. 3. In the case of 1,2 to meet the requirements of those road score the biggest score of the road reconstruction as small as possible.

Task: As City Planning Bureau you should make the best decisions, choose which roads should be constructed.

Input Format

The first line has two integers n, m denotes n-urban intersections, m roads.

Next is a description of m rows each road, u, v, c is connected to a road between intersections expressed u and v, score of c. (1≤n≤300,1≤c≤10000,1≤m≤100000)

Output Format

Two integers s, max, that you select a few roads, score maximum score of that road is.

Sample input and output

Input # 1
4 5
1 2 3
1 4 5
2 4 7
2 3 6
3 4 8
Output # 1
36 

ideas
  yellow question difficulty, map building run kruskal, the road must be a minimum number of intersection number -1, maintaining maximum edge weight value trees can be

CODE
. 1 #include <the iostream>
 2 #include <cstdio>
 . 3 #include <CString>
 . 4 #include <algorithm>
 . 5  
. 6  the using  namespace STD;
 . 7  const  int MAXN = + 1E3 . 7 ;
 . 8  
. 9  /// to sort all sides, denoted the first i is the small side: E [i] (. 1 <= i <m)
 10  /// initialized empty MST
 11  /// initialization connected components, so that each point self-contained independent connected components
 12 is  /// for (int I = 0; I <m; I ++) {
 13 is  ///    IF (e [i] .u and e [i] .v not in the same connected component) {
 14  ///      the edge e [i] was added the MST
 15 ///      combined e [i] .u and e [i] .v communication component located
 16  ///    }
 . 17  /// } 
18 is  
. 19  int FA [ 5050 ], n-, m, ANS, EU, EV, CNT ;
 20 is  
21 is  struct Node {
 22 is      int U, V, W;
 23 is } E [ 1000007 ];
 24  
25  int A [MAXN] [MAXN];
 26 is  
27  BOOL CMP (Node A, Node B)
 28  {
 29      return AW < BW ;
 30  }
 31 is  
32  int FID ( int X)
 33 is {
34     return x == fa[x] ? x : fa[x] = fid(fa[x]);
35 }
36 
37 void init(int n)
38 {
39     for(int i = 1; i <= n; i++) {
40         fa[i] = i;
41     }
42     ans = 0;
43     cnt = 0;
44 }
45 
46 bool unite(int r1, int r2)///冰茶鸡
47 {
 48      you fidroot1 = at (r1), fidroot2 = at (r2);
49      ow (fidroot1! = Fidroot2) {
 50          fa [fidroot2] = fidroot1;
51          return  true ;
52      }
 53      return  false ;
54  }
 55  
56  void kruskal ( you m)
 57  {
 58      sort (e + 1 , e + m + 1 , CMP)
59      for ( you i = 1 ; i <= m; i ++ ) {
 60         eu = fid(e[i].u);
61         ev = fid(e[i].v);
62         if(eu == ev) {
63             continue;
64         }
65         ans = max(ans, e[i].w);
66         fa[ev] = eu;
67         if(++cnt == n-1) {
68             break;
69         }
70     }
71 }
72 
73 int main()
74 {
75     scanf("%d %d",&n, &m);
76     init(n);
77     for ( int i = 1; i <= m; ++i ) {
78         scanf("%d %d %d",&e[i].u, &e[i].v, &e[i].w);
79     }
80     kruskal(m);
81     printf("%d %d\n",n-1, ans);
82     return 0;
83 }
View Code

 

Guess you like

Origin www.cnblogs.com/orangeko/p/12389148.html