http://poj.org/problem?id=3164
Test instructions
To find the minimum tree shape.
Ideas:
Set of Templates.
Cite a commentary from the Great God Blog: http://www.cnblogs.com/acjiumeng/p/7136604.html
The algorithm steps are as Follows:
1. Determine the connectivity of the graph, if not connected directly without solution, otherwise there must be a Solution.
2. For all points except the root node, select one of the least-weighted incoming edges, assuming that the pre-array is used to record the precursor, the F-array records the selected edge length, and the selected edge and Temp.
3. (can use and check the Set) to determine whether the selected edges constitute a ring, if not directly $ans+=temp$ and output ans, if any, then proceed to the next Step.
4. to the ring to implement a pinch operation, set the ring on a bit $v1,v2 ... Vi ..... vn$, The indented point is node, and the following changes are made to all point P that are not in the ring:
(1) the distance from point p to node is min{$a [p,vi]-f[vi]$} (a is an array of edge Sets)
(2) the distance from point node to P is min{$a [vi,p]$}
The understanding of Operation (1): first assume that all edges on the ring are selected, if the next selection of an edge into the ring, you can break the entry point and the entry point of the edge between the precursor, that is, to break the f[entry point], so the equivalent of a direct $a[p,node]$ assignment to min{$a [p,vi]-f[vi]$}.
Special Reminder: The subject has self-ring, can be deleted in advance, because it is useless.
1#include <iostream>2#include <algorithm>3#include <cstring>4#include <cstdio>5#include <sstream>6#include <vector>7#include <stack>8#include <queue>9#include <cmath>Ten#include <map> one#include <Set> a using namespacestd; -typedefLong Longll; -typedef pair<int,ll>pll; the Const intINF =0x3f3f3f3f; - Const intmaxn= ++5; - Const intmod=1e9+7; - + intn, m; - + struct point a { at Doublex, y; - }p[maxn]; - - structnode - { - intu,v; in Doublew; -}edge[maxn*maxn]; to + intpre[maxn],id[maxn],use[maxn]; - Double inch[maxn]; the * Doubledis (point a,point b) $ {Panax Notoginseng returnSQRT ((a.x-b.x) * (a.x-b.x) + (a.y-b.y) * (a.y-b.y)); - } the + DoubleMini_tree (intRootintNintM//the root, The number of nodes, the number of sides, the sequence number starting from 1 a { the Doubleans=0; + intu; - while(true) $ { $ for(intI=1; I<=n;i++)inch[i]=inf; - for(intI=1; i<=m;i++) - { the intu=edge[i].u; - intv=edge[i].v;Wuyi if(edge[i].w<inch[v]&&u!=V) the { - inch[v]=edge[i].w; wupre[v]=u; - } about}//Find the smallest entry edge $ for(intI=1; i<=n;i++) - { - if(i==root)Continue; -ans+=inch[i];//Add the right Side. a if(inch[i]==inf]//If there are no points in the arc there is no minimum tree chart + return-1; the } -Memset (id,-1,sizeof(id)); $Memset (use,-1,sizeof(use)); the intCnt=0; the for(intI=1; I<=n;i++)//Enumerate each point, search for loops the { the intv=i; - while(v!=root&&use[v]!=i&&id[v]==-1) in { theuse[v]=i; thev=pre[v]; about } the if(v!=root&&id[v]==-1)//when you find the ring, indent the Number. the { the++cnt; +id[v]=cnt; - for(u=pre[v];u!=v;u=pre[u]) theid[u]=cnt;Bayi } the } the if(cnt==0)//If there is no ring end program - break; - for(intI=1; I<=n;i++)//Number the rest of the not in the Ring. the if(id[i]==-1) theid[i]=++cnt; the for(intI=1; I<=m;i++)//Create a new diagram the { - intu=edge[i].u; the intv=edge[i].v; theedge[i].u=id[u]; theedge[i].v=id[v];94 if(edge[i].u!=Edge[i].v) theedge[i].w-=inch[v]; the } then=cnt;//update the number of nodes and root nodes98root=id[root]; about } - returnans;101 }102 103 intMain ()104 { the //freopen ("in.txt", "r", stdin);106 while(~SCANF ("%d%d",&n,&M))107 {108 for(intI=1; I<=n;i++) scanf ("%LF%LF",&p[i].x,&p[i].y);109 for(intI=1; i<=m;i++) the {111scanf"%d%d",&edge[i].u,&edge[i].v); the if(edge[i].u!=Edge[i].v)113edge[i].w=Dis (p[edge[i].u],p[edge[i].v]); the Elseedge[i].w=inf; the } the DoubleAns=mini_tree (1, n,m);117 if(ans==-1) printf ("Poor snoopy\n");118 Elseprintf"%.2f\n", ans);119 } - return 0;121}
POJ 3164 Command Network (minimum tree Diagram template title + Detailed)