Cod sursa(job #3267482)

Utilizator PsyDuck1914Feraru Rares-Serban PsyDuck1914 Data 11 ianuarie 2025 12:26:04
Problema Amenzi Scor 70
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.66 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];
int 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;
    
    for(int t=1; t<=TMAX+5; t++){
        
        for(int i=1; i<=n; i++){
            
            for(auto [next, timp] : adj[i]){
                
                
                if(t - timp >= 0 and vis[i][t - timp]){
                    
                    vis[next][t] = true;
                    
                    dp[next][t] = max(dp[next][t], dp[i][t - timp] + amenda[next][t]);
                    dp[next][t] = max(dp[next][t], dp[next][t - 1] + amenda[next][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;
}