Pagini recente » Cod sursa (job #1528369) | Cod sursa (job #3259545) | Cod sursa (job #2260094) | Cod sursa (job #1359285) | Cod sursa (job #1649195)
#include <fstream>
#include <vector>
using namespace std;
ifstream fin("amenzi.in");
ofstream fout("amenzi.out");
const int MaxT = 3501, MaxN = 151;
int N, M, K, P;
vector<vector<pair<int, int>>> G;
int a[MaxN][MaxT], D[MaxN][MaxT];
void Read();
int main() {
Read();
for (int i = 0; i < MaxN; i++)
for (int j = 0; j < MaxT; j++)
D[i][j] = -1;
D[1][0] = 0;
for (int j = 0; j < MaxT; j++)
for (int i = 1; i <= N; i++) {
if (j != 0) D[i][j] = D[i][j - 1];
for (const auto e : G[i])
if (j >= e.second)
D[i][j] = max(D[i][j], D[e.first][j - e.second]);
if (D[i][j] != -1) D[i][j] += a[i][j];
}
for (int i = 0, x, y; i < P; i++) {
fin >> x >> y;
fout << D[x][y] << '\n';
}
fin.close();
fout.close();
return 0;
}
void Read() {
fin >> N >> M >> K >> P;
G = vector<vector<pair<int, int>>>(N + 1);
for (int i = 0, x, y, w; i < M; i++) {
fin >> x >> y >> w;
G[x].push_back({y, w});
G[y].push_back({x, w});
}
for (int i = 0, x, y ,w; i < K; i++) {
fin >> x >> y >> w;
a[x][y] += w;
}
}