[Minimum tree structure of a directed graph-zhu liu algorithm] Template

Source: Internet
Author: User

Find a learning URL for the tree in the smallest tree, like ~ Http://acm.nudt.edu.cn /~ The Minimum Spanning Tree of twcourse/tree.html undirected graphs can use prim. Algorithm Or the krusual algorithm, and the Minimum Spanning Tree of the directed graph is also called the Minimum Spanning Tree. It first fixes a root and then obtains the tree embedding template with the smallest weight.
# Define n 1005 # define type int # define Max int_maxstruct edge {int U, V; type cost;} e [40005]; int pre [N], Id [N], vis [N]; Type in [N]; int n, m; Type Zhuliu (INT root, int NV, int ne) {// root, number of knots, number of edges type ret = 0; while (true) {// 1. find the smallest inbound edge for (INT I = 0; I <NV; I ++) in [I] = max; For (INT I = 0; I <ne; I ++) {int u = E [I]. u; int v = E [I]. v; If (E [I]. cost <in [v] & U! = V) {// In this step, you can cut off the self-ring and find the smallest inbound edge of each vertex pre [v] = u; in [v] = E [I]. cost ;}}for (INT I = 0; I <NV; I ++) {if (I = root) continue; If (in [I] = max) return-1; // if there is no inbound edge except the heel, the root cannot reach it} // 2. int cntnode = 0; memset (ID,-1, sizeof (ID); memset (VIS,-1, sizeof (VIS); in [root] = 0; for (INT I = 0; I <NV; I ++) {// mark each ring RET + = in [I]; int v = I; while (vis [v]! = I & ID [v] =-1 & V! = Root) {vis [v] = I; // vis is used to mark each node in the ring as the vertex with an inbound edge, for example, to mark 1, 2, 3, 4, 5, 6 are marked as 1 V = pre [v];} If (V! = Root & ID [v] =-1) {for (INT u = pre [v]; u! = V; u = pre [u]) {ID [u] = cntnode;} ID [v] = cntnode ++ ;}} if (cntnode = 0) break; // For (INT I = 0; I <NV; I ++) if (ID [I] =-1) {ID [I] = cntnode ++ ;} // 3. scale down, re-mark, adjust the weight of the edge entering the ring, so that the minimum inbound edge can be found in the next cycle (INT I = 0; I <ne; I ++) {int 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 = cntnode; root = ID [root];} return ret ;}

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.