Pagini recente » Cod sursa (job #1176328) | Cod sursa (job #583072) | Cod sursa (job #1757933) | Cod sursa (job #2194747) | Cod sursa (job #566698)
Cod sursa(job #566698)
#include<fstream>
#include<vector>
#define maxN 50005
#define INF 1 << 30
using namespace std;
ifstream f("distante.in");
ofstream g("distante.out");
int i,x,y,Poz[maxN],D[maxN],D2[maxN],H[maxN];
int T,ii,L,cst,N,M,S,a,b;
vector< pair<int,int> >A[maxN];
void urca(int poz){
while ( poz != 0 && D[ H[poz/2] ] > D[ H[poz] ] ){
swap(H[poz],H[poz/2]);
swap(Poz[H[poz]],Poz[H[poz/2]]);
poz = poz >> 1;
}
}
void coboara(int x){
int y = 0;
while ( x != y ){
y = x;
if ( (y << 1) <= L && D[H[ y << 1 ]] < D[H[ x ]] )
x = y << 1;
if ( (y << 1) + 1 <= L && D[H[(y<<1)+1]] < D[H[ x ]] )
x = ( y << 1 ) + 1;
swap(H[x],H[y]);
swap(Poz[H[x]],Poz[H[y]]);
}
}
void delete_heap () {
H[1] = H[L--]; Poz[H[1]] = 1;
coboara(1);
}
int dijkstra () {
f >> N >> M >> S;
for ( i = 1 ; i <= N ; ++i ){
f >> D2[i];
D[i] = INF;
Poz[i] = 0;
A[i].clear();
}
for ( i = 1 ; i <= M ; ++i ){
f >> a >> b >> cst;
A[a].push_back( make_pair( b,cst ) );
A[b].push_back( make_pair( a,cst ) );
}
D[S] = 0;
L = 1;
H[L] = S; Poz[S] = 1;
while ( L ){
int nod = H[1];
delete_heap();
if ( D[nod] != D2[nod] )
return 0;
for ( i = 0 ; i < A[nod].size() ; ++i ){
int nd = A[nod][i].first;
int cst = A[nod][i].second;
if ( D[nod] + cst < D[nd] ){
D[nd] = D[nod] + cst;
if ( Poz[nd] ){
urca(Poz[nd]);
}
else{
++L;
H[L] = nd;
Poz[nd] = L;
urca(L);
}
}
}
}
return 1;
}
int main () {
f >> T;
for ( ii = 1 ; ii <= T ; ++ii ){
if ( dijkstra() )
g << "DA\n";
else
g << "NU\n";
}
f.close();
g.close();
return 0;
}