Cod sursa(job #2824071)

Utilizator toma_ariciuAriciu Toma toma_ariciu Data 30 decembrie 2021 20:56:31
Problema Amenzi Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.4 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <cstring>

using namespace std;

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

struct node {
    int nod, cost;
};

const int maxN = 150, maxT = 3500;
int nr_noduri, nr_muchii, nr_crime, nr_momente;
int amenda[maxN + 5][maxT + 5], dp[maxN + 5][maxT + 5];
vector <node> G[maxN + 5];

int main()
{
    fin >> nr_noduri >> nr_muchii >> nr_crime >> nr_momente;
    for(int i = 1; i <= nr_muchii; i++)
    {
        node a, b;
        fin >> a.nod >> b.nod >> a.cost;
        b.cost = a.cost;
        G[a.nod].push_back(b);
        G[b.nod].push_back(a);
    }
    for(int i = 1; i <= nr_crime; i++)
    {
        int loc, timp, val;
        fin >> loc >> timp >> val;
        amenda[loc][timp] += val;
    }
    memset(dp, -1, sizeof dp);
    dp[1][0] = 0;
    for(int j = 1; j <= maxT; j++)
    {
        for(int i = 1; i <= nr_noduri; i++)
        {
            dp[i][j] = dp[i][j - 1];
            for(node next : G[i])
                if(next.cost <= j)
                    dp[i][j] = max(dp[i][j], dp[next.nod][j - next.cost]);
            if(dp[i][j] != -1)
                dp[i][j] += amenda[i][j];
        }
    }
    for(int i = 1; i <= nr_momente; i++)
    {
        int loc, timp;
        fin >> loc >> timp;
        fout << dp[loc][timp] << '\n';
    }
    return 0;
}