Minimum tree (Zhu Liu algorithm) - Study Notes

Defined tree diagram: a directed graph, from a point u satisfies may traverse all points, and does not exist in FIG ring (edges are exactly points -1), the graph called a tree, where u It is the root of the FIG. For a directed graph, if we can generate on which all points of artwork including a tree T, and T satisfy all sides and the right of the minimum, then T is the picture of a minimum tree.

  By definition it is easy to think of the minimum spanning tree of FIG. However, Kruskal and prim algorithm can not be applicable to the case of a directed graph; minimum tree problem solving algorithms is the earliest Zhu Liu algorithm proposed in 1965, the time complexity of O (VE), used widely.

  According to the author's understanding, three cases Zhuliu algorithm generated after one kind of greedy determination process: We find the root of the minimum outer edge of each point in the original image G, with its weight in [v] FIG. So, we consider the original point and the edges of these new compositions of FIG. G ': If the loop does not exist in FIG (including self-loop), then G' is the smallest tree must ask, it is very straightforward. And if there is some point (the root u) can not find the edge, then this figure must traverse the full program does not exist. The key is the third case: If the ring appear, how we deal with the next step?

  If we now encounter into a side composed of the smallest ring c. Has k points on the ring, k edges, since each point is only one record into the side, where the communication by the in [v] of an edge in the absence of the external ring. What we need now is to tear down the ring on a certain edge in [v], and then select the outside edge with a connection point v such that the chain ring becomes a tree.

  Zhu Liu algorithm selection policy this step given is: we originally ring shrunk to a point. For point to the new point (point to point v on the assumed original loop) of each edge, so that we subtracted the weight in [v]; Thus, provided on the right side of all ring S, in [v] = w , the optimal choice of the piece into edge points e and v e is the cost of the original weight, then:

  (S - w) + cost = S + (cost - w)

  On the left indicates visually removed in [v] and the plus side of the piece of e process, on the right the value of the original loop plus "edge weight after the pretreatment according to the above approach." This equation shows that the new view of the compression goes point + pretreatment of FIG find generated again for the Minimum tree in [v] and continues the above-described three cases determination process, the resulting recursively thereof, with the original the minimum weight of the tree is the same . This is the core of Zhu Liu algorithm. So we continue to look for ring contraction point, the right process side, satisfied until no change or absence of far found minimum tree. Implemented and recursive cycle are possible.

  Description of the complexity of the algorithm: each recursive run again needs to find all edges in [v], and the worst case out of a reduced size of each ring 2 (on the loopback side when screened out election a, see the code), the removal of one point, is performed up to n-1 times reduction site will be able to get the results. According to the more familiar notation, in fact, the complexity is O (mn) of.

Code:

  1. #include <iostream>  
  2. #include <cstdio>  
  3. #include <cstring>  
  4. #include <cctype>  
  5. #include <climits>  
  6. #define maxn 110  
  7. #define maxm 10010  
  8. #define inf INT_MAX  
  9. using namespace std;  
  10. int n, m, root;  
  11. long long ans;  
  12. struct E {  
  13.     int u, v, w;  
  14. } Edge [MaxM];  
  15. int vis [maxn], pre [maxn], in [maxn], c [maxn]; // pre array record precursor to look for ring service, c denotes a point where the number of ring
  16. void zhu_liu () {  
  17.     while (1) {  
  18.         memset(in, 127, sizeof(in));  
  19.         for (int i = 1; i <= m; ++i) {  
  20.             int u = edge[i].u, v = edge[i].v, w = edge[i].w;  
  21.             if (v != u && w < in[v])  
  22.                 in[v] = w, pre[v] = u;  
  23.         }  
  24.         in[root] = 0;  
  25.         for (int i = 1; i <= n; ++i)  
  26.             if (in[i] > 2e9) {  
  27.                 years = -1; return;  
  28.             }  
  29.         int cnt = 0;  
  30.         memset(vis, 0, sizeof(vis));  
  31.         memset(c, 0, sizeof(c));  
  32.         for (int i = 1; i <= n; ++i) {  
  33.             ans + = in [i]; // the weights of each edge are accumulated in advance, not a ring, then preprocesses directly cut into 0, does not affect the results
  34.             int v = i;  
  35.             while (v != root && vis[v] != i && !c[v]) {  
  36.                 view [V] = i;  
  37.                 in the = [on];  
  38.             }  
  39.             if (v! = root &&! c [v]) {// is a ring
  40.                 c[v] = ++cnt;  
  41.                 for (int u = pre[v]; u != v; u = pre[u])  
  42.                     c[u] = cnt;  
  43.             }  
  44.         }  
  45.         IF (! cnt)  return; // no ring, find the answer
  46.         for (int i = 1; i <= n; ++i)  //建新图
  47.             if (!c[i]) c[i] = ++cnt;  
  48.         for (int i = 1; i <= m; ++i) {  
  49.             int u = edge[i].u, v = edge[i].v;  
  50.             edge[i].u = c[u], edge[i].v = c[v];  
  51.             if (c[u] != c[v])  
  52.                 edge [i] .w - = in [v]; // process the edge weight
  53.         }  
  54.         n = cnt, root = c [root]; // new to FIG.
  55.     }  
  56. }  
  57. int main () {  
  58.     scanf("%d %d %d", &n, &m, &root);  
  59.     int u, v, w;  
  60.     for (int i = 1; i <= m; ++i) {  
  61.         scanf("%d %d %d", &u, &v, &w);  
  62.         edge[i] = (E) {u, v, w};  
  63.     }  
  64.     zhu_liu ();  
  65.     printf("%lld", ans);  
  66.     return 0;  
  67. }  

Expand shrink become the original point of the smallest tree algorithms have not learned (I guess less than) , I first wrote about here.

Guess you like

Origin www.cnblogs.com/TY02/p/11122886.html