Pagini recente » Cod sursa (job #1729654) | Cod sursa (job #352770) | Cod sursa (job #397115) | Cod sursa (job #417043) | Cod sursa (job #1829231)
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
const int nmax=150;
const int tmax=3500;
vector< pair<int,int> > v[nmax+5];
int best[tmax+5][nmax+5],a[tmax+5][nmax+5];
int n,m,i,k,p,ind,loc,timp,c,x,y,j;
int main()
{
ifstream f("amenzi.in");
ofstream g("amenzi.out");
f>>n>>m>>k>>p;
for(i=1;i<=m;i++)
{
f>>x>>y>>c;
v[x].push_back(make_pair(y,c));
v[y].push_back(make_pair(x,c));
}
for(i=1;i<=k;i++)
{
f>>x>>y>>c;
a[y][x]+=c;
}
for(i=0;i<=tmax;i++)
for(j=1;j<=n;j++)
best[i][j]=-1;
best[0][1]=0;
for(i=0;i<=tmax;i++)
for(j=1;j<=n;j++)
if(best[i][j]!=-1)
{
best[i][j]+=a[i][j];
if(best[i][j]>best[i+1][j])
best[i+1][j]=best[i][j];
for(ind=0;ind<v[j].size();ind++)
{
loc=v[j][ind].first;
timp=i+v[j][ind].second;
if(timp<=tmax&&best[i][j]>best[timp][loc])
best[timp][loc]=best[i][j];
}
}
for(i=1;i<=p;i++)
{
f>>loc>>timp;
g<<best[timp][loc]<<'\n';
}
return 0;
}