Pagini recente » Cod sursa (job #456944) | Cod sursa (job #2776163) | Cod sursa (job #1894680) | Cod sursa (job #448527) | Cod sursa (job #1387122)
#include<fstream>
#include<vector>
using namespace std;
ifstream fin( "distante.in" );
ofstream fout( "distante.out" );
const int nmax = 50000;
const int inf = 1 << 30;
int v[ nmax + 1 ];
int hn, h[ 2 * nmax + 1 ], p[ nmax + 1 ], d[ nmax + 1 ];
struct muchie{ int x, cost; };
vector< muchie > g[ nmax + 1 ];
inline muchie mp( int x, int cost ) {
muchie s; s.x = x; s.cost = cost; return s;
}
inline bool in_fata( int a, int b ) {
return ( d[ h[ a ] ] < d[ h[ b ] ] );
}
inline void heap_swap( int a, int b ) {
p[ h[ a ] ] = b; p[ h[ b ] ] = a;
int aux = h[ a ]; h[ a ] = h[ b ]; h[ b ] = aux;
}
void urcare( int pos ) {
while ( pos > 1 && in_fata( pos, (pos >> 1) ) ) {
heap_swap( pos, (pos >> 1) );
pos >>= 1;
}
}
void coborare( int pos ) {
int q; bool ok = 1;
while ( (pos << 1) <= hn && ok == 1 ) {
q = (pos << 1);
if ( q + 1 <= hn && in_fata( q + 1, q ) ) {
++ q;
}
if ( in_fata( q, pos ) ) {
heap_swap( q, pos );
pos = q;
} else {
ok = 0;
}
}
}
void heap_insert( int x ) {
h[ ++ hn ] = x;
p[ x ] = hn;
urcare( hn );
}
void heap_pop() {
p[ h[ 1 ] ] = -1;
heap_swap( 1, hn );
-- hn;
coborare( 1 );
}
void update( int nod ) {
for( vector< muchie >::iterator it = g[ nod ].begin(); it != g[ nod ].end(); ++ it ) {
if ( p[ it -> x ] != -1 && d[ it -> x ] > d[ nod ] + it -> cost ) {
d[ it -> x ] = d[ nod ] + it -> cost;
urcare( p[ it -> x ] );
}
}
}
int main() {
int t, n, m, s, x, y, z;
bool ok;
fin >> t;
while ( t -- ) {
fin >> n >> m >> s;
hn = 0; ok = 1;
for( int i = 1; i <= n; ++ i ) {
fin >> v[ i ];
d[ i ] = inf; heap_insert( i );
g[ i ].clear();
}
for( int i = 0; i < m; ++ i ) {
fin >> x >> y >> z;
g[ x ].push_back( mp( y, z ) );
g[ y ].push_back( mp( x, z ) );
}
d[ s ] = 0; urcare( p[ s ] );
while ( hn > 0 ) {
x = h[ 1 ];
heap_pop();
update( x );
}
for( int i = 1; i <= n && ok == 1; ++ i ) {
if ( d[ i ] != v[ i ] ) {
ok = 0;
}
}
if ( ok == 1 ) {
fout << "DA\n";
} else {
fout << "NU\n";
}
}
fin.close();
fout.close();
return 0;
}