Cod sursa(job #1169435)

Utilizator andreiagAndrei Galusca andreiag Data 11 aprilie 2014 12:23:58
Problema Amenzi Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.25 kb
#include <fstream>
#include <string.h>
#include <vector>

using namespace std;
const int Nmax = 155;
const int Tmax = 3505;
const int Kmax = 12005;
const int INF = 0x3f3f3f3f;

int dp[Nmax][Tmax];
int fine[Nmax][Tmax];

vector<pair<int, int>> G[Nmax];

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

int main()
{
    ifstream f ("amenzi.in");
    ofstream g ("amenzi.out");

    int N, M, K, P, a, b, c;
    f >> N >> M >> K >> P;

    memset(dp, -1, sizeof(dp));
    memset(fine, 0, sizeof(fine));
    dp[1][0] = 0;

    while(M--)
    {
        f >> a >> b >> c;
        G[a].push_back(make_pair(b, c));
        G[b].push_back(make_pair(a, c));
    }

    while(K--)
    {
        f >> a >> b >> c;
        fine[a][b] += c;
    }

    for(int t = 0; t < Tmax; t++)
    for(int a = 1; a <= N; a++)
    {
        if (t > 0 && dp[a][t-1] != -1)
            dp[a][t] = dp[a][t-1];

        for (auto x: G[a])
        {
            if (t >= x.second && dp[x.first][t-x.second] != -1)
            {
                dp[a][t] = max(dp[a][t], dp[x.first][t-x.second]);
            }
        }

        if (dp[a][t] != -1)
            dp[a][t] += fine[a][t];
    }

    while(P--)
    {
        f >> a >> b;
        g << dp[a][b] << '\n';
    }

    return 0;
}