[PTA-tianyao training] the local minimum cost of smooth engineering, pta-smooth Engineering
After a survey on urban traffic conditions in a certain area, the statistical data of existing fast roads between towns is obtained, and the goal of "smooth engineering" is put forward: so that any two towns in the whole area can achieve rapid transportation (but not necessarily there are direct and fast roads connected, as long as they can be indirectly accessible through fast roads ). The statistical table of urban roads shows the cost of expressway construction between any two towns and whether the roads have been successfully repaired. Please write a program to calculate the lowest cost for smooth access across the region.
Input Format:
The first line of the input shows the number of villages N (1 ≤ N ≤ 100); the subsequent N (N − 1)/2 lines correspond to the cost and construction status of the inter-village roads: each row is given with four positive integers (numbers from 1 to N). The road cost between the two villages and the construction state-1 indicates that the two villages have been built, and 0 indicates that the two villages have not been built.
Output Format:
Outputs the lowest cost required for smooth operation across the province.
Input example:
41 2 1 11 3 4 01 4 1 12 3 3 02 4 2 13 4 5 0
Output example:
3
# Include <bits/stdc ++. h> using namespace std; int top [105]; int n, tot = 0; // The connectivity component is initially 0 struct edge // edge {int x, y, w; // x and y are the two vertices of the edge, w is the edge weight} a [10005]; int find (int r) {if (top [r]! = R) top [r] = find (top [r]); return top [r];} void init (int n) {for (int I = 1; I <= n; I ++) top [I] = I;} bool cmp (edge a, edge B) // the edge is arranged from small to large {return. w <B. w ;}int kruskal () {sort (a, a + tot, cmp); int cnt = 0, ans = 0, I; for (I = 0; I <tot; I ++) {int fx = find (a [I]. x), fy = find (a [I]. y); if (fx! = Fy) {top [fx] = fy; ans = ans + a [I]. w; cnt ++;} if (cnt = N-1) break;} return ans;} int main () {int x, y, w, d, I; cin> n; init (n); for (I = 0; I <n * (n-1)/2; I ++) {cin> x> y> w> d; a [tot]. x = x; a [tot]. y = y; a [tot ++]. w = w; if (d! = 0) {int fx = find (x), fy = find (y); if (fx! = Fy) top [fx] = fy ;}} printf ("% d \ n", kruskal (); return 0 ;}