POJ 3164 Command Network: Minimum tree template problem

Source: Internet
Author: User
Tags bool min pow time limit

Link:
http://poj.org/problem?id=3164
Topic:

Command Network
Time limit:1000ms Memory limit:131072k
Total submissions:8922 accepted:2609

Description

After a long lasting war on words, the A war on arms finally breaks out between Littleken ' s and Knuthocean ' s kingdoms. A sudden and violent assault by Knuthocean ' Force has the rendered a total failure of Littleken ' s command network. A Provisional network must be built immediately. Littleken orders Snoopy to take charge of the project.

With the situation studied to every detail, Snoopy believes the most urgent ' s-to-enable Littenken ' commands T o Reach every disconnected node in the destroyed network and decides on a plan to build a unidirectional communication net Work. The nodes are distributed on a plane. If Littleken ' s commands are to is able to is delivered directly from a node A to another node B, a wire would have to is BU Ilt along the straight line segment connecting the two nodes. Since it ' s in wartime, not between all pairs of nodes can wires is built. Snoopy wants the "plan" to require the shortest total length of wires so, the construction can be done very soon.

Input

The input contains several test cases. Each test case starts with a line containing two integer N (n≤100), the number of nodes in the destroyed network, and M (m≤104), the number of pairs of nodes between which a wire can be built. The next N lines each contain a ordered pair Xi and Yi, giving the Cartesian coordinates of the nodes. Then follow M lines each containing two integers i and J between 1 and N (inclusive) meaning a wire can is built between no De I and Node J for unidirectional command delivery from the former to the latter. Littleken ' s headquarter is always located at node 1. Process to end of file.

Output

For each test case, output exactly one line containing the shortest total length of wires to two digits past the decimal p Oint. In the cases this such a network does not exist, just output ' poor Snoopy '.

Sample Input

4 6
0 6
4 6
0 0
7 20
1 2
1 3
2 3
3 4
3 1
3 2
4 3
0 0
1 0
0 1
1 2
1 3
4 1
2 3

Sample Output
31.19
Poor Snoopy

Source
POJ monthly--2006.12.31, Galaxy

Analysis and Summary:

1. Did not pay attention to read the title, the unidirectional as a undirectional, a letter of difference, meaning is different, orz ... (Ps:unidirectional one-way)

2. Know is one-way, or very naïve to think that is the minimum spanning tree, using the prim algorithm, the result WA

3. The topic is the smallest tree graph, solves this problem the algorithm is Zhu Yongzin and Liuzhenhong in the last century 60 's solution (Zhu Liu algorithm), finally met the Chinese naming algorithm

Look for some information on the Internet, but the feeling for the self-contraction of the smallest tree-shaped picture of the most important parts are not clear enough, I really confused ah,

Baidu Encyclopedia that process map, for beginners is too complicated.

Finally saw this great God's blog to really understand, explain to be easy to understand, process picture is also very good, see understand, Big love AH

http://hi.baidu.com/lydrainbowcat/item/5fbae3fb9c159c5ec8f33753

The following is from this blog:

The topic is: given a direction graph, the root node is known, and the minimum tree graph of the direction graph is obtained. The minimum tree, the minimum spanning tree of a direction graph, is defined as: Select some edges so that the root node can reach all nodes in the graph and make the selected edges Benquan and minimal.

Title algorithm: Zhu-Liu Algorithm (that is, the Chinese Zhu Yongzin and Liuzhenhong together to invent the algorithm).

The algorithm steps are as follows: (this article no longer prove, refer to the following given by my own drawing of a diagram to understand)

1. Determine the connectivity of graphs, if not connected directly without solution, otherwise there must be solution.

2. For all points except the root node, select one of the smallest weights, assuming that the pre array record of the predecessor, F array record selection of the edge length, remember the selected edge and for temp.

3. (Can use and search the set) to determine whether the selection of the edge constitutes a ring, if not directly ans+=temp and output ans, if so, then proceed to the next step.

4. To the loop to implement the operation of the indentation, set the ring a bit v1,v2 ... Vi. ... Vn, the indented point is node, and the following changes are made to all point P that is not in the ring:

(1) The distance of point p to node is MIN{A[P,VI]-F[VI]} (A is an array of edges)

(2) The distance from point node to P is min{a[vi,p]}

Operation (1) of the understanding: assuming all the edges on the ring are selected, if the next selection of a side to enter the ring, you can disconnect the entry point and the entry point of the edge between the predecessor, that is, to disconnect the f[entry point], so the equivalent of the A[p,node is directly assigned to MIN{A[P,VI]-F[VI]}.

Special reminder: The subject has a self ring, can be deleted in advance, because it is not used.

In addition to this template problem, there are also template problems:

UVa 11183-teen Girl Squad

Tju 2248 Channel Design

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

Code implemented with adjacency matrix:

#include <cstdio> #include <iostream> #include <cstring> #include <cmath> using namespace std  
    
;  
const int VN = 105;  
    
const int INF = 0x7fffffff;   
        Template<typename type> class directed_mst{public:void init (int _n) {n=_n;  
        Ans = 0;  
        memset (Vis, 0, sizeof (VIS));  
        Memset (inc, 0, sizeof (INC));  
            for (int i=0; i<=n; ++i) {w[i][i] = INF;  
        for (int j=i+1; j<=n; ++j) W[i][j]=w[j][i]=inf;  
    } void Insert (int u, int v, Type _w) {if (w[u][v]>_w) w[u][v] = _w;  
        Type directed_mst (int u) {//== Step 1: Determine whether the smallest tree can be formed, direct DFS Traversal DFS (u);  
    
        for (int i=1; i<=n; ++i) if (!vis[i]) {return-1;}  
        = = If the minimum tree can be formed, continue memset (VIS, 0, sizeof (VIS)); while (true) {//== 1. Find the smallest precursor for (int i=1; i<=n; ++i) if (i!=u&&!inc[i]) {w[i][i]=inf, pre[i] = i;  
                for (int j=1; j<=n; ++j) if (!inc[j] && w[j][i]<w[pre[i]][i]) {pre[i] = j;  
            }//== 2. Determine if there is a ring int i;  
                For (I=1 i<=n; ++i) if (I!=u&&!inc[i]) {int j=i, cnt=0;  
                while (j!=u && pre[j]!=i && cnt<=n) j=pre[j], ++cnt; if (J==u | | cnt>n) continue;  
            No break found; //== did not find the ring, get the answer if (i>n) {for (int i=1; i<=n; ++i  
                ) if (I!=u &&!inc[i]) ans+=w[pre[i]][i];  
            return ans;  
            //== has a ring, for narrowing int j=i;  
            memset (Vis, 0, sizeof (VIS));  
            do{ans + = w[pre[j]][j], j=pre[j], vis[j]=inc[j]=true;  
            }while (j!=i); Inc[i] = fAlse;  
                The loop shrinks to the point I, the point I still exists//== contraction for (int k=1; k<=n; ++k) if (vis[k)) {//in the ring midpoint  
                    for (int j=1; j<=n; ++j) if (!vis[j]) {//Not in the ring point if (W[i][j] > W[k][j]) w[i][j] = W[k][j]; if (W[j][k]<inf && w[j][k]-w[pre[k]][k] < W[j][i]) w[j][i] = W[j  
                ][K]-w[pre[k]][k];        
    }} return ans;  
        Private://traverse from the root node to determine if there is a minimum tree-shape void dfs (int u) {Vis[u] = true;  
        for (int i=1; i<=n; ++i) if (!vis[i]&&w[u][i]<inf) {DFS (i);         } private:type ans;            The answer is int n;      Number of nodes int PRE[VN];     The precursor of the smallest weight value is bool VIS[VN];     is in the ring or in the outer ring bool INC[VN];   Whether the point was deleted (shrink) Type W[VN][VN];  
    
    
Figure};  
directed_mst<double>g;  
Double X[VN],Y[VN]; Inline Double getdist(Double x1,double y1,double x2,double y2)  
{return sqrt (POW (x1-x2,2) +pow (y1-y2,2));  
    int main () {int n,m;  
        while (~SCANF ("%d%d", &n,&m)) {g.init (n);  
        for (int i=1; i<=n; ++i) scanf ("%lf%lf", &x[i],&y[i));  
            for (int i=0; i<m; ++i) {int a,b;  
            scanf ("%d%d", &a,&b);  
            if (a==b) continue;  
        G.insert (A,b,getdist (x[a],y[a],x[b],y[b));  
        Double ans = g.directed_mst (1);  
        if (ans < 0) puts ("poor Snoopy");  
    else printf ("%.2f\n", ans);  
return 0; }

Author: csdn Blog shuangde800

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.