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