【Topic】Minimum Spanning Tree Kruskal

Introduction

Kruskal algorithm is a clever use of union search to find the minimum spanning tree method. First, we make some points connected to each other in the undirected graph to be in the same connected block. Kruskal's algorithm imitates a connected block as a set. Kruskal first sorts all the edges in ascending order, and considers that each point is isolated and belongs to n independent sets. Then play each side in order. If this edge links two different sets, add these two edges to the minimum spanning tree, and these two different sets are merged; if the two points connected by this edge belong to the same set, skip until Pick up to n-1 edges.

Algorithm Description

  • Initialize and search the set, father[x]=x
  • to=0
  • Sort all edges from small to large using quicksort
  • for i=1~n // Loop all the edges sorted from small to large
    if this is an edge (u, v) where u and v do not belong to a unified set
    begin
    merging the sets around u and v, which is equivalent to adding the edge (u, v) , v) Join the minimum spanning tree
    tot:=tot+w(u,v)
    k+1
    If k=n-1, it means that the minimum spanning tree has been formed, then break
    end;

code

var
        x,y:array[0..2000]of longint;
        z,x1,y1:array[0..4000000]of longint;
        i,j,n,m,ans,k,max,min,xx,yy,t:longint;
        father:array[0..2000]of longint;
procedure kp(l,r:longint);//快排
var
        i,j,mid:longint;
begin
        i:=l;
        j:=r;
        mid:=z[(l+r) div 2];
        while i<=j do
        begin
                while z[i]<mid do inc(i);
                while z[j]>mid do dec(j);
                if i<=j then
                begin
                        x[0]:=x[i];
                        x[i]:=x[j];
                        x[j]:=x[0];
                        y[0]:=y[i];
                        y[i]:=y[j];
                        y[j]:=y[0];
                        z[0]:=z[i];
                        z[i]:=z[j];
                        z[j]:=z[0];
                        inc(i);
                        dec(j);
                end;
        end;
        if l<j then kp(l,j);
        if r>i then kp(i,r);
end;
function get(x:longint):longint;//找父亲
begin
        if father[x]=x then exit(x);
        father[x]:=get(father[x]);
        exit(father[x]);
end;
procedure hbfather(x,y:longint);//合并父亲
var
        fx,fy:longint;
begin
        fx:=get(x);
        fy:=get(y);
        if fx<>fy then father[fx]:=father[fy];
end;
begin
        readln(n,m);
        for i:=1 to n do
                readln(x[i],y[i],z[i]);
        kp(1,n);//排序
        for j:=1 to n do
                 father[j]:=j;
        k:=0;
        min:=0;
        for i:=1 to t do
        begin
                if get(x[i])<>get(y[i]) then
                begin
                        hbfather(x[i],y[i]);
                        ans:=ans+z[i];
                        inc(k);
                end;
                if k=n-1 then
                        breakend;
        if ans=maxlongint then
                writeln(-1)
        else
                writeln(ans);
end.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326047519&siteId=291194637