Cod sursa(job #1329370)

Utilizator gabrielinelusGabriel-Robert Inelus gabrielinelus Data 29 ianuarie 2015 14:18:31
Problema Amenzi Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.69 kb
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cstring>


#define Nmax 155
#define Tmax 3505
#define INF 0x3f3f3f3f
#define next second
#define timp first

using namespace std;

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

int DP[Nmax][Tmax]; /// DP[i][j] = costul maxim pe care il poate obtine daca
                    /// sotia il asteapta in nodul i la momentul j
int Amenda[Nmax][Tmax],N,M,K,P;

void Read()
{
    scanf("%d%d%d%d",&N,&M,&K,&P);
    int a,b,c;
    for(int i = 1; i <= M; ++i){
        scanf("%d%d%d",&a,&b,&c);
        G[a].push_back(make_pair(c,b));
        G[b].push_back(make_pair(c,a));
    }
    for(int i = 1; i <= K; ++i){
        scanf("%d%d%d",&a,&b,&c);
        Amenda[a][b] += c; /// poate sunt mai multe amenzi simultane
    }
}

void Solve()
{
    memset(DP,-INF,sizeof(DP));
    DP[1][0] = Amenda[1][0]; /// initializam cu amenda daca sta pe loc :D

    for(int T = 1; T <= 3500; ++ T)
        for(int nod = 1; nod <= N; ++ nod)
        {
            DP[nod][T] = DP[nod][T-1] + Amenda[nod][T];
            for(vector<pair<int,int> >::iterator it = G[nod].begin(); it != G[nod].end(); ++it)
                if(T >= it->timp)
                    if(DP[nod][T] < DP[it->next][T - it->timp] + Amenda[nod][T])
                        DP[nod][T] = DP[it->next][T - it->timp] + Amenda[nod][T];
        }

    int a,b;
    for(int i = 1; i <= P; ++i)
    {
        scanf("%d%d",&a,&b);
        if(DP[a][b] >= 0)
            printf("%d\n",DP[a][b]);
        else
            printf("-1");
    }
}

int main()
{
    freopen("amenzi.in","r",stdin);
    freopen("amenzi.out","w",stdout);

    Read();
    Solve();

    return 0;
}