Pagini recente » Cod sursa (job #2263042) | Cod sursa (job #2977830) | Cod sursa (job #2981433) | Cod sursa (job #1030667) | Cod sursa (job #2236782)
#include <bits/stdc++.h>
using namespace std;
int n, m, k, p;
int d[155][3505];
int am[155][3505];
queue <pair <int, int> > q;
vector <pair <int, int> > v[155];
inline void wutm8(){
for(int i = 1; i <= n ; ++i)
for(int j = 0; j <= 3500 ; ++j) d[i][j] = -2000000000;
d[1][0] = am[1][0];
q.push({1, 0});
for(int i = 1; i <= 3500 ; ++i){
d[1][i] = d[1][i - 1];
if(d[1][i] < d[1][i - 1] + am[1][i]){
d[1][i] = d[1][i - 1] + am[1][i];
q.push({1, i});
}
}
while(!q.empty()){
int nod = q.front().first, timp = q.front().second;
q.pop();
for(auto it : v[nod]){
if(timp + it.second > 3500) continue ;
if(d[it.first][timp + it.second] < d[nod][timp] + am[it.first][timp + it.second]){
q.push({it.first, timp + it.second});
d[it.first][timp + it.second] = d[nod][timp] + am[it.first][timp + it.second];
for(int i = timp + it.second + 1; i <= 3500 ; ++i){
d[it.first][i] = max(d[it.first][i - 1], d[it.first][i]);
if(d[it.first][i] < d[it.first][i - 1] + am[it.first][i]){
d[it.first][i] = d[it.first][i - 1] + am[it.first][i];
q.push({it.first, i});
}
}
}
}
}
}
int main()
{
freopen("amenzi.in", "r", stdin);
freopen("amenzi.out", "w", stdout);
scanf("%d%d%d%d", &n, &m, &k, &p);
int x, y, c;
for(int i = 1; i <= m ; ++i){
scanf("%d%d%d", &x, &y, &c);
v[x].push_back({y, c});
v[y].push_back({x, c});
}
for(int i = 1; i <= k ; ++i){
scanf("%d%d%d", &x, &y, &c);
am[x][y] += c;
}
wutm8();
for(int i = 1; i <= n ; ++i)
for(int j = 1; j <= 3500 ; ++j)
d[i][j] = max(d[i][j], d[i][j - 1]);
for(int i = 1; i <= p ; ++i){
scanf("%d%d", &x, &y);
printf("%d\n", max(d[x][y], -1));
}
return 0;
}