Cod sursa(job #22709)

Utilizator gurneySachelarie Bogdan gurney Data 27 februarie 2007 09:47:13
Problema Amenzi Scor 0
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.57 kb
program amenzi;
  const
    fin='amenzi.in';
    fout='amenzi.out';
    kmax=12000;
    pmax=8000;
    nmax=150;
    amax=3500;
type
  edge=^muchie;
  muchie=record
    x:integer;cost:longint;urm:edge;
  end;
var
  q:edge;
  e:array[1..nmax] of edge;
  c:array[0..amax,1..nmax] of longint;
  a:array[0..amax,1..nmax] of longint;
  i,j,k,n,t,m,p,x,y,cost:longint;

procedure insert(x,y,cost:longint);
  var
    p:edge;
  begin
    new(p);
    p^.x:=y;
    p^.cost:=cost;
    p^.urm:=e[x];
    e[x]:=p;
  end;

begin
  assign(input,fin);assign(output,fout);
    rewrite(output);
    reset(input);
    readln(n,m,k,p);
    for i:=1 to n do
      begin
        new(e[i]);
        e[i]^.urm:=nil;
      end;
    for i:=1 to m do
      begin
        readln(x,y,cost);
        insert(x,y,cost);
        insert(y,x,cost);
      end;
    for i:=1 to k do
      begin
        readln(x,y,cost);
        a[y,x]:=cost;
      end;
    for t:=0 to amax do
      for i:=1 to n do
        c[t,i]:=-1;
    c[0,1]:=a[0,1];
    for t:=1 to amax do
      for i:=1 to n do
        begin
          q:=e[i];
          c[t,i]:=c[t-1,i];
          while q<>nil do
            begin
              if t-q^.cost>=0 then
                if c[t-q^.cost,q^.x]>c[t,i] then
                  c[t,i]:=c[t-q^.cost,q^.x];
              q:=q^.urm;
            end;
          if c[t,i]<>-1 then
            inc(c[t,i],a[t,i]);
        end;
    for i:=1 to p do
      begin
        readln(x,y);
        writeln(c[y,x]);
      end;
  close(input);
  close(output);
end.