Cod sursa(job #13338)

Utilizator andreitheo87Teodorescu Andrei-Marius andreitheo87 Data 6 februarie 2007 12:05:14
Problema Radiatie Scor 100
Compilator fpc Status done
Runda Arhiva de probleme Marime 2.11 kb
var fi,fo:text;
    x,y,tat,h:array[1..30010]of integer;
    h1,h2,i,n,m,k,t1,t2,xx,yy:integer;
    maxx:longint;
    c,cost:array[1..30010]of longint;
procedure qsort(li,ls:integer);
var aux,ii,jj,i,j:longint;
begin
 if li<ls then
  begin
        i:=li; j:=ls;
        ii:=1; jj:=0;
        while i<>j do
         begin
               if c[i]>c[j]then
                begin
                      aux:=x[i]; x[i]:=x[j]; x[j]:=aux;
                      aux:=y[i]; y[i]:=y[j]; y[j]:=aux;
                      aux:=c[i]; c[i]:=c[j]; c[j]:=aux;
                      aux:=ii; ii:=-jj; jj:=-aux;

                end;
               inc(i,ii); inc(j,jj);
         end;
        dec(i); inc(j);
        while (i>li) and (c[i]=c[i-1])do dec(i);
        while (j<ls) and (c[j]=c[j+1])do inc(j);
        qsort(li,i); qsort(j,ls);
  end;
end;
function tata(nod:integer):integer;
begin
 while tat[nod]<>0 do nod:=tat[nod];
 tata:=nod;
end;
begin
 assign(fi,'radiatie.in'); reset(fi);
 assign(fo,'radiatie.out'); rewrite(fo);
 readln(fi,n,m,k);
 for i:=1 to m do readln(fi,x[i],y[i],c[i]);
 qsort(1,m);
 for i:=1 to n do
  begin tat[i]:=0; h[i]:=0; end;
 for i:=1 to m do
  begin
        t1:=tata(x[i]); t2:=tata(y[i]);
        if t1<>t2 then
         if h[t1]<h[t2] then
          begin tat[t1]:=t2; cost[t1]:=c[i]; end
         else begin
                    tat[t2]:=t1; cost[t2]:=c[i];
                    if h[t1]=h[t2] then inc(h[t1]);
              end;
  end;
 for i:=1 to k do
  begin
        readln(fi,xx,yy);
        h1:=0; h2:=0;
        t1:=xx; t2:=yy;
        while t1<>0 do
         begin inc(h1); t1:=tat[t1]; end;
        while t2<>0 do
         begin inc(h2); t2:=tat[t2]; end;
        maxx:=-1;
        t1:=xx; t2:=yy;
        while t1<>t2 do
         if h1>h2 then
          begin
                if maxx<cost[t1] then maxx:=cost[t1];
                dec(h1); t1:=tat[t1];
          end
         else begin
                    if maxx<cost[t2] then maxx:=cost[t2];
                    dec(h2); t2:=tat[t2];
              end;
        writeln(fo,maxx);
  end;
 close(fo);
end.