Pagini recente » Cod sursa (job #2792683) | Cod sursa (job #1953113) | Cod sursa (job #226699) | Cod sursa (job #280299) | Cod sursa (job #2620591)
#include <bits/stdc++.h>
#define DAU ios::sync_with_stdio(false); fin.tie(0); fout.tie(0);
#define PLEC fin.close(); fout.close(); return 0;
using namespace std;
const string problem("amenzi");
ifstream fin(problem + ".in");
ofstream fout(problem + ".out");
using PII = pair<int, int>;
using VP = vector<PII>;
using VVP = vector<VP>;
using VI = vector<int>;
using VVI = vector<VI>;
const int TMAX(3505);
VVI dp, a;
VVP g;
int n, m, k, p, x, y, w;
int main() {
DAU
fin >> n >> m >> k >> p;
g = VVP(n + 1);
for (int i = 1; i <= m; ++i) {
fin >> x >> y >> w;
g[x].emplace_back(y, w);
g[y].emplace_back(x, w);
}
a = VVI(TMAX, VI(n + 1));
for (int i = 1; i <= k; ++i) {
fin >> x >> y >> w;
a[y][x] += w;
}
dp = VVI(TMAX, VI(n + 1, -1e9));
dp[0][1] = a[0][1];
for (int t = 0; t < TMAX; ++t)
for (int x = 1; x <= n; ++x) {
if (t) dp[t][x] = max(dp[t][x], dp[t-1][x] + a[t][x]);
if (!t || (t && dp[t][x] > dp[t-1][x]))
for (const PII& P : g[x]) {
tie(y, w) = P;
if (t + w < TMAX)
dp[t+w][y] = max(dp[t+w][y], dp[t][x] + a[t+w][y]);
}
}
for (int i = 1; i <= p; ++i) {
fin >> x >> y;
fout << max(dp[y][x], -1) << '\n';
}
PLEC
}