Pagini recente » Cod sursa (job #516917) | Cod sursa (job #106490) | Cod sursa (job #3129550) | Cod sursa (job #1696963) | Cod sursa (job #2236199)
#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
vector < int > LOC; /// UNDE SE VEDE CU NEVASTA
vector < int > CAND; /// CAND SE VEDE CU NEVASTA
int max_CAND;
int mat[ NMAX ][ 3502 ];
struct Stare
{
int nod, moment;
} temp;
struct comp
{
bool operator()( const Stare & A, const Stare & B )
{
return A.moment > B.moment;
}
};
priority_queue < Stare, vector < Stare >, comp > Heap;
void Do();
void Print( int A, int B );
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 >> a >> b;
max_CAND = max( max_CAND, b );
LOC.push_back( a );
CAND.push_back( b );
}
TAIM = max_CAND;
Do();
for( int i = 0; i < tasks; ++i )
Print( LOC[ i ], CAND[ i ] );
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.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 = mat[ n_curent ][ t_curent ];
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 ];
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 ];
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( int A, int B )
{
bool ok = false;
for( int i = B; i >= 1; --i )
if( mat[ A ][ i ] > -INF )
{
fout << mat[ A ][ i ] << '\n';
ok = true;
return;
}
if( ! ok ) fout << "-1" << '\n';
}
int main()
{
Read();
/// Test();
return 0;
}