Cod sursa(job #2350926)

Utilizator tifui.alexandruTifui Ioan Alexandru tifui.alexandru Data 21 februarie 2019 20:11:07
Problema Amenzi Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.31 kb
#include <bits/stdc++.h>
#define Nmax 155
#define Tmax 3505

using namespace std;

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

int N,M,K,P,T;
list < pair <int,int> > G[Nmax];
vector < pair <int,int> > meeting;

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

void read_data(){

    f>>N>>M>>K>>P;
    int x,y,z;
    while(M--){

        f>>x>>y>>z;
        G[x].push_back({y,z});
        G[y].push_back({x,z});
    }

    while(K--){

        f>>x>>y>>z;
        fine[x][y]=z;
    }

    meeting.resize(P);
    for(auto &it:meeting){

        f>>it.first>>it.second;
        T=max(T,it.second);
    }
}

void compute_dp(){

    for(int j,i=0;i<=T;i++)
        for(j=0;j<=N;j++)
            dp[i][j]=-1;

    dp[1][0]=fine[1][0];

    for(int node,t=0;t<=T;t++)
        for(node=1;node<=N;node++)
            if(dp[node][t]>-1){

                dp[node][t+1]=max(dp[node][t+1],dp[node][t]+fine[node][t+1]);
                for(const auto &it:G[node])
                    dp[it.first][t+it.second]=max(dp[it.first][t+it.second],dp[node][t]+fine[it.first][t+it.second]);
            }
}

void print_ans(){

    for(const auto &it:meeting)
        g<<dp[it.first][it.second]<<'\n';
}

int main(){

    read_data();
    compute_dp();
    print_ans();

    return 0;
}