Poj--3164--command Network "Zhu Liu Algorithm" minimum tree diagram

Source: Internet
Author: User

Links: http://poj.org/problem?id=3164

Test instructions: Tells N Point coordinates, m-bars indicate a path between two points. Start from 1 to create a graph minimum spanning tree.

Zhu Liu Algorithm template problem

========================== Cutting line excerpt from

User_id=sasuke_scut "style=" Color:rgb (202,0,0); Text-decoration:none; font-family:arial; font-size:14px; Line-height:26px ">sasuke_scutthe blog==================================================
The smallest tree diagram , which is to assign a special point root to a weighted graph, asks for a root-rooted, forward-spanning trees T. And the total weight of all sides in T is the smallest. The first algorithm for the minimum tree diagram is the one proposed by Zhu Yongzin and Liuzhenhong in the 1965 for the Complexity of O (VE).
It is very easy to infer the existence of a tree diagram, and it is only necessary to traverse the graph with V as the root, so the following algorithms no longer consider the absence of tree diagrams.
we need to clear all the self-loops in the diagram before all operations start. It is very obvious that the self-ring is impossible in any tree shape. Only by doing this, finally the complexity of the method is really guaranteed to be O (VE).
The first one is selected for each point outside the root, which is bound to be the smallest of all into the edge.

All of the smallest incoming edges are now selected. Assuming that there is no Shang in the boundary set, we can prove that the set is the smallest tree of the graph. The proof is not very difficult. Assuming there is a Shang, we are going to call this an artificial vertex called the ring. Change the right side of the graph at the same time. Suppose a point u is on that ring. and set the Benquan in this ring that points to U is in[u]. So for each edge starting from U (U, i, W), The edge of the new, I, W is connected in the diagram, where new is the newly added artificial vertex; For each edge entering u (i, u, W), Edge (I, new, W-in[u]) is established in the new diagram. Why the right to edge minus in[u], this is explained later. The steps of the algorithm are given here first. It is then possible to prove that the right of the smallest tree diagram in the new graph plus the right of the ring that was contracted in the old image is the right of the smallest tree diagram in the original.


The above conclusions do not prove. Now, according to the above conclusions, explain why the right of the edge is not changed, the right to enter the edge minus in [u]. For the minimum tree map T in the new diagram, the edge that points to the human node is E.

When the human node is expanded, E points to a ring.

If the original E is pointing to u, this time we will be the ring on the edge of U in[u] Delete, so that the original image of a tree. We will find that if the right W ' (e) of E in the new diagram is the right of E in the original image (E) minus In[u], then when we delete the in[u], and the E is restored to the original state, the right of the tree is still the right plus ring of the new graph, and this weight is the weight of the least-squares graph. So after the expansion of the node, we still get the smallest tree figure.

By gradually expanding all the artificial nodes, you will get the minimum tree diagram of the initial graph.
Assuming that the implementation is very clever. Can reach to find the smallest incoming edge O (e), find the Ring O (V), shrink O (e). It requires a little bit of skill to find the Ring O (V). So the complexity of each contraction is O (E), and then it shrinks a few times? Since we have taken away all the self-loops at the beginning, we can know that each ring consists of at least 2 points. After shrinking to 1 points. The total number of points decreased by at least 1.

When the entire graph shrinks to just 1 points, the smallest tree diagram does not need to be asked. So we're only going to do V-1 contractions at most. So the overall complexity of the natural is O (VE). Thus. Suppose that the self-ring is not removed at the beginning. The complexity of the theory is related to the number of self-loops.
======================== Cutting line excerpt from

User_id=sasuke_scut "style=" Color:rgb (202,0,0); Text-decoration:none; font-family:arial; font-size:14px; Line-height:26px ">sasuke_scut 's blog=====================================================

Simply put, in addition to the source point at each point to choose a minimum weight of the incoming edge, assuming that there is a ring there is also an excess of edge, the ring into a point of the point to be the next generation tree, until there is no ring.


Zhu Liu algorithm template, vertex subscript from 0 start

/* Minimum tree figure template-Zhu Liu Algorithm template description: Point marking must 0-(N-1) must be removed to its own point (to its own side of the Benquan infinite) */#include <cstring> #include <string> #include <fstream> #include <iostream> #include <iomanip> #include <cstdio> #include <cctype># include<algorithm> #include <queue> #include <map> #include <set> #include <vector># include<stack> #include <ctime> #include <cstdlib> #include <functional> #include <cmath >using namespace std; #define PI ACOs ( -1.0) #define MAXN 50100#define EPS 1e-7#define INF 0x7fffffff#define seed 131#defi NE mod 1000000007#define ll long long#define ull unsigned ll#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1st    ruct node{int u,v; double Dis;} Edge[10100];int pre[110],id[110],vis[110];int n,m;int x[110],y[110];d ouble in[110];d ouble Directed_MST (int root,int Nv,int NE) {DOUBLE ret = 0;while (true) {//1. Find minimum in-Edge for (int i=0;i<nv;i++) in[i] = inf;for (int i=0;i<ne;i++) {int u = EDG E[i].u;int v = edge[i].v;if (Edge[i].dis < In[v] && u! = V) {Pre[v] = u;in[v] = Edge[i].dis;}}        for (int i=0;i<nv;i++) {if (i = = root) continue;if (in[i] = = INF) return-1;//The root does not reach it}//2 except the root is a bit not in the edge. Find Ring int cntnode = 0;        memset (id,-1,sizeof (ID)); memset (vis,-1,sizeof (Vis)); In[root] = 0;for (int i=0;i<nv;i++) {//mark each ring ret + = In[i];int v = i;while (vis[v]! = i &AMP;&A mp ID[V] = = 1 && v! = root) {Vis[v] = I;v = Pre[v];} if (v! = root && id[v] = =-1) {for (int u = pre[v]; u = v; u = pre[u]) {id[u] = Cntnode;} ID[V] = Cntnode + +;}} if (Cntnode = = 0) break;//no ring for (int i=0;i<nv;i++) if (id[i] = = 1) {Id[i] = Cntnode + +;} 3. Pinch point, Mark again for (int i=0;i<ne;i++) {int v = edge[i].v;edge[i].u = ID[EDGE[I].U];EDGE[I].V = Id[edge[i].v];if (Edge[i]. U! = edge[i].v) {Edge[i].dis-= In[v];}} NV = Cntnode;root = Id[root];} return ret;}    int main () {int i,j,a,b;    Double temp;        while (scanf ("%d%d", &n,&m)!=eof) {for (i=1;i<=n;i++) {scanf ("%d%d", &x[i],&y[i]); } for (i=0;i<m; i++) {scanf ("%d%d", &a,&b);            edge[i].u = A-1;            EDGE[I].V = b-1;            if (a==b) Edge[i].dis = INF;                else{temp = (X[a]-x[b]) * (X[a]-x[b]) + (Y[a]-y[b]) * (Y[a]-y[b]);                temp = sqrt (temp);            Edge[i].dis = temp;        }} Double ans = directed_mst (0,n,m);        if (Ans==-1) puts ("poor Snoopy");    else printf ("%.2lf\n", ans); } return 0;}


Poj--3164--command Network "Zhu Liu Algorithm" 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.