Link: Click to open link
The main idea:
There are n villages, N (N-1)/2 roads, enter which two villages and lengths each route connects to, so that all villages are connected (not all 22 connected)
The shortest distance to the paved road.
Problem Solving Ideas:
Use and check the basic and Kruskal algorithm. Use a structure to store the length of the two villages and highways connected by the route.
Reference code:
#include <stdio.h> #include <algorithm>using namespace std;int per[110];int n;struct node{int u,v,w;} A[10000];int CMP (node A,node b) {return A.W<B.W;} void Init () {for (int i=1;i<=n;i++) per[i]=i;} int find (int x) {if (per[x]==x) return X;elsereturn per[x]=find (per[x]);} BOOL Join (int x,int y) {int fx=find (x); int fy=find (y); if (fx!=fy) {Per[fx]=fy;return true;} Else{return false;}} int main () {while (scanf ("%d", &n)!=eof&&n) {int m=n* (n-1)/2;for (int i=0;i<m;i++) scanf ("%d%d%d", &a [I].U,&A[I].V,&A[I].W); init (); sort (a,a+m,cmp); int sum=0;for (int i=0;i<m;i++) {if (Join (A[I].U,A[I].V)) SUM+=A[I].W;} printf ("%d\n", sum);} return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Hdu 1233 is still a smooth project