Pagini recente » Cod sursa (job #677654) | Cod sursa (job #1570335) | Cod sursa (job #844148) | Cod sursa (job #232113) | Cod sursa (job #2777463)
#include <bits/stdc++.h>
using namespace std;
ofstream fout("amenzi.out");
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;
}
};
struct idk{
int a, b;
};
const int nmax = 151;
int n, m, k, p, infractiune[nmax][3501], dp[nmax][3501], a, b, c;
vector <idk> G[nmax];
int main(){
InParser fin("amenzi.in");
fin >> n >> m >> k >> p;
while (m--) {
fin >> a >> b >> c;
G[a].push_back({b, c});
G[b].push_back({a, c});
}
while (k--) {
fin >> a >> b >> c;
infractiune[a][b] += c;
}
memset(dp, -1, sizeof dp);
dp[1][0] = infractiune[1][0];
for (int timp = 1; timp <= 3500; ++timp){
for (int i = 1; i <= n; ++i){
dp[i][timp] = dp[i][timp - 1];
for (auto it : G[i]){
int nod = it.a, cost = it.b;
if (timp - cost >= 0){
if (dp[nod][timp - cost] > dp[i][timp]){
dp[i][timp] = dp[nod][timp - cost];
}
}
}
if (dp[i][timp] >= 0) dp[i][timp] += infractiune[i][timp];
}
}
while (p--) {
fin >> a >> b;
fout << dp[a][b] << "\n";
}
return 0;
}