Cod sursa(job #254876)

Utilizator valytgjiu91stancu vlad valytgjiu91 Data 7 februarie 2009 22:30:10
Problema Arbore partial de cost minim Scor 20
Compilator fpc Status done
Runda Arhiva educationala Marime 1.41 kb
type muchie=record
u,v,cost:integer;
end;
graf=array[1..1000]of muchie;
var g:graf;
x,y,c:array[1..1000]of integer;
j,i,n,m,ms,k,a,b:integer;
ct:longint;
aux:muchie;
ok:boolean;
f,h:text;

procedure divid(st,dr:longint);
 var p,i,j:longint;
 begin
 i:=st;
 j:=dr;
 p:=g[(st+dr)div 2].cost;
 while (i<=j) do
    begin
      while (g[i].cost<p) do i:=i+1;
      while (g[j].cost>p) do j:=j-1;
     if i<=j then begin
              aux:=g[i];
              g[i]:=g[j];
              g[j]:=aux;
              i:=i+1;
              j:=j-1;
              end;
    end;
    if st<j then divid(st,j);
    if dr>i then divid(i,dr);
end;

begin
assign(f,'apm.in');
reset(f);
assign(h,'apm.out');
rewrite(h);
readln(f,n,m);
for i:=1 to m do
  readln(f,g[i].u,g[i].v,g[i].cost);
divid(1,m);
for i:=1 to n do
c[i]:=i;

ct:=0;
ms:=0;
i:=1;
k:=1;
while (i<=m)and (ms<N-1) do
begin
  if c[g[i].u]<>c[g[i].v]then
                     begin
                        ms:=ms+1;
                        ct:=ct+g[i].cost;
                        a:=g[i].u;
                        x[ms]:=a;
                        b:=g[i].v;
                        y[ms]:=b;
                        for j:=1 to n do
                           if c[j]=c[a] then c[j]:=c[b];
                        end;
  i:=i+1;
  end;
writeln(h,ct);
writeln(h,ms);
for i:=1 to ms do
  writeln(h,x[i],' ',y[i]);
close(h);
end.