BZOJ 1083: [SCOI2005] Busy City

1083: [SCOI2005] Busy City

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

Description

  City C is a very busy metropolis, and the roads in the city are very congested, so the mayor decided to renovate the roads. The roads in city C
are distributed as follows: there are n intersections in the city, some intersections are connected by roads, and at most one road is connected between two intersections
. These roads are bidirectional and connect all intersections directly or indirectly. Each road has a score, and the smaller the score,
the busier the road is and the more it needs to be renovated. However, the funds of the municipal government are limited, and the mayor wants to renovate as few roads as possible, so he puts forward the following
requirements: 1. The reconstructed roads can connect all the intersections directly or indirectly. 2. Under the condition of meeting requirement 1, the
roads to be renovated should be as few as possible. 3. Under the condition that requirements 1 and 2 are met, the road with the largest score among the reconstructed roads should be as small as possible. Task: You, the city planning
bureau, should make the best decisions about which roads should be built.

Input

  The first line has two integers n, m indicating that the city has n intersections and m roads. The next m lines are descriptions of each road, u, v, c indicate that
there is a road connecting the intersection u and v, and the score is c. (1≤n≤300, 1≤c≤10000)

Output

  Two integers, s, max, indicate how many roads you have selected, and what is the score of the road with the highest score.

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

 

topic link

 

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 #include<cmath>
 5 #include<cstring>
 6 #include<vector>
 7  
 8 using namespace std;
 9 
10 template <typename tn> void read (tn & a) {
11     tn x = 0, f = 1;
12     char c = getchar();
13     while (c < '0' || c > '9'){ if (c == '-') f = -1; c = getchar(); }
14     while (c >= '0' && c <= '9'){ x = x * 10 + c - '0'; c = getchar(); }
15     a = f == 1 ? x : -x;
16 }
17 
18 const int MAXN = 100010;
19 int n, m, ans;
20 int fa[MAXN];
21 pair< int, pair<int, int> > g[MAXN];
22 
23 int find (int x){ return (fa[x] == x || fa[x] == 0) ? x : fa[x] = find(fa[x]); }
24 
25 int main() {
26     read(n);
27     read(m);
28     for (int i = 0; i < m; ++i) {
29         read(g[i].second.first);
30         read(g[i].second.second);
31         read(g[i].first);
32     }
33     sort(g, g + m);
34     ans = 0;
35     for (int i = 1; i <= n; ++i) fa[i] = i;
36     for(int i = 0; i < m; ++i) {
37         int x = find(g[i].second.first);
38         int y = find(g[i].second.second);
39         if (x == y) continue;
40         ans = g[i].first;
41         fa[x] = y;
42     }
43     printf("%d %d\n", n - 1, ans);
44     return 0;
45 }
View Code

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325041922&siteId=291194637