Cod sursa(job #2777386)

Utilizator PatrickCplusplusPatrick Kristian Ondreovici PatrickCplusplus Data 23 septembrie 2021 10:07:26
Problema Amenzi Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.39 kb
#include <bits/stdc++.h>

using namespace std;

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

const int nmax = 155;
int n, m, k, p, infractiune[nmax][3505], sotie[nmax][3505], dp[nmax][3505];
vector <pair <int, int> > G[nmax];

int main(){
    fin >> n >> m >> k >> p;
    for (int i = 1; i <= m; ++i){
        int a, b, c;
        fin >> a >> b >> c;
        G[a].push_back({b, c});
        G[b].push_back({a, c});
    }
    for (int i = 1; i <= k; ++i){
        int a, b, c;
        fin >> a >> b >> c;
        infractiune[a][b] += c;
    }
    for (int j = 0; j <= 3500; ++j){
        for (int i = 1; i <= n; ++i){
            dp[i][j] = -1e9;
        }
    }
    dp[1][0] = infractiune[1][0];
    for (int timp = 1; timp <= 3500; ++timp){
        for (int i = 1; i <= n; ++i){
            if (dp[i][timp - 1] >= 0)
                dp[i][timp] = dp[i][timp - 1];
            for (auto it : G[i]){
                if (timp - it.second >= 0)
                    if (dp[it.first][timp - it.second] >= 0)
                        dp[i][timp] = max(dp[i][timp], infractiune[i][timp] + dp[it.first][timp - it.second]);
            }
        }
    }
    for (int i = 1; i <= p; ++i){
        int a, b;
        fin >> a >> b;
        if (dp[a][b] >= 0)
            fout << dp[a][b] << "\n";
        else{
            fout << 0 << "\n";
        }
    }
    return 0;
}