Pagini recente » Cod sursa (job #410467) | Cod sursa (job #2670356) | Cod sursa (job #3292978) | Cod sursa (job #3129467) | Cod sursa (job #9539)
Cod sursa(job #9539)
program amenzi;
const
fin='amenzi.in';
fout='amenzi.out';
inf=maxint;
nmax=150;
type
coada=^elem;
elem=record
nod:integer;t:longint;
sum:longint;
last:integer;
urm:coada;
end;
amenda=record
n,t,sum:longint;
end;
var
d:array[1..nmax,1..nmax] of longint;
m,n,p,i,j,k,x,y,c,ind:longint;
cap,aux,q,ult:coada;
am:array[1..12000] of amenda;
max:longint;
meet:array[1..8000] of record
n,t:longint;
end;
begin
assign(input,fin);
reset(input);
readln(n,m,k,p);
for i:=1 to n do
for j:=1 to n do
d[i,j]:=inf;
for i:=1 to n do
d[i,i]:=0;
for i:=1 to m do
begin
readln(x,y,c);
d[x,y]:=c;
d[y,x]:=c;
end;
for i:=1 to k do
begin
readln(am[i].n,am[i].t,am[i].sum);
end;
for i:=1 to p do
readln(meet[i].n,meet[i].t);
close(input);
assign(output,fout);
rewrite(output);
for x:=1 to n do
for i:=1 to n do
for j:=1 to n do
if d[i,x]+d[x,j]<d[i,j] then
d[i,j]:=d[i,x]+d[x,j];
for ind:=1 to p do
if d[1,meet[ind].n]<=meet[ind].t then
begin
new(cap);
cap^.nod:=1;cap^.t:=0;cap^.sum:=0;cap^.urm:=nil;
max:=0;
ult:=cap;
cap^.last:=0;
while cap<>nil do
begin
for i:=1 to k do
begin
if am[i].t+d[am[i].n,meet[ind].n]<=meet[ind].t then
if cap^.t+d[cap^.nod,am[i].n]<=am[i].t then
if cap^.last<>i then
begin
new(q);
ult^.urm:=q;q^.urm:=nil;
q^.nod:=am[i].n;
q^.t:=am[i].t;
q^.sum:=cap^.sum+am[i].sum;
q^.last:=i;
ult:=q;
if q^.sum>max then
max:=q^.sum;
end;
end;
q:=cap;
cap:=cap^.urm;
dispose(q);
end;
writeln(max);
end
else
writeln('-1');
close(output);
end.