https://vjudge.net/problem/POJ-1258
Template problem, nothing to say.
#include <iostream>#include<cmath>#include<cstring>#include<queue>#include<vector>#include<cstdio>#include<algorithm>#include<map>#include<Set>#defineRep (i,e) for (int i=0;i< (e); i++)#defineREP1 (i,e) for (int i=1;i<= (e); i++)#defineREPX (i,x,e) for (int i= (x); i<= (e); i++)#defineX First#defineY Second#definePB push_back#defineMP Make_pair#defineMset (Var,val) memset (Var,val,sizeof (VAR))#defineSCD (a) scanf ("%d", &a)#defineSCDD (A, b) scanf ("%d%d", &a,&b)#defineSCDDD (a,b,c) scanf ("%d%d%d", &a,&b,&c)#definePD (a) printf ("%d\n", a)#defineSCL (a) scanf ("%lld", &a)#defineSCLL (A, b) scanf ("%lld%lld", &a,&b)#defineSclll (a,b,c) scanf ("%lld%lld%lld", &a,&b,&c)#defineIOS Ios::sync_with_stdio (false); Cin.tie (0)using namespaceStd;typedefLong Longll;template<classT>voidTest (T a) {cout<<a<<Endl;} Template<classTclassT2>voidTest (T a,t2 b) {cout<<a<<" "<<b<<Endl;} Template<classTclassT2,classT3>voidTest (T a,t2 b,t3 c) {cout<<a<<" "<<b<<" "<<c<<Endl;} Template<classT>inlineBOOLScan_d (T &ret) { CharCintSGN; if(C=getchar (), c==eof)return 0; while(c!='-'&& (c<'0'|| C>'9')) c=GetChar (); SGN= (c=='-')?-1:1; RET= (c=='-')?0:(C-'0'); while(C=getchar (), c>='0'&&c<='9') ret = ret*Ten+ (C-'0'); RET*=SGN; return 1;}//const int N = 1E6+10;Const intINF =0x3f3f3f3f;Constll INF =0x3f3f3f3f3f3f3f3fll;Constll mod =1000000000;intT;voidtestcase () {printf ("Case %d:",++T);}Const intMAXN =2500 ;Const intMAXM =550;Const DoubleEPS = 1e-8;Const DoublePI = ACOs (-1.0);BOOLVIS[MAXN];intLOWC[MAXN];intCOST[MAXN][MAXN];intPrim (intN) { intans=0; Mset (Vis,false); vis[0]=true; for(intI=1; i<n;i++) lowc[i]=cost[0][i]; for(intI=1; i<n;i++){ intMinc =inf; intp =-1; for(intj=0; j<n;j++){ if(!vis[j]&&minc>Lowc[j]) {Minc=Lowc[j]; P=J; } } if(Minc = =inf) { return-1; } ans+=Minc; VIS[P]=true; for(intj=0; j<n;j++){ if(!vis[j]&&lowc[j]>Cost[p][j]) {Lowc[j]=Cost[p][j]; } } } returnans;}intMain () {#ifdef LOCAL freopen ("data.in","R", stdin);#endif //LOCAL intN; while(~SCANF ("%d",&N)) { for(intI=0; i<n;i++){ for(intj=0; j<n;j++) {scanf ("%d",&Cost[i][j]); } } intans=Prim (n); printf ("%d\n", ans); } return 0;}
POJ-1258 agri-net (minimum spanning tree)