Cod sursa(job #3284957)

Utilizator PsyDuck1914Feraru Rares-Serban PsyDuck1914 Data 12 martie 2025 13:18:12
Problema Amenzi Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.76 kb
#include <bits/stdc++.h>

using namespace std;

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

const int KMAX = 12e3;
const int PMAX = 8e3;
const int NMAX = 150;
const int MMAX = 15e2;
const int TMAX = 35e2;

int n, m, p, k;

vector<pair<int, int>> adj[NMAX+1];
long long amenda[NMAX+1][TMAX+10], dp[NMAX+1][TMAX+10];
bool vis[NMAX+1][TMAX+10];

int main()
{
    
    f >> n >> m >> k >> p;
    
    
    for(int i=1; i<=m; i++){
        
        int a, b, c;
        f >> a >> b >> c;
        
        //cout << a << ' ' << b << ' ' << c << endl;
        
        adj[a].push_back({b, c});
        adj[b].push_back({a, c});
        
        
    }
    
    for(int i=1; i<=k; i++){
        
        int a, b, c;
        f >> a >> b >> c;
        
        amenda[a][b] += c;
        
    }
    
    vis[1][0] = true;
    dp[1][0] = amenda[1][0];
    
    for(int t=0; t<=TMAX+5; t++){
        
        for(int i=1; i<=n; i++){
            
            for(auto [next, timp] : adj[i]){
                
                
                if(t - timp >= 0 and vis[next][t - timp]){
                    
                    vis[i][t] = true;
                    
                    dp[i][t] = max(dp[i][t], dp[next][t - timp] + amenda[i][t]);
                    
                    
                }
                
                if(t - 1 >= 0 and vis[next][t-1])
                    dp[i][t] = max(dp[i][t], dp[i][t - 1] + amenda[i][t]);
                
               
                
            }
            
        }
        
    }
    
    //cout << dp[0][0] << endl;
    
    for(int i=1; i<=p; i++){
        
        int a, b;
        f >> a >> b;
        if(!vis[a][b])
            g << -1 << "\n";
        else
            g << dp[a][b] << "\n";
        
    }
    

    return 0;
}