Cod sursa(job #2604417)

Utilizator CraniXortDumitrescul Eduard CraniXort Data 22 aprilie 2020 17:13:12
Problema Amenzi Scor 30
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.03 kb
#include <bits/stdc++.h>

//std::ifstream fin ("input.txt");
//std::ofstream fout ("output.txt");

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


int main()
{
    int V, E, A, Q, Tmax = 3500;
    int src, dest, cost, i ,j;
    fin >> V >> E >> A >> Q;

    std::vector < std::pair < int, int > > edge[V+1];
    for (i=0; i<E; i++){
        fin >> src >> dest >> cost;
        edge[src].push_back ({dest, cost});
        edge[dest].push_back ({src, cost});
    }
    int dp[Tmax+1][V+1], max[V+1];

    for (i=0; i<=Tmax; i++)
        for (j=0; j<=V; j++)
        dp[i][j] = -1;

    for (i=0; i<=V; i++)
            max[i] = -1;
    dp[0][1] = 0;
    max[1] = 0;

    int x[Tmax+1][V+1];
    memset (x, 0, sizeof x);
    for (i=1; i<=A; i++){
        int node, pay, time;
        fin >> node >> time >> pay;
        x[time][node] = pay;
    }

    /*
    for (int time=0; time<=20; time++, fout << '\n')
        for (i=1; i<=V; i++)
        fout << x[time][i] << ' ';
        */

    dp[0][1] = x[0][1];

    for (int time=1; time<=Tmax; time++)
        for (int node=1; node<=V; node++){
            dp[time][node] = max[node];
            for (i=0; i<edge[node].size(); i++){
                int prec = edge[node][i].first;
                int t = edge[node][i].second;
                if (t > time)
                    continue;
                t = time - t;
                dp[time][node] = std::max (dp[time][node], dp[t][prec]);
            }
            if (dp[time][node] == -1)
                continue;
            dp[time][node] += x[time][node];
            max[node] = std::max (max[node], dp[time][node]);
        }

    /*
    for (int time=0; time<=20; time++){
        fout << time << "\n";
        for (i=1; i<=V; i++)
            fout << dp[time][i] << ' ';
        fout << '\n' << "**********" << '\n';
    }
    */

    while (Q--){
        int time, node;
        fin >> node >> time;
        fout << dp[time][node] << '\n';
    }


    return 0;
}