Minimum tree diagram "POJ 3164 Command Network"

Source: Internet
Author: User

Definition: The smallest spanning tree on a graph.


Algorithm process: The same as the minimum spanning tree, but this is not a graph, but it can also use a similar algorithm, the smallest tree diagram of the first algorithm number of Zhu Liu algorithm, based on the minimum number of spanning trees algorithm formation.

We know that in the minimum spanning tree algorithm, we select the shortest length of the edge each time, if the conditions are satisfied then join the smallest spanning tree, know that all the points are in the tree, the smallest graph is the same.

First, as with the minimum spanning tree, you must first ensure that the diagram is connected, otherwise the minimum tree shape can not be formed.

But because there is a direction, and we just need to find all the points in the edge of the smallest in the edge of the graph and is the smallest tree, but the most important thing is that it is possible to form a ring, we have to do is to shrink points, the formation of the ring of the place to shrink, and then all the edge of the ring into dis-in Dis is the distance from the original edge to the ring, and the smallest incoming edge of the point u on the "U" ring. And the edge is not the edge, so always shrink point, know that there is no ring, all the minimum value of the edge and is the minimum tree shape of the answer.


POJ 3164 Command Network to paste a template (note that VC compiled C + + submission before)

#include <iostream> #include <algorithm> #include <string> #include <math.h> #include < vector> #include <cstring> #include <cstdio>using namespace std;const int N = 110;const int inf = 0x3f3f3f3f; int n,m;struct node{double x, y;};    Node p[n];struct tree{int from,to; double Dis;}; vector<tree> e;double dis (Node a,node b) {return sqrt ((a.x-b.x) * (a.x-b.x) + (A.Y-B.Y) * (A.Y-B.Y));}    int Ha[n],vis[n],father[n];d ouble in[n];d ouble zhuliu (int root) {double ans = 0;        while (true) {for (int i=0;i<n;i++) in[i] = inf;        memset (father,-1,sizeof (father));            for (int i=0; i<e.size (); i++)//Find minimum entry side {int to = e[i].to;                if (E[i].dis<in[to] && e[i].from!=e[i].to) {in[to] = E[i].dis;            Father[to] = E[i].from;            }} for (int i=0;i<n;i++) {//printf ("%.2lf", In[i]);  if (i!=root && in[i]==inf)              return-1;        } int cnt = 0;        In[root] = 0;        memset (ha,-1,sizeof (ha));        memset (vis,-1,sizeof (VIS));            for (int i=0;i<n;i++)//find self ring {ans + = in[i];            int v = i;                while (V!=root && ha[v]==-1 && vis[v]!=i) {Vis[v] = i;            v = father[v];                } if (V!=root && ha[v]==-1) {for (int j = father[v];j! = V;j=father[j])                {Ha[j] = cnt;            } Ha[v] = cnt++;        }} if (cnt = = 0)//break out of condition;        for (int i=0;i<n;i++) if (ha[i]==-1) ha[i]=cnt++;            for (int i = 0; i< e.size (); i++) {int tmp = E[I].TO;            E[i].from = Ha[e[i].from];            E[i].to = ha[e[i].to];        if (e[i].from! = e[i].to) E[i].dis-= in[tmp];   } n = cnt;     root = Ha[root]; } return ans;    int main () {freopen ("Input.txt", "R", stdin); while (~SCANF ("%d%d", &n,&m)) {for (int i=0; i<n; i++) {scanf ("%lf%lf", &p[i].x,&        AMP;P[I].Y);            } for (int i=0; i<m; i++) {int from,to;            scanf ("%d%d", &from,&to);            if (from==to) continue;            From--, to--;            Double tmp = DIS (p[from],p[to]);            printf ("%.2LF", TMP);        E.push_back (Tree) {from,to,tmp});        } Double ans = zhuliu (0);        if (Ans==-1) puts ("poor Snoopy");        else printf ("%.2lf\n", ans);    E.clear (); } return 0;}


Minimum tree diagram "POJ 3164 Command Network"

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.