Pagini recente » Cod sursa (job #1761306) | Cod sursa (job #401396) | Cod sursa (job #855182) | Cod sursa (job #2704980) | Cod sursa (job #2052317)
#include <iostream>
#include <fstream>
#include <vector>
#include <cstring>
using namespace std;
ifstream fin("amenzi.in");
ofstream fout("amenzi.out");
const int TMax = 3505;
const int NMax = 155;
vector < pair < int, int > > G[NMax];
int dp[TMax][NMax];//best in timpul t pana in intersectia j
int amenda[TMax][NMax];
int N, M, K, P;
void Read()
{
fin >> N >> M >> K >> P;
for(int i=1; i<=M; ++i)
{
int x, y, c;
fin >> x >> y >> c;
G[x].push_back({y,c});
G[y].push_back({x,c});
}
for(int i=1; i<=K; ++i)
{
int nod, t, val;
fin >> nod >> t >> val;
amenda[t][nod] += val;
}
}
void Solve()
{
memset(dp,-1,sizeof(dp));
dp[0][1] = 0;
for(int t=0; t < TMax; ++t)
{
for(int i=1; i <= N; ++i)
{
if(t==0 && i==1)
continue;
if(t>=1 && dp[t-1][i] != -1)
dp[t][i] = dp[t-1][i] + amenda[t][i];
vector < pair < int, int > > ::iterator it;
for(it=G[i].begin(); it!=G[i].end(); ++it)
{
int itr = it->first;
int cst = it->second;
if(t >= cst && dp[t - cst][itr] !=-1)
dp[t][i] = max(dp[t][i], dp[t-cst][itr] + amenda[t][i]);
}
}
}
}
void Print()
{
for(int i=1; i<=P; ++i)
{
int nod, timp;
fin >> nod >> timp;
fout << dp[timp][nod] << "\n";
}
}
int main()
{
Read();
Solve();
Print();
return 0;
}