Pagini recente » Cod sursa (job #1431649) | Cod sursa (job #947573) | Cod sursa (job #885898) | Cod sursa (job #885800) | Cod sursa (job #263969)
Cod sursa(job #263969)
#include <algorithm>
#include <stdio.h>
#include <vector>
#define pb push_back
#define timpMax 3510
#define MAX 156
using namespace std;
class drum
{
public:
int dest, cost;
drum(int dest, int cost)
{
this->dest = dest, this->cost = cost;
}
};
int verts, edges, incid, meets;
vector <drum> listDrum[MAX];
int matBonus[timpMax][MAX], matCastig[timpMax][MAX];
int main()
{
freopen("amenzi.in", "r", stdin);
freopen("amenzi.out", "w", stdout);
scanf("%d %d %d %d", &verts, &edges, &incid, &meets);
for (; edges; edges--)
{
int t, f, c;
scanf("%d %d %d", &t, &f, &c);
listDrum[t].pb(drum(f, c));
listDrum[f].pb(drum(t, c));
}
for (; incid; incid--)
{
int nod, time, bonus;
scanf("%d %d %d", &nod, &time, &bonus);
matBonus[time][nod] += bonus;
}
// init
for (int timp = 0; timp < timpMax; timp++)
for (int nod = 1; nod <= verts; nod++)
matCastig[timp][nod] = -1;
matCastig[0][1] = 0;
for (int timp = 0; timp < timpMax; timp++)
for (int nod = 1; nod <= verts; nod++)
{
if (matCastig[timp][nod] != -1)
matCastig[timp][nod] += matBonus[timp][nod];
for (int i = 0; i < listDrum[nod].size(); i++)
if (timp + listDrum[nod][i].cost < timpMax)
matCastig[timp + listDrum[nod][i].cost][listDrum[nod][i].dest] =
max(matCastig[timp + listDrum[nod][i].cost][listDrum[nod][i].dest], matCastig[timp][nod]);
matCastig[timp + 1][nod] = max(matCastig[timp + 1][nod], matCastig[timp][nod]);
}
for (; meets; meets--)
{
int Qnod, Qtime;
scanf("%d %d", &Qnod, &Qtime);
printf("%d\n", matCastig[Qtime][Qnod]);
}
fclose(stdin);
fclose(stdout);
return 0;
}