Cod sursa(job #980630)

Utilizator CosminRusuCosmin Rusu CosminRusu Data 5 august 2013 11:48:49
Problema Amenzi Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.6 kb
/**
 *   DINAMICA "INAPOI"
 *
 */

#include <fstream>
#include <vector>
#include <bitset>
#include <queue>
#include <algorithm>
#include <utility>
#include <string.h>

using namespace std;

ifstream cin("amenzi.in");
ofstream cout("amenzi.out");

const int MAXN = 155;
const int MAXT = 3505;
const int oo = (1<<31)-1;

typedef vector< pair<int, int> > Graph[MAXN];
typedef vector< pair<int, int> > :: iterator It;

Graph G;
int N, M, K, P;
int DP[MAXT][MAXN];   /// suma maxina de amenzi aflandu-ma la momentul i in orasul j;
int Cost[MAXT][MAXN]; /// amenda la timpul i in orasul j

int main()
{
    cin >> N >> M >> K >> P;
    for(int i = 1 ; i <= M ; ++ i ) {
        int x, y, z;
        cin >> x >> y >> z;
        G[x].push_back(make_pair(y, z));
        G[y].push_back(make_pair(x, z));
    }
    for(int i = 1 ; i <= K ; ++ i) {
        int x, y, z;
        cin >> x >> y >> z;
        Cost[y][x] += z;
    }
    memset(DP, -1, sizeof(DP));
    DP[0][1] = Cost[0][1];
    for(int i = 1 ; i < MAXT ; ++i)
        for(int j = 1 ; j <= N ; ++ j) {
            DP[i][j] = max(DP[i][j], DP[i-1][j]); /// din momentul i in momentul i+1 stau
            for(It it = G[j].begin(), fin = G[j].end() ; it != fin ; ++ it)
                if(i - (it->second) >= 0)
                    DP[i][j] = max(DP[i][j], DP[i-it->second][it->first]);
            if(DP[i][j] != -1)      DP[i][j] += Cost[i][j];
        }
    for(int i = 1 ; i <= P ; ++ i) {
        int x, y;
        cin >> x >> y;
        cout << DP[y][x] << '\n';
    }
    cin.close();
    cout.close();
    return 0;
}