poj1258 agri-net (PRIM+HEAP)

Source: Internet
Author: User

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&AMP;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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.