#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>#include <vector>#include <queue>#include <set>#include <map>#include <string>#include <math.h>#include <stdlib.h>#include <time.h>using namespace STD;structdata{intU,v;DoubleW;} e[ the];BOOLCMP (data A, data b) {returnA.W<B.W;}Doublex[ -+5],y[ -+5];intn,m,bin[ the];intFind (intx) {intS for(s=x;bin[s]>=0; s=bin[s]); while(s!=x) {intT=BIN[X]; Bin[x]=s; x=t; }returns;}voidUnion (intX1,intX2) {intF1=find (x1), F2=find (x2);intT=BIN[F1]+BIN[F2];if(Bin[f1]>bin[f2]) {bin[f1]=f2; bin[f2]=t; }Else{bin[f2]=f1; bin[f1]=t; }}intMain () {inti,j,cas=0, Num,u,v;DoubleT,sum; while(~scanf("%d%d", &n,&m)) {if(n==0) Break; for(i=0; i<=m;i++) bin[i]=-1; for(i=0; i<n;i++) {scanf("%d%d%d", &E[I].U,&E[I].V,&E[I].W); } sort (e,e+n,cmp); sum=0; num=0; for(i=0; i<n;i++) {u=e[i].u; V=E[I].V;if(Find (U)!=find (v)) {SUM+=E[I].W; Union (U,V); num++; }if(num>=m-1) Break; }if(num>=m-1)printf("%d\n", sum);Else printf("? \ n"); }return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
HDU 1863 unblocked Project