Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1372
Previous Example 2: Template question
OJ is really strange, sometimes a sometimes W,
# Include <iostream> # include <stdio. h> # include <string. h> # include <stdlib. h>Using namespaceSTD;StructNode{IntX,Y,Z;}Q[100*101/2+1];IntM,N;IntSum=0;IntBin[101];IntCMP(ConstVoid*A,ConstVoid*B){Return(*(StructNode*)A).Z-(*(StructNode*)B).Z;}IntFindx(IntX){IntR=X;While(Bin[R]! =R){R=Bin[R];}IntJ=X;IntK;While(J! =R){K=Bin[J];Bin[J] =R;J=K;}ReturnR;}VoidMerge(IntX,IntY){// Int FX = findx (x); // int FY = findx (y); // sometimes W occurs on different platforms such as OJ, because findx () has a return value.If(X! =Y){Bin[X] =Y; // Save time }}VoidK(){IntL=1;For(IntI=0;I<M;I++ ){If(L> =N)Break;If(Findx(Q[I].X)! =Findx(Q[I].Y)){Merge(Q[I].X,Q[I].Y);L++;Sum=Sum+Q[I].Z;}}If(L=N)Printf("% D \ n",Sum);ElsePrintf("? \ N");}IntMain(){While(Scanf("% D",&M,&N)&&M! =0){Sum=0;For(IntI=1;I<=N;I++)Bin[I] =I;For(IntI=0;I<M;I++ ){Scanf("% D",&Q[I].X,&Q[I].Y,&Q[I].Z);}Qsort(Q,M,Sizeof(Q[0]),CMP);K();}Return0;}