Pagini recente » Cod sursa (job #2695850) | Cod sursa (job #1218950) | Cod sursa (job #3215075) | Cod sursa (job #1606683) | Cod sursa (job #2777456)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("amenzi.in");
ofstream fout("amenzi.out");
struct idk{
int a, b;
};
const int nmax = 155;
int n, m, k, p, infractiune[nmax][3505], dp[nmax][3505];
vector <idk> G[nmax];
int main(){
fin >> n >> m >> k >> p;
while (m--) {
int a, b, c;
fin >> a >> b >> c;
G[a].emplace_back({b, c});
G[b].emplace_back({a, c});
}
while (k--) {
int a, b, c;
fin >> a >> b >> c;
infractiune[a][b] += c;
}
for (int j = 0; j <= 3500; ++j){
for (int i = 1; i <= n; ++i){
dp[i][j] = -1e9;
}
}
dp[1][0] = infractiune[1][0];
for (int timp = 1; timp <= 3500; ++timp){
for (int i = 1; i <= n; ++i){
dp[i][timp] = dp[i][timp - 1];
int s = G[i].size();
for (int t = 0; t < s; ++t){
int nod = G[i][t].a, cost = G[i][t].b;
if (timp - cost >= 0)
dp[i][timp] = max(dp[i][timp], dp[nod][timp - cost]);
}
if (dp[i][timp] >= 0) dp[i][timp] += infractiune[i][timp];
}
}
while (p--) {
int a, b;
fin >> a >> b;
fout << dp[a][b] << "\n";
}
return 0;
}