Cod sursa(job #1501854)

Utilizator laurageorgescuLaura Georgescu laurageorgescu Data 13 octombrie 2015 21:41:48
Problema Amenzi Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.76 kb
#include<fstream>
#include<vector>

using namespace std;

ifstream fin( "amenzi.in" ); ofstream fout( "amenzi.out" );

const int nmax = 150;
const int tmax = 3500;
const int inf = 1 << 30;
int n, m, k, q;
int a[ nmax + 1 ][ tmax + 1 ];
int d[ tmax + 1 ][ nmax + 1 ];

struct muchie{
    int x, c;
    muchie() {}
    muchie( int _x, int _c ) : x( _x ), c( _c ) {}
};
typedef vector< muchie > graf;
graf g[ nmax + 1 ];

inline int max2( int x, int y ) {
    return ( x < y ? y : x );
}
void citire() {
    fin >> n >> m >> k >> q;
    for( int i = 0; i < m; ++ i ) {
        int x, y, z;
        fin >> x >> y >> z;
        g[ x ].push_back( muchie( y, z ) );
        g[ y ].push_back( muchie( x, z ) );
    }
    for( int i = 0; i < k; ++ i ) {
        int x, t, c;
        fin >> x >> t >> c;
        a[ x ][ t ] = max2( a[ x ][ t ], c );
    }
}
inline void set_values() {
    for( int t = 0; t <= tmax; ++ t ) {
        for( int i = 1; i <= n; ++ i ) {
            d[ t ][ i ] = -inf;
        }
    }
}
void solve() {
    d[ 0 ][ 1 ] = 0;
    for( int t = 1; t <= tmax; ++ t ) {
        for( int i = 1; i <= n; ++ i ) {
            d[ t ][ i ] = d[ t - 1 ][ i ];
            for( graf::iterator it = g[ i ].begin(); it != g[ i ].end(); ++ it ) {
                if ( t >= (it -> c) ) {
                    d[ t ][ i ] = max2( d[ t ][ i ], d[ t - (it -> c) ][ (it -> x ) ] + a[ (it -> x ) ][ t - (it -> c) ] );
                }
            }
        }
    }
}
void queries() {
    while ( q -- ) {
        int x, y;
        fin >> x >> y;
        if ( d[ y ][ x ] < 0 ) {
            fout << "-1\n";
        } else {
            fout << d[ y ][ x ] << "\n";
        }
    }
}
int main() {
    citire();
    set_values();
    solve();
    queries();

    fin.close();
    fout.close();
    return 0;
}