Cod sursa(job #840625)

Utilizator stoicatheoFlirk Navok stoicatheo Data 22 decembrie 2012 22:19:38
Problema Amenzi Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.41 kb
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
 
ifstream f("amenzi.in");
ofstream g("amenzi.out");
 
#define nmax 155
#define Tmax 3502
 
int n, m, K, P, amenda[nmax][Tmax], dp[Tmax][nmax];
vector<pair<int,int> > gf[nmax];
 
void citeste(){
 
    f >> n >> m >> K >> P;
    for(int i=1; i<=m; i++){
        int x,y, z;
        f >> x >> y >> z;
        gf[x].push_back(make_pair(y,z));
        gf[y].push_back(make_pair(x,z));
    }
    for(int i=1; i<=K; i++){
        int x, y, z;
        f >> x >> y >> z;
        amenda[x][y] += z;
    }
}
 
 
void rezolva(){
 
    
 
    
    for(int i=0; i<Tmax; i++)
        for(int j=1; j<=n; j++) dp[i][j] = -1;
 
    dp[0][1] = amenda[1][0];
 
    for(int i=1; i<Tmax; i++){
        for(int j=1; j<=n; j++){
            dp[i][j] = max(dp[i][j], dp[i-1][j]);
            for(int k=0; k<gf[j].size(); k++){
                int vc = gf[j][k].first;
                int X = gf[j][k].second;
                if (i-X >= 0){ 
                    dp[i][j] = max(dp[i][j], dp[i-X][vc]);                 }
            }
            if (dp[i][j] != -1) dp[i][j] += amenda[j][i];
        }
 
    }
 
    for(int i=1; i<=P; i++){
        int x, y; 
        f >> x >> y;
        g << dp[y][x] << "\n";
    }
 
}
 
int main(){
 
    citeste();
    rezolva();
 
    f.close();
    g.close();
 
    return 0;
 
}