Minimum spanning tree ------------ busy city

City C is a very busy metropolis, and the roads in the city are very crowded, so the mayor decided to transform the roads in it.
The roads of city C are distributed like this:
There are nn intersections in the city, numbered 1∼n1∼n. Some intersections are connected by roads, and at most one road is connected between two intersections.
These roads are two-way and connect all the intersections directly or indirectly.
Each road has a score. The smaller the score, the busier the road and the more need to be rebuilt.
However, the city government has limited funds, and the mayor hopes that the fewer roads to be reformed, the better, so he puts forward the following requirements:
1. The reconstructed roads can connect all intersections directly or indirectly.
2. When the requirement 1 is met, the roads to be reconstructed should be as few as possible.
3. When the requirements 1 and 2 are met, the maximum value of the points on the reconstructed roads should be as small as possible.
As a city planning bureau, you should make the best decision and choose which roads should be built.
Input format The
first line has two integers n, mn, m, which means that there are nn intersections and mm roads in the city.
The next mm line is a description of each road. Each line contains three integers u, v, cu, v, and c, indicating that there is a road between the intersection uu and vv, and the score is cc.
Output format
Two integers s, maxs, max, which indicate how many roads you have selected, and the score of the road with the highest score.
Data range
1≤n≤3001≤n≤300,
1≤m≤80001≤m≤8000,
1≤c≤100001≤c≤10000
Input sample:
4 5
1 2 3
1 4 5
2 4 7
2 3 6
3 4 8

Sample output:
3 6


```#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 3010, M = 10010;
int p[N];
int n, m;
struct Edge{
 int a, b, w;
 bool operator<(const Edge &t)const{
        return w < t.w;
 }
}E[M];
int find(int x){
 if (p[x] != x)   p[x] = find(p[x]);
 return p[x];
}
int main(){
 cin >> n >> m;
  for (int i = 1; i <= n; i ++)   p[i] = i;
  for (int i = 0; i < m; i ++){
  int a, b, c;
  cin >> a >> b >> c;
  E[i] = {a, b, c};
 }
  sort(E, E + m);
  int res = 0;
 for (int i = 0; i < m; i ++){
  int a = find(E[i].a), b = find(E[i].b), w = E[i].w;
  if (a != b){
   p[a] = b;
   res = w;
  }
 }
  cout << n - 1 << ' ' << res << endl;
  return 0;
}
164 original articles published · Like 112 · Visitors 6780

Guess you like

Origin blog.csdn.net/qq_45772483/article/details/105397751