Cod sursa(job #2316476)

Utilizator IulianOleniucIulian Oleniuc IulianOleniuc Data 11 ianuarie 2019 19:46:07
Problema Amenzi Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.46 kb
#include <vector>
#include <fstream>

#define NMAX 160
#define TMAX 3510

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

struct Edge {
    int64_t node, cost;
    Edge(int64_t node, int64_t cost) {
        this->node = node;
        this->cost = cost;
    }
};

int64_t n, m;
std::vector<Edge> ad[NMAX];

int64_t k;
int64_t ev[NMAX][TMAX];

int64_t p;
int64_t dp[NMAX][TMAX];

inline int64_t max(int64_t x, int64_t y) {
    return x > y ? x : y;
}

int main() {
    int64_t x, y, z;
    fin >> n >> m >> k >> p;

    for (int64_t i = 0; i < m; i++) {
        fin >> x >> y >> z;
        ad[x].push_back(Edge(y, z));
        ad[y].push_back(Edge(x, z));
    }

    for (int64_t i = 0; i < k; i++) {
        fin >> x >> y >> z;
        ev[x][y] = z;
    }

    for (int64_t i = 2; i <= n; i++)
        dp[i][0] = -1;

    for (int64_t j = 1; j < TMAX; j++)
        for (int64_t i = 1; i <= n; i++) {
            dp[i][j] = dp[i][j - 1];
            for (Edge edge : ad[i])
                if (i != edge.node && j >= edge.cost)
                    dp[i][j] = max(dp[i][j], dp[edge.node][j - edge.cost]);

            if (dp[i][j] != -1)
                dp[i][j] += ev[i][j];
        }

    for (int64_t i = 0; i < p; i++) {
        fin >> x >> y;
        if (dp[x][y] == -1)
            fout << "-1\n";
        else
            fout << dp[x][y] - ev[x][y] << '\n';
    }

    fout.close();
    return 0;
}