Pagini recente » Rating Veljko Radic (painkiller) | Rating Horchidan Sonia-Florina (SoniaFlorina) | Rating ACEUCVRO (UCV_Urdiniceanu_Sperila_Mitrica) | Cod sursa (job #776935) | Cod sursa (job #11659)
Cod sursa(job #11659)
#include<cstdio>
#define maxtime 3501
#define maxN 151
#define maxM 1501
#define maxP 8001
long N, M, K, P, Tmax, A[maxtime][maxN], nd[maxP], time[maxP];
long B[maxtime][maxN];
struct nod
{
long nd;
long c;
nod *next;
} *L[maxN], *v;
void add(nod *&L, long x, long cost)
{
nod *camp = new nod;
camp -> nd = x;
camp -> c = cost;
camp -> next = L;
L = camp;
}
int main()
{
freopen("amenzi.in", "r", stdin);
freopen("amenzi.out", "w", stdout);
scanf("%ld %ld %ld %ld", &N, &M, &K, &P);
long i, x, y, c, t;
for(i=1; i<=M; ++i)
{
scanf("%ld %ld %ld", &x, &y, &c);
add(L[x], y, c);
add(L[y], x, c);
}
for(i=1; i<=K; ++i)
{
scanf("%ld %ld %ld", &x, &t, &c);
A[t][x] = c;
}
for(i=1; i<=P; ++i)
{
scanf("%ld %ld", nd+i, time+i);
Tmax = time[i] > Tmax ? time[i] : Tmax;
}
for(x=2; x<=N; ++x)
B[0][x] = -1;
for(t=1; t<=Tmax; ++t)
{
for(x=1; x<=N; ++x)
{
v = L[x];
B[t][x] = B[t-1][x];
while(v)
{
y = v -> nd;
c = v -> c;
if(c <= t && B[t-c][y] > B[t][x])
B[t][x] = B[t-c][y];
v = v -> next;
}
if(B[t][x] != -1)
B[t][x] += A[t][x];
}
}
for(i=1; i<=P; ++i)
printf("%ld\n", B[time[i]][nd[i]]);
fclose(stdin); fclose(stdout);
return 0;
}