Cod sursa(job #2777462)

Utilizator PatrickCplusplusPatrick Kristian Ondreovici PatrickCplusplus Data 23 septembrie 2021 12:42:48
Problema Amenzi Scor 90
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.21 kb
#include <bits/stdc++.h>

using namespace std;
ifstream fin("amenzi.in");
ofstream fout("amenzi.out");

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(){
    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;
}