Kruskal algorithm to find the minimum spanning tree

Source: Internet
Author: User
Tags faa

Just write a template, the specific explanation will not speak, is a and check the application of the Set + greedy ideas.

Path compression is very useful, there is no compression when the tml three, after the compression of a lot faster, although still so slow

First of all, my compression method is to learn a bit and check the set:

1 intFind (intx)2 {3     intR=x;4      while(fa[r]!=r) r=Fa[r];5      while(x!=R) {6x=Fa[x];7fa[x]=R;8     }9     returnFa[x];Ten}

Non-recursive path compression, first find the Ancestor node, and then update the path from beginning to end each point, so that they directly point to the ancestor node

There is a recursive compression, code is not very understand, you can go to Baidu to learn a bit;

Then is the minimum spanning tree code, with a structure to save each pass the value and two nodes, the order from small to large, and then determine whether two points in a set, if the operation, if not on the operation, execution N-1 completed a minimum spanning tree

Rokua template Problem 3366:http://www.luogu.org/problem/show?pid=3366#sub

1#include <iostream>2#include <cstdio>3#include <algorithm>4#include <string>5 using namespacestd;6 #defineMAX 2000057 structnode{8     intLast , Next, Val;9 }node[max];Ten  One inteax5005],m,n,ans=0; A  - intFind (intx) - { the     intR=x; -      while(fa[r]!=r) r=Fa[r]; -      while(x!=R) { -x=Fa[x]; +fa[x]=R; -     } +     returnFa[x]; A } at  - voidWork () - { -     ints=0; -      for(intI=1; i<=m;i++){ -         if(s==m-1) Break; in         intFaa=Find (node[i].last); -         intfbb=Find (node[i].next); to         if(FAA! =FBB) { +fa[faa]=fbb; -ans+=Node[i].val; thes++; *         } $     }Panax Notoginseng } -  the BOOLCMP (node A, node B) + { A     returna.val<B.val; the } +  - voidInit () $ { $     inti; -Cin>>n>>M; -      for(i=1; i<=m;i++){ theCin>>node[i].last>>node[i].next>>Node[i].val; -     }WuyiSort (node+1, node+1+M, CMP); the      for(i=1; i<=n;i++){ -fa[i]=i; Wu     } - } About  $ intMain () - { - init (); - Work (); Acout<<ans; +     return 0; the}

Kruskal algorithm to find the minimum spanning tree

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.