Pagini recente » Cod sursa (job #1854603) | Cod sursa (job #2702695) | Cod sursa (job #2361047) | Cod sursa (job #1744030) | Cod sursa (job #2236193)
#include <fstream>
#include <queue>
#include <vector>
#define NMAX 152
#define INF 99999999
using namespace std;
ifstream fin( "amenzi.in" );
ofstream fout( "amenzi.out");
int N, M, K;
int tasks;
int place, TAIM;
vector < int > Ad [ NMAX ];
vector < int > Cost [ NMAX ];
vector < int > Infra_time[ NMAX ]; /// ORA AMENZILOR LA FIECARE NOD
vector < int > Infra_pret[ NMAX ]; /// COSTUL AMENZILOR LA FIECARE NOD
int mat[ NMAX ][ 3502 ];
struct Stare
{
int nod, moment, bani;
} temp;
struct comp
{
bool operator()( const Stare & A, const Stare & B )
{
return A.bani < B.bani;
}
};
priority_queue < Stare, vector < Stare >, comp > Heap;
void Do();
void Print();
void Read()
{
fin >> N >> M >> K >> tasks;
int a, b, c;
for( int i = 1; i <= M; ++i )
{
fin >> a >> b >> c;
Ad[ a ].push_back( b );
Cost[ a ].push_back( c );
Ad[ b ].push_back( a );
Cost[ b ].push_back( c );
}
for( int i = 1; i <= K; ++i )
{
fin >> a >> b >> c;
Infra_time[ a ].push_back( b );
Infra_pret[ a ].push_back( c );
}
for( int i = 1; i <= tasks; ++i )
{
fin >> place >> TAIM;
Do();
Print();
}
fin.close();
fout.close();
}
void Do()
{
for( int i = 0; i <= N; ++i )
for( int j = 0; j <= TAIM + 2; ++j )
mat[ i ][ j ] = -INF;
temp.bani = 0;
temp.moment = 0;
temp.nod = 1;
Heap.push( temp );
mat[ 1 ][ 0 ] = 0;
int n_curent;
int b_curent;
int t_curent;
int w;
while( !Heap.empty() )
{
n_curent = Heap.top().nod;
t_curent = Heap.top().moment;
b_curent = Heap.top().bani;
Heap.pop();
// fout << n_curent << ' ' << t_curent << ' ' << b_curent << '\n';
for( int i = 0; i < Ad[ n_curent ].size(); ++i )
if( t_curent + Cost[ n_curent ][ i ] <= TAIM ) /// DACA MAI ESTE TIMP DE MERS UNDEVA
{
w = Ad[ n_curent ][ i ];
for( int j = 0; j < Infra_time[ w ].size(); ++j )
if( t_curent + Cost[ n_curent ][ i ] <= Infra_time[ w ][ j ] ) /// DACA AJUNGE LA TIMP SA PRINDA INFRACTIUNEA
if( mat[ w ][ Infra_time[ w ][ j ] ] < b_curent + Infra_pret[ w ][ j ] )
{
mat[ w ][ Infra_time[ w ][ j ] ] = b_curent + Infra_pret[ w ][ j ];
temp.nod = w;
temp.moment = Infra_time[ w ][ j ];
temp.bani = mat[ w ][ Infra_time[ w ][ j ] ];
Heap.push( temp );
}
if( t_curent + Cost[ n_curent ][ i ] < TAIM && mat[ w ][ t_curent + Cost[ n_curent ][ i ] ] < b_curent )
{
mat[ w ][ t_curent + Cost[ n_curent ][ i ] ] = b_curent;
temp.nod = w;
temp.moment = t_curent + Cost[ n_curent ][ i ];
temp.bani = b_curent;
Heap.push( temp );
}
}
}
}
void Test()
{
for( int i = 1; i <= N; ++i )
{
for( int j = 0; j <= TAIM; ++j )
if( mat[i][j] == -INF ) fout << "* ";
else fout << mat[i][j] << ' ';
fout << '\n';
}
}
void Print()
{
bool ok = false;
for( int i = TAIM; i >= 1; --i )
if( mat[ place ][ i ] > -INF )
{
fout << mat[ place ][ i ] << '\n';
ok = true;
return;
}
if( ! ok ) fout << "-1" << '\n';
}
int main()
{
Read();
/// Test();
return 0;
}