Cod sursa(job #514770)

Utilizator ssergiussSergiu-Ioan Ungur ssergiuss Data 19 decembrie 2010 15:58:07
Problema Amenzi Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.92 kb
#include <algorithm>
#include <cstring>
#include <fstream>
#include <vector>

using namespace std;

#define f first
#define s second
#define mp make_pair
#define pb push_back

const char Input[] = "amenzi.in";
const char Output[] = "amenzi.out";

const int Dim = 151;
const int Max = 3501;

int N, M, K, P;
int sol[Dim][Max];
vector < pair <int, int> > a[Dim], v[Dim];

void Calc( int nod, int tmp ) {

    vector < pair <int, int> > :: iterator it;

    if( sol[nod][tmp] != -1 )
        return;

    if( tmp > 0 ) {

        Calc( nod, tmp - 1 );
        sol[nod][tmp] = sol[nod][tmp - 1];
    }

    for( it = v[nod].begin(); it != v[nod].end(); ++it )
        if( tmp - it->s >= 0 ) {

            Calc( it->f, tmp - it->s );
            sol[nod][tmp] = max( sol[nod][tmp], sol[it->f][tmp - it->s] );
        }

    for( it = a[nod].begin(); sol[nod][tmp] != -1 && it != a[nod].end() && it->f <= tmp; ++it )
        if( it->f == tmp )
            sol[nod][tmp] += it->s;
}

int main() {

    ifstream fin( Input );
    ofstream fout( Output );

    int i, c, x, y;
    vector < pair <int, int> > :: iterator it;

    fin >> N >> M >> K >> P;
    while( M-- ) {

        fin >> x >> y >> c;
        v[x].pb( mp( y, c ) );
        v[y].pb( mp( x, c ) );
    }
    while( K-- ) {

        fin >> x >> y >> c;
        a[x].pb( mp( y, c ) );
    }

    for( i = 1; i <= N; ++i )
        sort( a[i].begin(), a[i].end() );

    memset( sol, -1, sizeof( sol ) );
    for( sol[1][0] = 0, it = a[1].begin(); it != a[1].end() && it->f == 0; ++it )
        sol[1][0] += it->s;

    while( P-- ) {

        fin >> x >> y;
        Calc( x, y );
        fout << sol[x][y] << "\n";
    }

///DE
//    for( i = 1; i <= N; fout << "\n", ++i )
//        for( int j = 0; j <= 50; ++j )
//            fout << sol[i][j] << " ";
///BUG

    fin.close();
    fout.close();

    return 0;
}