Pagini recente » Cod sursa (job #2653675) | Cod sursa (job #310821) | Cod sursa (job #508272) | Cod sursa (job #565394) | Cod sursa (job #2343474)
#include <bits/stdc++.h>
class InParser {
private:
FILE *fin;
char *buff;
int sp;
char read_ch() {
++sp;
if (sp == 4096) {
sp = 0;
fread(buff, 1, 4096, fin);
}
return buff[sp];
}
public:
InParser(const char* nume) {
fin = fopen(nume, "r");
buff = new char[4096]();
sp = 4095;
}
InParser& operator >> (int &n) {
char c;
while (!isdigit(c = read_ch()) && c != '-');
int sgn = 1;
if (c == '-') {
n = 0;
sgn = -1;
} else {
n = c - '0';
}
while (isdigit(c = read_ch())) {
n = 10 * n + c - '0';
}
n *= sgn;
return *this;
}
InParser& operator >> (long long &n) {
char c;
n = 0;
while (!isdigit(c = read_ch()) && c != '-');
long long sgn = 1;
if (c == '-') {
n = 0;
sgn = -1;
} else {
n = c - '0';
}
while (isdigit(c = read_ch())) {
n = 10 * n + c - '0';
}
n *= sgn;
return *this;
}
};
int max(int x, int y) {
if (x > y) return x;
return y;
}
#define int_pair std::pair <int, int>
#define MAXN 155
#define MAXT 3505
int N, M, K, P;
int DP[2*MAXT][MAXN], Pay[2*MAXT][MAXN];
std::vector <int_pair> ADC[MAXN];
inline void AddEdge(int X, int Y, int W) {
ADC[X].push_back({Y, W});
ADC[Y].push_back({X, W});
}
void Dynamic() {
for (int j=0, i; j<MAXN; ++j)
for (i=0; i<MAXT; ++i)
DP[i][j] = -1;
DP[0][1] = Pay[0][1];
for (int i=0, j; i+1<MAXT; ++i)
for (j=1; j<=N; ++j) if (DP[i][j] > -1) {
DP[i+1][j] = std::max(DP[i+1][j], DP[i][j] + Pay[i+1][j]);
for (int_pair Edge:ADC[j])
DP[i + Edge.second][Edge.first] = std::max(DP[i][j] + Pay[i + Edge.second][Edge.first], DP[i + Edge.second][Edge.first]);
}
}
InParser In ("amenzi.in");
std::ofstream Out("amenzi.out");
void Citire() {
In >> N >> M >> K >> P;
for (int i=0, x, y, w; i<M; ++i)
In >> x >> y >> w, AddEdge(x, y, w);
for (int i=0, a, t, s; i<K; ++i)
In >> a >> t >> s, Pay[t][a] += s;
}
void Rezolvare() {
Dynamic();
for (int i=0, x, y; i<P; ++i)
In >> x >> y, Out << DP[y][x] << '\n';
}
int main()
{
Citire();
Rezolvare();
return 0;
}