Pagini recente » Cod sursa (job #931657) | Cod sursa (job #1799529) | Cod sursa (job #1388269) | Cod sursa (job #1539886) | Cod sursa (job #599079)
Cod sursa(job #599079)
const f = 'apm.in'; g = 'apm.out';
type muchie = record
x, y, cost : longint;
end;
type vector = array[0..400001] of muchie;
var
MC : vector;
aux : muchie;
n, m, i, j : longint;
ct, sf : longint;
TREE,rang : array[0..200001] of longint;
APM : array[0..200001] of muchie;
buf, buf1 : array[1..1 shl 17 ] of char;
procedure quick( var a : vector; st: longint; dr : longint);
var
min, max, mij : longint;
begin
min := st;
max := dr;
mij := a[(st + dr) div 2].cost;
repeat
while (a[min].cost < mij) do inc( min );
while (a[max].cost > mij) do dec( max );
if (min <= max) then begin
aux := a[min];
a[min] := a[max];
a[max] := aux;
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;
function grupa( i : longint ) : longint;
begin
if Tree[i] = i then Tree[i] := i
else Tree[i] := grupa(Tree[i]);
grupa := Tree[i];
end;
procedure reuneste(i, j : longint);
begin
i := grupa(i);
j := grupa(j);
if i<>j then begin
if rang[i]<rang[j] then tree[i] := j
else tree[j] := i;
if rang[i] = rang[j] then inc(rang[i]);
end;
end;
procedure kruskal();
var
x, y, i, costmin : longint;
begin
quick(MC, 1, m );
ct := 0;
costmin := 0;
for i := 1 to n do begin rang[i] := 0;TREE[i] := i;end;
//x := MC[j].x; y := MC[j].y;
for i := 1 to m-1 do begin
x := mc[i].x;
y := mc[i].y;
if grupa(X)=grupa(y) then continue;
reuneste(x,y);
costmin := costmin + MC[i].cost;
inc( ct );
APM[ct] := MC[i];
end;
writeln( costmin );
end;
begin
assign( input,f ); reset( input );
assign( output,g ); rewrite( output );
settextbuf( input,buf );
settextbuf( output,buf1 );
readln( n, m );
for i := 1 to m do
readln(MC[i].x, MC[i].y, MC[i].cost);
kruskal();
writeln( n-1 );
for i := 1 to n-1 do writeln( APM[i].x,' ',APM[i].y );
end.