Pagini recente » Cod sursa (job #95128) | Cod sursa (job #1457670) | Statistici Adelina Petre (adelinapetre) | Cod sursa (job #68386) | Cod sursa (job #3283230)
#include <bits/stdc++.h>
using namespace std;
ifstream f("amenzi.in");
ofstream g("amenzi.out");
struct drumuri{
int node, timp;
};
int n,m,k,p,x,y,z,drum[3520][152],amenda[3500][152];
vector <drumuri> edges[200];
queue <int> q[3520];
void dial()
{
for(int i=0; i<=3500; i++)
for(int j=1; j<=n; j++)
drum[i][j]=-1;
drum[0][1]=0, q[0].push(1);
for(int i=0; i<=3500; i++)
while(!q[i].empty())
{
int x=q[i].front(); q[i].pop();
for(auto y:edges[x])
if(drum[i+y.timp][y.node]<drum[i][x]+amenda[i][x])
drum[i+y.timp][y.node]=drum[i][x]+amenda[i][x], q[i+y.timp].push(y.node);
}
}
int main()
{
f>>n>>m>>k>>p;
for(int i=1; i<=m; i++)
f>>x>>y>>z, edges[x].push_back({y,z}), edges[y].push_back({x,z});
for(int i=1; i<=n; i++)
edges[i].push_back({i,1});
for(int i=1; i<=k; i++)
f>>x>>y>>z, amenda[y][x]=z;
dial();
for(int i=1; i<=p; i++)
f>>x>>y, g<<drum[y][x]<<'\n';
return 0;
}