Pagini recente » Cod sursa (job #2097228) | Cod sursa (job #3129741) | Cod sursa (job #1672871) | Cod sursa (job #2498955) | Cod sursa (job #3267482)
#include <bits/stdc++.h>
using namespace std;
ifstream f ("amenzi.in");
ofstream g ("amenzi.out");
const int KMAX = 12e3;
const int PMAX = 8e3;
const int NMAX = 150;
const int MMAX = 15e2;
const int TMAX = 35e2;
int n, m, p, k;
vector<pair<int, int>> adj[NMAX+1];
int amenda[NMAX+1][TMAX+10], dp[NMAX+1][TMAX+10];
bool vis[NMAX+1][TMAX+10];
int main()
{
f >> n >> m >> k >> p;
for(int i=1; i<=m; i++){
int a, b, c;
f >> a >> b >> c;
//cout << a << ' ' << b << ' ' << c << endl;
adj[a].push_back({b, c});
adj[b].push_back({a, c});
}
for(int i=1; i<=k; i++){
int a, b, c;
f >> a >> b >> c;
amenda[a][b] += c;
}
vis[1][0] = true;
for(int t=1; t<=TMAX+5; t++){
for(int i=1; i<=n; i++){
for(auto [next, timp] : adj[i]){
if(t - timp >= 0 and vis[i][t - timp]){
vis[next][t] = true;
dp[next][t] = max(dp[next][t], dp[i][t - timp] + amenda[next][t]);
dp[next][t] = max(dp[next][t], dp[next][t - 1] + amenda[next][t]);
}
}
}
}
//cout << dp[0][0] << endl;
for(int i=1; i<=p; i++){
int a, b;
f >> a >> b;
if(!vis[a][b])
g << -1 << "\n";
else
g << dp[a][b] << "\n";
}
return 0;
}