[Kuangbin take you to fly] topic six minimum spanning tree POJ 2421 constructing Roads

Source: Internet
Author: User

Give you a full map of n points and I'll give you the M-path. How long will it take you to get all the villages connected?

The path to the M points will be given a re-weighting of zero edges into the edge set and then the minimum spanning tree is obtained

1#include <cstdio>2#include <iostream>3#include <algorithm>4#include <cmath>5#include <cstring>6#include <string>7 #defineCL (b) memset (A,b,sizeof (a))8 #defineDebug (x) cerr<< #x << "= =" << (x) <<endl9 using namespacestd;TentypedefLong Longll; One  A Const intmaxn= the; -  - intf[maxn],tol,n,m; the  - struct_edge - { -     intu,v,w; +}edge[maxn*maxn*MAXN]; -  + BOOLCMP (_edge A,_edge B) A { at     returna.w<B.W; - } -  - voidAddedge (intUintVintW) - { -edge[tol].u=u; inedge[tol].v=v; -edge[tol].w=W; totol++; + } -  the int_find (intx) * { $     if(f[x]==-1)returnx;Panax Notoginseng     Else returnf[x]=_find (f[x]); - } the  + intKruskal () A { theCL (f,-1); +Sort (edge,edge+tol,cmp); -     intCnt=0, ans=0; $     intU,v,w,f1,f2; $      for(intI=0; i<tol; i++) -     { -u=edge[i].u; thev=edge[i].v; -w=EDGE[I].W;Wuyif1=_find (u); theF2=_find (v); -         if(f1!=F2) Wu         { -ans+=W; Aboutf[f1]=F2; $cnt++; -         } -         if(cnt==n-1) Break; -     } A     returnans; + } the  - intMain () $ { the      while(SCANF ("%d", &n)!=eof&&N) the     { theTol=0; the          for(intI=0; i<n;i++) -         { in              for(intj=0; j<n;j++) the             { the                 intx; Aboutscanf"%d",&x); the Addedge (i,j,x); the             } the         } +scanf"%d",&m); -          for(intI=0; i<m;i++) the         {Bayi             intx, y; thescanf"%d%d",&x,&y); theAddedge (x1, Y1,0); -Addedge (y1, X-1,0); -         } theprintf"%d\n", Kruskal ()); the     } the     return 0; the } - /* the  the 3 the 0 990 69294 990 0 179 the 692 179 0 the 1 the 1 298  About */

[Kuangbin take you to fly] topic six minimum spanning tree POJ 2421 constructing Roads

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.