Zhu-Liu Algorithm)

Source: Internet
Author: User

Definition: A directed graph that has reached all the smallest spanning trees starting from a certain vertex.

I have been turning over materials since the morning and finally understood a little bit. The general process of the Zhu-Liu algorithm is as follows:

1. Find the smallest inbound edge except for the root vertex. Use in [I] to record

2. If the root user thinks there are other isolated points, there is no minimum tree structure.

3. Find all the rings in the graph, scale down the rings, and rename them.
4. Update the distance between other points and points on the ring, for example:

The points in the circle are (VK1, vk2 ,... , Vki) a total of I, which is replaced by a scaled vertex called VK. In the compressed graph, the distance between all the other vertices V and VK that are not in the ring is defined as follows:
GH [v] [VK] = min {GH [v] [vkj]-mincost [vkj]} (1 <= j <= I)

The distance from VK to V is
GH [VK] [v] = min {GH [vkj] [v]} (1 <= j <= I)

5. Repeat 3, 4 and know there is no ring.

 

As shown in:

The algorithm process is very simple, but it uses a lot of coding techniques .... Find a good template poj 3164 in HH:

View code

# Include <iostream> # include <cstdio> # include <cmath> # include <vector> # include <cstring> # include <algorithm> # include <string> # include <set> # include <ctime> # include <queue> # include <map> # include <sstream> # define Cl (ARR, val) memset (ARR, Val, sizeof (ARR) # define rep (I, n) for (I) = 0; (I) <(N ); ++ (I) # define for (I, L, H) for (I) = (l); (I) <= (h ); ++ (I) # define Ford (I, H, L) for (I) = (h); (I)> = (l); -- (I) # define L (x) <1 # define R (x) <1 | 1 # define mid (L, R) (L + r)> 1 # define min (x, y) x <Y? X: y # define max (x, y) x <Y? Y: X # define e (x) (1 <(x) const double EPS = 1e-6; const double INF = ~ 0u> 1; typedef long ll; using namespace STD; const int n = 110; const int M = 10010; struct node {Double X, Y;} Point [N]; struct EDG {int U, V; double cost;} e [m]; double in [N]; int ID [N]; int vis [N]; int pre [N]; int NV, Ne; double Sq (int u, int v) {return SQRT (point [u]. x-point [v]. x) * (point [u]. x-point [v]. x) + (point [u]. y-point [v]. y) * (point [u]. y-point [v]. y);} double directed_mst (INT root) {Dou Ble ret = 0; int I, U, V; while (true) {rep (I, NV) in [I] = inf; rep (I, NE) {// find the smallest inbound edge u = E [I]. u; V = E [I]. v; If (E [I]. cost <in [v] & U! = V) {in [v] = E [I]. cost; Pre [v] = u;} rep (I, NV) {// if there are isolated points other than root, there is no minimum tree if (I = root) continue; // printf ("%. 3lf ", in [I]); If (in [I] = inf) Return-1;} int CNT = 0; CL (ID,-1 ); CL (VIS,-1); In [root] = 0; rep (I, NV) {// find the ring RET + = in [I]; int v = I; while (vis [v]! = I & ID [v] =-1 & V! = Root) {vis [v] = I; V = pre [v];} If (V! = Root & ID [v] =-1) {// re-label for (u = pre [v]; u! = V; u = pre [u]) {ID [u] = CNT;} ID [v] = CNT ++;} If (CNT = 0) break; rep (I, NV) {If (ID [I] =-1) ID [I] = CNT ++; // re-label} rep (I, NE) {// update the distance from other points to the Ring v = E [I]. v; E [I]. U = ID [E [I]. u]; E [I]. V = ID [E [I]. v]; If (E [I]. u! = E [I]. v) {e [I]. cost-= in [v];} NV = CNT; root = ID [root];} return ret;} int main () {// freopen ("data. in "," r ", stdin );...}

 

It is about determining the root. If it is about an infinitus root, We Can Virtualize a root so that the distance from the virtual root to each node is the sum of the weights of all edges on the graph. In this way, find the minimum tree structure and then subtract the weight of all edges and add one.

For example, HUD 2121

View code

# Include <iostream> # include <cstdio> # include <cmath> # include <vector> # include <cstring> # include <algorithm> # include <string> # include <set> # include <ctime> # include <queue> # include <map> # include <sstream> # define Cl (ARR, val) memset (ARR, Val, sizeof (ARR) # define rep (I, n) for (I) = 0; (I) <(N ); ++ (I) # define for (I, L, H) for (I) = (l); (I) <= (h ); ++ (I) # define Ford (I, H, L) for (I) = (h); (I)> = (l); -- (I) # define L (x) <1 # define R (x) <1 | 1 # define mid (L, R) (L + r)> 1 # define min (x, y) x <Y? X: y # define max (x, y) x <Y? Y: X # define e (x) (1 <(x) const int EPS = 1e-6; const int INF = ~ 0u> 1; typedef long ll; using namespace STD; const int n = 1024; const int M = N * n; struct EDG {int U, V; int cost ;} E [m]; int in [N]; int ID [N]; int vis [N]; int pre [N]; int NV, Ne; int minroot; int directed_mst (INT root) {int ret = 0; int I, U, V; while (true) {rep (I, NV) in [I] = inf; rep (I, NE) {// find the smallest inbound edge u = E [I]. u; V = E [I]. v; If (E [I]. cost <in [v] & U! = V) {in [v] = E [I]. cost; If (u = root) minroot = I; // cannot be directly equal to V, because the pre [v] = u ;}} rep (I, NV) {// If an isolated point other than root exists, the minimum tree structure if (I = root) Continue does not exist; If (in [I] = inf) Return-1 ;} int CNT = 0; CL (ID,-1); CL (VIS,-1); In [root] = 0; rep (I, NV) {// find the ring RET + = in [I]; int v = I; while (vis [v]! = I & ID [v] =-1 & V! = Root) {vis [v] = I; V = pre [v];} If (V! = Root & ID [v] =-1) {// re-label for (u = pre [v]; u! = V; u = pre [u]) {ID [u] = CNT;} ID [v] = CNT ++;} If (CNT = 0) break; rep (I, NV) {If (ID [I] =-1) ID [I] = CNT ++; // re-label} rep (I, NE) {// update the distance from other points to the Ring v = E [I]. v; E [I]. U = ID [E [I]. u]; E [I]. V = ID [E [I]. v]; If (E [I]. u! = E [I]. v) {e [I]. cost-= in [v];} NV = CNT; root = ID [root];} return ret;} int main () {// freopen ("data. in "," r ", stdin); int I, L, X; while (~ Scanf ("% d", & NV, & ne) {L = 0; X = ne; rep (I, NE) {scanf ("% d", & E [I]. u, & E [I]. v, & E [I]. cost); L + = E [I]. cost;} l ++; rep (I, NV) {e [NE]. U = NV; E [NE]. V = I; E [NE]. cost = L; ne ++;} NV ++; int ans = directed_mst (NV-1); If (ANS =-1 | ans> = 2 * l) puts ("impossible"); else printf ("% d \ n", ANS-L, minroot-x); cout <Endl;} return 0 ;}

 

 

 

 

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.