Describe
http://www.lydsy.com/JudgeOnline/problem.php?id=1626
Give the coordinates of \ (n\) points, some of which are already connected, now to connect all points, to find the minimum length of the road.
Analysis
The minimum spanning tree problem with some edges is already connected.
Here incidentally reviewed the prim and Krusakal.
Proof of Prim:
To set the currently connected tree to \ (t\), the current smallest edge is \ (e\), we come to prove that \ (e\) must be in the minimum spanning tree \ (g\).
Assuming \ (e\) is not in \ (g\), the edge of Connectivity \ (g-t\) and \ (t\) must be greater (or equal) than \ (e\). At this time we join \ (e\) in \ (g\), will form a ring, remove the ring in the \ (e ' \), the tree is still connected, and the cost is smaller, this with \ (g\ ) is the minimum spanning tree contradiction. (If \ (e\) and \ (e ' \) are equal then although the cost is not smaller, that is, \ (e\) can no longer \ (g\), but we can also use \ (e\) to replace \ (e ' \), in other words, \ (e\) in \ (g\) is not wrong.)
So \ (e\) must be in the minimum spanning tree \ (g\).
Proof of Kruskal:
To set the minimum edge of the currently connected two non-connected components to \ (e\), we prove that \ (e\) must be in the minimum spanning tree \ (g\).
Assuming that \ (e\) is no longer \ (g\), then the sides that connect the two components \ (e ' \) must be larger (or equal) than \ (e\). At this time we add \ (e\) in the \ (g\), will form a ring, remove the ring in the \ (e ' \), the tree is still connected, and the cost is smaller, this is the minimum spanning tree contradiction with \ (g\). (If \ (e\) and \ (e ' \) are equal then although the cost is not smaller, that is, \ (e\) can no longer \ (g\), but we can also use \ (e\) to replace \ (e ' \), in other words, \ (e\) in \ (g\) is not wrong.)
1#include <bits/stdc++.h>2 using namespacestd;3 4 Const intmaxn= ++5;5 structpt{6 Doublex, y;7PtDoublex=0,Doubley=0): X (x), Y (y) {}8 }P[MAXN];9 structedge{Ten int from, to; One DoubleD; A Edge () {} -Edgeint from,intTo,DoubleD): from( from), to, D (d) {} - BOOL operator< (ConstEdge &RHS)Const{returnd<RHS.D;} the}g[maxn*MAXN]; - intN,m,cnt=1; - intF[MAXN]; - Doubleans; +InlineDoubleDis (pt a,pt b) {returnsqrt (Pow (a.x-b.x,2) +pow (A.Y-B.Y,2)); } -InlineintFindintx) {returnx==f[x]?x:f[x]=find (F[x]);} + intMain () { Ascanf"%d%d",&n,&m); at for(intI=1; i<=n;i++){ -scanf"%LF%LF",&p[i].x,&p[i].y); -f[i]=i; - } - for(intI=1; i<=m;i++){ - intU,v; scanf"%d%d",&u,&v); in intFu=find (u), fv=Find (v); - if(FU!=FV) f[fu]=fv,cnt++; to } + for(intI=1; i<=n;i++) for(intj=1; j<=n;j++) g[(i-1) *n+j]=Edge (I,j,dis (P[I],P[J)); -Sort (g+1, g+1+n*n); the inttot=n*N; * for(intI=1; i<=tot,cnt<=n;i++){ $ intFx=find (G[i]. from), fy=find (g[i].to);Panax Notoginseng if(fx!=FY) { -f[fx]=fy; theAns+=dis (P[g[i]. from],p[g[i].to]); +cnt++; A } the } +printf"%.2lf\n", ans); - return 0; $}
View Code
Bzoj_1626_[usaco2007_dec]_building_roads_ Construction Road _ (Kruskal)