Cod sursa(job #596747)

Utilizator vendettaSalajan Razvan vendetta Data 18 iunie 2011 21:04:51
Problema Arbore partial de cost minim Scor 50
Compilator fpc Status done
Runda Arhiva educationala Marime 1.57 kb
const f = 'apm.in'; g = 'apm.out';
type graf = record
        x, y, c : longint;
end;
type mat = record
        x, y : longint;
end;

var
    e : array[0..400001] of graf;
    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 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

        for i := 1 to m do
            for j := i+1 to m do
                if e[i].c > e[j].c then begin
                    ax := e[i];
                    e[i] := e[j];
                    e[j] := ax;
                end;

        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.