Pagini recente » Cod sursa (job #1478636) | Cod sursa (job #1834916) | Cod sursa (job #83252) | Cod sursa (job #154537) | Cod sursa (job #2777121)
#include <bits/stdc++.h>
#define PII pair <int, int>
#define DIM 155
#define TMAX 3505
using namespace std;
ifstream f("amenzi.in");
ofstream g("amenzi.out");
int n, m, k, p;
int dp[TMAX][DIM], amenda[TMAX][DIM];
vector <PII> edges[DIM];
int main()
{
f >> n >> m >> k >> p;
for(int i = 1; i <= m; i++)
{
int x, y, cost;
f >> x >> y >> cost;
edges[x].push_back(make_pair(y, cost));
edges[y].push_back(make_pair(x, cost));
}
for(int i = 1; i <= k; i++)
{
int x, cost, t;
f >> x >> t >> cost;
amenda[t][x] = cost;
}
for(int i = 0; i <= 3500; i++)
for(int j = 1; j <= n; j++)
dp[i][j] = -1;
dp[0][1] = 0;
for(int t = 1; t <= 3500; t++)
for(int i = 1; i <= n; i++)
{
dp[t][i] = dp[t - 1][i];
for(auto child : edges[i])
if(t >= child.second)
dp[t][i] = max(dp[t][i], dp[t - child.second][child.first]);
if(dp[t][i] != -1)
dp[t][i] += amenda[t][i];
}
for(int i = 1; i <= p; i++)
{
int x, y;
f >> x >> y;
g << dp[y][x] << "\n";
}
return 0;
}