Link:
#include <stdio.h>int main(){ puts("转载请注明出处[vmurder]谢谢"); puts("网址:blog.csdn.net/vmurder/article/details/44935891");}
My previous version
Algorithm construction process and silly fork Code + weak version comments See previous blog
http://blog.csdn.net/vmurder/article/details/38819711
Minimum tree chart: noun Explanation:
&NBSP; &NBSP; &NBSP; is actually the smallest spanning tree that has a graph, and then needs to have a root (which is typically 1 by default), and if it's a tree without root, we can sacrifice the complexity of time, o ( n ) Enumeration root run min tree chart.
Algorithm Flow:
- For a graph with a direction, each point except the root is found with a minimum weighted edge (referred to as [minimum arc]).
- This may constitute a forest of radical ring trees, and then we will indent each ring and reconstruct the map according to the new number of points. But if there is no ring (all points connected to the root), the smallest tree diagram has been built, jumping out of the process.
If you are calculating edge rights:
Each time the minimum arc length of all points (except the root) is accumulated, the Benquan value associated with the X is subtracted from all the minimum arc lengths, thus guaranteeing that all the minimum arc lengths added in the process of the last point are equal to the original length of the last added edge.
Code:
#include <cmath>#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#define INF 0x3f3f3f3f#define N#define M 50000using namespace STD;structfiona{Doublex, y;} S[n];structsyndra{intU,v;DoubleW;} E[M];intPre[n],f[n],vis[n];DoubleL[n];DoubleDist (Fiona A,fiona B) {return sqrt((a.x-b.x) * (a.x-b.x) + (A.Y-B.Y) * (A.Y-B.Y));DoubleDirected_mst (intRootintNintm) {intI,j,k;intu,v,cnt;Doubleans=0; while(1) { for(i=1; i<=n;i++) f[i]=vis[i]=0, L[i]=inf; for(i=1; i<=m;i++) {u=e[i].u;v=e[i].v;if(L[V]>E[I].W) PRE[V]=U,L[V]=E[I].W; } for(i=1; i<=n;i++)if(L[i]==inf&&i!=root)return-1; L[root]=cnt=0; for(i=1; i<=n;i++) {ans+=l[i];if(Vis[i])Continue; for(v=i;! VIS[V]&&V!=ROOT;VIS[V]=I,V=PRE[V]);if(v!=root&&vis[v]==i) {f[v]=++cnt; for(U=pre[v];u!=v;u=pre[u]) f[u]=cnt; } }if(!cnt) Break; for(i=1; i<=n;i++)if(!f[i]) f[i]=++cnt; N=cnt,cnt=0, Root=f[root]; for(i=1; i<=m;i++) {u=e[i].u,v=e[i].v;if(F[u]!=f[v]) {E[++cnt].u=f[u]; E[CNT].V=F[V]; E[CNT].W=E[I].W-L[V]; }} m=cnt; }returnAns;}intMain () {Freopen ("Test.in","R", stdin);intI,n,m;DoubleAns while(scanf("%d%d", &n,&m)!=eof) { for(i=1; i<=n;i++)scanf("%LF%LF", &S[I].X,&S[I].Y); for(i=1; i<=m;i++) {scanf("%d%d", &E[I].U,&E[I].V);if(E[I].U!=E[I].V) e[i].w=dist (S[E[I].U],S[E[I].V]);ElseI--, m--; } ans=directed_mst (1, n,m);if(ans==-1)printf("Poor snoopy\n");Else printf("%.2f\n", ans); }return 0;}
"POJ3164" Command Network minimum tree diagram template title rebuilt version