Cod sursa(job #376294)

Utilizator ionutz32Ilie Ionut ionutz32 Data 21 decembrie 2009 11:16:26
Problema Radiatie Scor 100
Compilator fpc Status done
Runda Arhiva de probleme Marime 2.66 kb
type muchie=record
     x,y:word;
     c:longint;
     end;
var tt,rg:array[1..15000] of word;
cost:array[1..15000] of longint;
s:array[1..15000] of byte;
v:array[1..30000] of muchie;
aux:muchie;
n,m,k,i,j,piv,b,x,y,str1,str2,a,max:longint;
f,g:text;
ok:boolean;
function sort2(min,max:word):word;
         begin
         piv:=v[min+(max-min) shr 1].c;
         i:=min-1;
         j:=max+1;
         ok:=true;
         repeat
               repeat
                     inc(i);
               until v[i].c>=piv;
               repeat
                     dec(j);
               until v[j].c<=piv;
               if i<j then
                  begin
                  aux:=v[i];
                  v[i]:=v[j];
                  v[j]:=aux;
                  end
               else
                   begin
                   ok:=false;
                   sort2:=j;
                   end;
         until ok=false;
         end;
procedure sort1(min,max:word);
          var p:word;
          begin
          if min<max then
             begin
             p:=sort2(min,max);
             sort1(min,p);
             sort1(p+1,max);
             end;
          end;
function str(nod:word):word;
         begin
         while nod<>tt[nod] do
               nod:=tt[nod];
         str:=nod;
         end;
begin
assign(f,'radiatie.in');
assign(g,'radiatie.out');
reset(f);rewrite(g);
readln(f,n,m,k);
for i:=1 to n do
    begin
    tt[i]:=i;
    rg[i]:=1;
    end;
for i:=1 to m do
    readln(f,v[i].x,v[i].y,v[i].c);
sort1(1,m);
i:=0;
repeat
      repeat
            inc(i);
            str1:=str(v[i].x);
            str2:=str(v[i].y);
      until str1<>str2;
      if rg[str1]>rg[str2] then
         begin
         tt[str2]:=str1;
         cost[str2]:=v[i].c;
         end
      else
          begin
          tt[str1]:=str2;
          cost[str1]:=v[i].c;
          end;
      if rg[str1]=rg[str2] then
         if tt[str1]=str2 then
            inc(rg[str2])
         else
             inc(rg[str1]);
      inc(b);
until b=n-1;
for i:=1 to k do
    begin
    readln(f,x,y);
    b:=x;
    while b<>tt[b] do
          begin
          s[b]:=1;
          b:=tt[b];
          end;
    b:=y;
    while (b<>tt[b]) and (s[b]<>1) do
          b:=tt[b];
    a:=x;
    max:=0;
    while a<>b do
          begin
          if cost[a]>max then
             max:=cost[a];
          a:=tt[a];
          end;
    a:=y;
    while a<>b do
          begin
          if cost[a]>max then
             max:=cost[a];
          a:=tt[a];
          end;
    writeln(g,max);
    fillchar(s,sizeof(s),0);
    end;
close(f);close(g);
end.