Cod sursa(job #918309)

Utilizator apopeid13Apopeid Alejandro apopeid13 Data 18 martie 2013 19:52:32
Problema Amenzi Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.47 kb
#include <iostream>
#include <fstream>
#include <cstring>
#include <algorithm>
#include <vector>
 
using namespace std;
 
#define maxN 155
#define maxT 3505
#define inf (1 << 30)
#define PB push_back
#define MKP make_pair
#define f first
#define s second
 
int DP[maxT][maxN], A[maxN][maxT];
vector < pair <int, int> > List[maxN];
 
 
int main()
{
    ifstream f ("amenzi.in");
    ofstream g ("amenzi.out");
 
    int N, M, K, P;
    f >> N >> M >> K >> P;
 
    while (M --)
    {
        int x, y, z;
        f >> x >> y >> z;
 
        List[x].PB ( MKP (y, z) );
        List[y].PB ( MKP (x, z) );
    }
 
    for (int i = 1; i <= K; ++ i)
    {
        int a, b, c;
        f >> a >> b >> c;
 
        A[a][b] += c;
    }
 
    for (int i = 0; i < maxT - 3; ++ i)
        for (int j = 1; j <= N; ++ j)
        {
            DP[i][j] = - inf;
            if (i == 0 && j == 1) DP[i][j] = 0;
 
            for (int t = 0; t < List[j].size(); ++ t)
            {
                int nod = List[j][t].f, tmp = List[j][t].s;
 
                if (i - tmp < 0) continue;
 
                DP[i][j] = max (DP[i][j], DP[i - tmp][nod]);
            }
 
            if (i - 1 >= 0) DP[i][j] = max (DP[i - 1][j], DP[i][j]);
 
            DP[i][j] += A[j][i];
        }
 
    while (P --)
    {
        int a, b;
        f >> a >> b;
 
        if (DP[b][a] < 0) g << "-1\n";
        else g << DP[b][a] << '\n';
    }
 
    return 0;
}