Poj 3164 Command Network)

Source: Internet
Author: User

Link:

Http://poj.org/problem? Id = 3164

Question:

Command Network
Time limit:1000 ms   Memory limit:131072 K
Total submissions:8922   Accepted:2609

Description

After a long lasting war on words, a war on arms finally breaks out between littleken's and knuthocean's kingdoms. a sudden and violent assault by knuthocean's force has 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 that the most urgent point is to enable littenken's commands to reach every disconnected node in the destroyed network and decides on a plan to build a unidirectional communication network. the
Nodes are distributed on a plane. if littleken's commands are to be able to be delivered directly from a node A to another node B, a wire will have to be built along the straight line segment connecting the two nodes. since it's in wartime, not between all
Pairs of nodes can wires be built. Snoopy wants the plan to require the Shortest total length of wires so that the construction can be done very soon.

Input

The input contains several test cases. Each test case starts with a line containing two integerN(N≤ 100), the number of nodes in the destroyed network, andM(M≤ 104), the number of pairs of nodes
Which a wire can be built. The nextNLines each contain an Ordered PairXIAndYi, Giving the Cartesian coordinates of the nodes. Then followMLines each containing two integersIAndJBetween
1 andN(Aggressive) meaning a wire can be built between nodeIAnd NodeJFor 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 point. In the cases that such a network does not exist, just output'poor snoopy'.

Sample Input

4 60 64 60 07 201 21 32 33 43 13 24 30 01 00 11 21 34 12 3

Sample output

31.19poor snoopy

Source

Poj monthly -- 2006.12.31, Galaxy


Analysis and Summary:

1. I did not pay attention to reading the question. I thought of unidirectional as undirectional. The difference between a letter is quite different. orz... (PS: unidirectional one-way)

2. After knowing it is a one-way method, I still naively think that it is the minimal spanning tree. With the prim algorithm, the result is wa.

3. This question is the smallest tree structure. the algorithms used to solve this problem were solved by Zhu Yongjin and Liu zhenhong in the 1960s S (zhu liu algorithm) and finally met an algorithm named by the Chinese people =

I found some information on the Internet, but I felt that I was not clear enough about the core part of the smallest tree structure due to auto-ring contraction,

The process diagram of Baidu encyclopedia is too complicated for beginners.

Finally, I really understood it after reading the blog of this great god. The explanation is easy to understand and the process diagram is also very good. I can understand it after reading it. I love it.

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

Refer to the blog below:

Given a directed graph, the root node is known and the minimum tree structure of the directed graph is obtained. The minimum tree structure is the smallest spanning tree of a directed graph. It is defined as: Selecting some edges so that the root node can reach all nodes in the graph and minimizing the Edge Weight and weight of the selected edges.

Question algorithm: Zhu-Liu algorithm (an algorithm jointly invented by Zhu Yongjin and Liu zhenhong ).

The algorithm steps are as follows: (this article does not prove it. Refer to a picture I have drawn below to understand it)

1. Determine the graph connectivity. If the graph is not connected, there is no solution. Otherwise, there must be a solution.

2. Select an inbound edge with the smallest weight for all vertices except the root node. Assume that the PRE array is used to record the precursor, the F Array records the selected edge length, and the selected Edge Weight and temp are recorded.

3. You can use and check the set to determine whether the selected edge is a ring. If no edge is set, ANS + = temp is used directly and ANS is output. If yes, perform the next step.

4. Perform the point-down operation on the ring, and set the ring to a bit V1, V2 ...... VI ...... Vn: The scaled point is node. Change the p of all vertices not in the ring as follows:

(1) the distance between P and node is min {A [p, VI]-f [VI]} (A is the number group of edge sets)

(2) The distance from node to P is min {A [VI, p]}.

Understanding of operation (1): assume that all edges on the ring are selected first. If you select an edge to enter the ring next time, you can disconnect the edges between the entry point and the start point, that is, the f [entry point] is disconnected, so it is equivalent to directly assigning a [p, node] To min {A [p, VI]-f [VI]}.

Note: This question has a self-loop and can be deleted in advance because it is useless.



In addition to this template question, there are also template questions:

Ultraviolet A 11183-teen girl squad

Tju2248 Channel Design



Code implemented using the adjacent matrix:

# Include <cstdio> # include <iostream> # include <cstring> # include <cmath> using namespace STD; const int VN = 105; const int INF = 0x7fffff; 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 minimum tree structure can be formed and direct DFS traversal (U); For (INT I = 1; I <= N; ++ I) if (! Vis [I]) {return-1;} // = if the minimum tree structure can be formed, continue memset (VIS, 0, sizeof (VIS); While (true) {// = 1. find the minimum front edge 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. judge whether 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; // The break is not found.} // = no ring is found. The answer is if (I> N) {for (INT I = 1; I <= N; ++ I) if (I! = U &&! INC [I]) ans + = W [pre [I] [I]; return ans;} // = with loops, perform 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 Ring is scaled into vertex I, and vertex I still exists // = for (int K = 1; k <= N; ++ K) if (vis [k]) {// point in the ring for (Int J = 1; j <= N; ++ J) if (! Vis [J]) {// 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 the root node to determine whether the smallest tree exists 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 int n; // number of nodes int pre [VN]; // The first edge bool vis [VN] with the smallest weight; // whether it is in the ring or outside the ring bool Inc [VN]; // whether the point is deleted (contracted) 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", & N, & M) {G. init (n); For (INT I = 1; I <= N; ++ I) scanf ("% lf", & X [I], & Y [I]); For (INT I = 0; I <m; ++ I) {int A, B; scanf ("% d", &, & 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 ;}

 
  

-- The meaning of life is to give it meaning.

Original Http://blog.csdn.net/shuangde800 , By d_double (reprinted, please mark)


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.