Cod sursa(job #1600039)

Utilizator blackoddAxinie Razvan blackodd Data 14 februarie 2016 17:19:54
Problema Amenzi Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.16 kb
#include <fstream>
#include <vector>
#include <cstring>
using namespace std;

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

#define MaxN 151
#define MaxT 3501
#define pb push_back

using PII = pair<int,int>;

int n, m, k, p;

vector<PII> G[MaxN];
int D[MaxN][MaxT], a[MaxN][MaxT];

int main() {

    fin >> n >> m >> k >> p;

    int x, y, z;

    for ( int i = 1; i <= m; ++i ) {
        fin >> x >> y >> z;
        G[x].pb({y,z});
        G[y].pb({x,z});
    }

    for ( int i = 1; i <= k; ++i ) {
        fin >> x >> y >> z;
        a[x][y] += z;
    }

    memset(D, -1, sizeof D);

    D[1][0] = 0;

    for ( int j = 0; j < MaxT; ++j )
        for ( int i = 1; i <= n; ++i ) {
            if ( j > 0 )
                D[i][j] = D[i][j-1];
            for ( const auto p : G[i] )
                if ( j >= p.second )
                    D[i][j] = max(D[i][j], D[p.first][j - p.second]);
            if ( D[i][j] != -1 )
                D[i][j] += a[i][j];
    }

    for ( int i = 1; i <= p; ++i) {
        fin >> x >> y;
        fout << D[x][y] << '\n';
    }

    fin.close();
    fout.close();
    return 0;
}