Title: Click to open link
#include <stdio.h> #include <algorithm>using namespace std;int n,m;int per[105];struct stu{int u,v,w;} Edge[105];void init () {int i;for (i=1;i<=m;i++) per[i]=i;} int find (int x) {if (x==per[x]) return X;elsereturn per[x]=find (per[x]);} BOOL Join (int x,int y) {int fx=find (x), Fy=find (y), if (fx!=fy) {Per[fx]=fy;return true;} Elsereturn false;} int cmp (stu A,stu b) {return A.W<B.W;} int main () {while (scanf ("%d%d", &n,&m), n) {init (); int i,sum=0,k=0;for (i=0;i<n;i++) scanf ("%d%d%d",& EDGE[I].U,&EDGE[I].V,&EDGE[I].W); sort (edge,edge+n,cmp);//order n for (i=0;i<n;i++) {if (Join (EDGE[I].U, EDGE[I].V)) SUM+=EDGE[I].W;} for (i=2;i<=m;i++) {if (Find (i)!=find (i-1)) k=1;} if (k) printf ("\ n"); elseprintf ("%d\n", sum);} return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Hdu OJ 1863 unblocked project