POJ3164 Command Network Minimum tree diagram

Source: Internet
Author: User

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

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.