Title Link: agri-net
Minimum spanning tree water problem, the array opened and the title of the same, but is re, has filled a 0, fortunately this problem with Kuruskal knocked over, found a point problem, previously written in the Library Lu Carl template a bit of a problem, write a step useless operation, has been amended.
Test instructions: A farmer wants to choose the mayor .... Build a road so that it can reach all points, the shortest distance.
scanf input
CIN input
#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include < algorithm>const int N = 11000;using namespace std;int n,ans,l;struct node{int u,v,w;} Edge[10000];int father[n];int cmp (const void *a,const void *b) {node *x = (struct node *) A; Node *y = (struct node *) b; return x->w-y->w;} int find (int x) {return (father[x]==x)? ( x): Find (Father[x]);} void Kruskal () {ans = 0; int UU,VV; for (int i = 0;i<n;i++) father[i] = i; for (int i = 0;i<l;i++) {UU = find (EDGE[I].U); VV = find (EDGE[I].V); if (UU!=VV) {Father[uu] = VV; Ans + = EDGE[I].W; }} Cout<<ans<<endl;} int main () {int A; while (cin>>n) {L = 0; for (int i = 0;i<n;i++) {for (int j = 0;j<n;j++) {cin>>a; EDGE[L].U = i; EDGE[L].V = j; EDGE[L].W = A; l++; }} qsort (Edge,l,sizeof (edge[0]), CMP); Kruskal (); } return 0;}