Cod sursa(job #596748)

Utilizator vendettaSalajan Razvan vendetta Data 18 iunie 2011 21:21:43
Problema Arbore partial de cost minim Scor 70
Compilator fpc Status done
Runda Arhiva educationala Marime 2 kb
const f = 'apm.in'; g = 'apm.out';
type graf = record
        x, y, c : longint;
end;
type mat = record
        x, y : longint;
end;
type vec= array[0..400001] of graf;
var
    e : vec;
    ax : graf;
    n, m, i, j : longint;
    nsol, k, cc, cost : longint;
    t : array[0..200000] of longint;
    sol : array[0..400001] of mat;


procedure quick( var a : vec; st: longint; dr : longint);
    var
        min, max, mij : longint;
    begin
        min := st;
        max := dr;
        mij := a[(st+dr) div 2].c;
        repeat
            while a[min].c < mij do inc( min );
            while a[max].c > mij do dec( max );
            if min <= max then begin
                ax := a[min];
                a[min] := a[max];
                a[max] := ax;
                inc( min );
                dec( max );
            end;
        until min >= max;
        if st<max then quick(a,st,max);
        if min<dr then quick(a,min,dr);
    end;

procedure reunire(i, j : longint );
    var
        k : longint;

    begin
        i := t[i];
        j := t[j];
        if i <> j then
            for k := 1 to n do
                if t[k]=i then t[k] := j;

    end;

procedure kruskal();
    var
        k : longint;
    begin

        quick(e, 1, m );

        for i := 1 to n do t[i] := i;

        for k := 1 to m do begin
            i := e[k].x;
            j := e[k].y;
            cc := e[k].c;
            if ( t[i] = t[j] ) then continue;
            reunire( i, j );
            //inc( cost, cc );
            cost := cost + cc;
            inc( nsol );
            sol[nsol].x := i;
            sol[nsol].y := j;
        end;
        writeln( cost );

    end;

begin
    assign( input,f ); reset( input );
    assign( output,g ); rewrite( output );
    readln( n, m );

    for i := 1 to m do
        readln(e[i].x, e[i].y, e[i].c);
    cost := 0;
    kruskal();
    writeln( nsol );
    for i := 1 to nsol do writeln( sol[i].x,' ',sol[i].y );
end.