The smallest tree diagram, which specifies a special point root for a weighted graph, asks for a root-rooted, forward spanning tree T, and minimizes the total weight of all edges in T.
Zhu Liu Algorithm template problem
#include <iostream>#include<algorithm>#include<cstdio>#include<cstdlib>#include<cstring>#include<string>#include<stack>#include<queue>#include<cmath>#include<vector>using namespaceStd;typedefLong LongLL;Const intn=1e2+5;Const intinf=0x3f3f3f3f;structnode{Doublex, y;} P[n];structedge{intu,v; DoubleW;} Edge[n*N];Double inch[N];intid[n],vis[n],pre[n],n,m;DoubleDisintUintv) { Doublex=p[u].x-p[v].x,y=p[u].y-p[v].y; returnsqrt (x*x+y*y); }DoubleZhuliu (intRtintNintm) { Doubleret=0; while(1){ for(intI=1; i<=n;++i)inch[i]=INF; for(intI=1; i<=m;++i) { if(edge[i].u!=edge[i].v&&edge[i].w<inch[EDGE[I].V]) {PRE[EDGE[I].V]=edge[i].u; inch[edge[i].v]=EDGE[I].W; } } for(intI=1; i<=n;++i)if(i!=rt&&inch[I]==inf]return-1; intCnt=0; memset (ID,-1,sizeof(ID)); memset (Vis,-1,sizeof(VIS)); inch[rt]=0; for(intI=1; i<=n;++i) {ret+=inch[i]; intv=i; while(vis[v]!=i&&id[v]==-1&&v!=RT) {Vis[v]=i; V=Pre[v]; } if(v!=rt&&id[v]==-1){ ++CNT; for(intu=pre[v];u!=v;u=Pre[u]) Id[u]=CNT; ID[V]=CNT; } } if(cnt==0) Break; for(intI=1; i<=n;++i)if(id[i]==-1) id[i]=++CNT; for(intI=1; i<=m;++i) { intu=edge[i].u,v=edge[i].v; EDGE[I].U=Id[u]; EDGE[I].V=Id[v]; if(Id[u]!=id[v]) edge[i].w-=inch[v]; } N=CNT; RT=Id[rt]; } returnret; }intMain () { while(~SCANF ("%d%d",&n,&m)) { for(intI=1; i<=n;++i) scanf ("%LF%LF",&p[i].x,&p[i].y); for(intI=1; i<=m;++i) {scanf ("%d%d",&edge[i].u,&edge[i].v); EDGE[I].W=INF; if(EDGE[I].U!=EDGE[I].V) edge[i].w=dis (EDGE[I].U,EDGE[I].V); } DoubleAns=zhuliu (1, n,m); if(ans<0) printf ("Poor snoopy\n"); Elseprintf"%.2f\n", ans); } return 0;}
View Code
POJ3164 Command Network Minimum tree diagram