BZOJ 1083 题解

1083: [SCOI2005]繁忙的都市

Time Limit: 10 Sec  Memory Limit: 162 MB
Submit: 2431  Solved: 1596
[Submit][Status][Discuss]

Description

  城市C是一个非常繁忙的大都市,城市中的道路十分的拥挤,于是市长决定对其中的道路进行改造。城市C的道
路是这样分布的:城市中有n个交叉路口,有些交叉路口之间有道路相连,两个交叉路口之间最多有一条道路相连
接。这些道路是双向的,且把所有的交叉路口直接或间接的连接起来了。每条道路都有一个分值,分值越小表示这
个道路越繁忙,越需要进行改造。但是市政府的资金有限,市长希望进行改造的道路越少越好,于是他提出下面的
要求: 1. 改造的那些道路能够把所有的交叉路口直接或间接的连通起来。 2. 在满足要求1的情况下,改造的
道路尽量少。 3. 在满足要求1、2的情况下,改造的那些道路中分值最大的道路分值尽量小。任务:作为市规划
局的你,应当作出最佳的决策,选择那些道路应当被修建。

Input

  第一行有两个整数n,m表示城市有n个交叉路口,m条道路。接下来m行是对每条道路的描述,u, v, c表示交叉
路口u和v之间有道路相连,分值为c。(1≤n≤300,1≤c≤10000)

Output

  两个整数s, max,表示你选出了几条道路,分值最大的那条道路的分值是多少。

Sample Input

4 5
1 2 3
1 4 5
2 4 7
2 3 6
3 4 8

Sample Output

3 6

HINT

Source

Solution

Kruskal

 1 /**************************************************************
 2     Problem: 1083
 3     User: shadowland
 4     Language: C++
 5     Result: Accepted
 6     Time:32 ms
 7     Memory:2856 kb
 8 ****************************************************************/
 9  
10 #include "bits/stdc++.h"
11  
12 using namespace std ;
13 struct MST { int x , y , val ; } ;
14 const int maxN = 100100 ;
15 const int INF = 2147483647 ;
16 typedef long long QAQ ;
17  
18 MST MST_e[ maxN ] ;
19 int father[ maxN ] ;
20  
21 void Init_Set ( const int n ) { for ( int i=1 ; i<=n ; ++i ) father[ i ] = i ; }
22 inline bool cmp ( MST a , MST b ) { return a.val < b.val ;}
23 inline int gmax ( int x , int y ) { return x > y ? x : y ; } 
24 int getfa ( const int x ) { father[ x ] == x ? x : father[ x ] = getfa ( father[ x ] ) ;}
25 inline void Union_Set ( int x , int y ) { father[ x ] = y ; } 
26  
27 int MST ( const int N , const int M ) {
28         int _cnt = 0 , _max = -INF ;
29         Init_Set ( N ) ;
30         sort ( MST_e + 1 , MST_e + M + 1 , cmp ) ;
31         for ( int i=1 ; i<=M ; ++i ) {
32             int px = getfa ( MST_e[ i ].x ) ;
33             int py = getfa ( MST_e[ i ].y ) ;
34             if ( px != py ) {
35                     ++ _cnt ;
36                     Union_Set ( px , py ) ;
37                     _max = gmax ( _max , MST_e[ i ].val ) ;
38             }
39             if ( _cnt == N - 1 ) break ;
40         }
41         cout << _cnt << ' ' ; 
42         return _max ;
43 }
44 int main ( ) {
45         int N , M ; 
46         scanf ( "%d %d" , &N , &M ) ;
47         for ( int i=1 ; i<=M ; ++i ) {
48             scanf ( "%d %d %d" , &MST_e[ i ].x , &MST_e[ i ].y , &MST_e[ i ].val ) ;
49         }
50         int Ans = MST ( N , M ) ;
51         cout << Ans << endl ; 
52         return 0 ;
53 } 
View Code

2016-10-12 19:58:54

(完)

posted @ 2016-10-12 20:02  SHHHS  阅读(141)  评论(0编辑  收藏  举报