Topic Links: poj1258 agri-net
This question I did last month, is a water problem, today saw someone to do with Prim+heap, learned the next.
1#include <cstdio>2#include <cstring>3#include <algorithm>4#include <queue>5#include <vector>6#include <Set>7 #defineCLR (A, B) memset ((a), (b), sizeof ((a)))8 using namespacestd;9 Ten Const intINF =0x3f3f3f3f; One Const intN =101; A intLow[n], s[n]; - intn, ans, num; - structedge{ the intv,c; -Edge (int_v=0,int_c=0): V (_v), C (_c) {} - BOOL operator< (ConstEDGE&R)Const{ - returnR.C <C; + } - }; +Vector<edge>G[n]; A at intprim_heap () { -Priority_queue<edge>Q; - inti, U, V, W; - Edge p; -Ans =0;//minimum Spanning tree total weight value -num =0;//number of vertices that have been added to the minimum spanning tree inQ.push (Edge (0,0)); - while(!q.empty () && num <N) { top =q.top (); Q.pop (); +U =p.v; - if(S[u])Continue; theS[u] =1; *Ans + =p.c; $num++;Panax Notoginseng for(i =0; I < g[u].size (); ++i) { -v =g[u][i].v; the if(S[v] = =0){ +W =g[u][i].c; A if(W <Low[v]) { theLOW[V] =W; + Q.push (Edge (V, W)); - } $ } $ } - } - if(Num <N) the return-1; - returnans;Wuyi } the intMain () { - intI, J, X; Wu while(SCANF ("%d", &n) = =1){ - for(i =0; I < n; ++i) About g[i].clear (); $ for(i =0; I < n; ++i) - for(j =0; J < N; ++j) { -scanf"%d", &x); - G[i].push_back (Edge (j, X)); A } +CLR (S,0); CLR (Low, INF); theprintf"%d\n", Prim_heap ()); - } $ return 0; the}
View Code
poj1258 agri-net (PRIM+HEAP)