Pagini recente » Cod sursa (job #178469) | Cod sursa (job #609515) | Cod sursa (job #184524) | Cod sursa (job #1603064) | Cod sursa (job #1104953)
#include <cstdio>
#include <vector>
using namespace std;
#define MaxN 10050
#define MaxW 1000050
int gen[MaxW], w;
vector <int>vec[MaxN];
vector <int>gcost[MaxN];
int nrvec[MaxN];
int heap[MaxN], nrheap;
int heaptov[MaxN];
int vtoheap[MaxN];
int cost[MaxN];
char visited[MaxN];
void Heap_Swap( int nod1, int nod2 )
{
int dummy = heap[nod1];
heap[nod1] = heap[nod2];
heap[nod2] = dummy;
dummy = heaptov[nod1];
heaptov[nod1] = heaptov[nod2];
heaptov[nod2] = dummy;
vtoheap[heaptov[nod1]] = nod1;
vtoheap[heaptov[nod2]] = nod2;
}
void Heap_Up( int nod )
{
while ( true )
{
if ( heap[nod] < heap[nod/2] )
Heap_Swap( nod, nod/2 );
else
break;
nod /= 2;
}
}
void Heap_Down( int nod )
{
while ( true )
{
if ( nod*2+1 > nrheap )
{
if ( nod*2 > nrheap )
break;
else
{
if ( heap[nod*2] < heap[nod] )
{
Heap_Swap( nod*2, nod );
nod *= 2;
continue;
}
else
break;
}
}
else
{
if ( heap[nod*2] < heap[nod] )
{
if ( heap[nod*2+1] < heap[nod*2] )
{
Heap_Swap( nod*2+1, nod );
nod = nod*2+1;
continue;
}
else
{
Heap_Swap( nod*2, nod );
nod *= 2;
continue;
}
}
else
{
if ( heap[nod*2+1] < heap[nod] )
{
Heap_Swap( nod*2+1, nod );
nod = nod*2+1;
continue;
}
else
break;
}
}
}
}
void Heap_Add( int nod )
{
heap[++nrheap] = cost[nod];
heaptov[nrheap] = nod;
vtoheap[nod] = nrheap;
Heap_Up( nrheap );
}
void Heap_Update( int nod ) // hnod
{
heap[nod] = cost[heaptov[nod]];
if ( heap[nod] < heap[nod/2] )
Heap_Up( nod );
else
Heap_Down( nod );
}
void Heap_DeleteFirst()
{
Heap_Swap( 1, nrheap-- );
Heap_Down( 1 );
}
int wait( int val, int timp ) // timp post-trecere
{
while ( timp <= w )
{
if ( gen[timp] >= val )
return timp;
++timp;
}
return 0;
}
int main()
{
heap[0] = -1;
int n, m;
freopen( "mine.in", "r", stdin );
freopen( "mine.out", "w", stdout );
scanf( "%d %d\n", &n, &m );
int i;
int x, y, c;
for ( i = 1; i <= m; ++i )
{
scanf( "%d %d %d\n", &x, &y, &c );
vec[x].push_back(y);
gcost[x].push_back(c);
++nrvec[x];
vec[y].push_back(x);
gcost[y].push_back(c);
++nrvec[y];
}
scanf( "%d\n", &w );
for ( i = 1; i <= w; ++i )
scanf( "%d ", &gen[i] );
Heap_Add( 1 );
int nod;
while ( nrheap )
{
nod = heaptov[1];
if ( nod == n )
break;
for ( i = 0; i < nrvec[nod]; ++i )
{
if ( visited[vec[nod][i]] )
continue;
c = wait( gcost[nod][i], cost[nod]+1 );
if ( !c )
continue;
if ( !heaptov[vec[nod][i]] )
{
cost[vec[nod][i]] = c;
Heap_Add( vec[nod][i] );
}
else
{
if ( cost[vec[nod][i]] > c )
cost[vec[nod][i]] = c;
Heap_Update( vtoheap[vec[nod][i]] );
}
}
visited[nod] = 1;
Heap_DeleteFirst();
}
if ( ( n == heaptov[1] ) && ( nrheap ) )
{
printf( "%d\n", cost[n] );
}
else
printf( "-1\n" );
fclose( stdin );
fclose( stdout );
return 0;
}