Pagini recente » Cod sursa (job #868016) | Cod sursa (job #165443) | Cod sursa (job #3177178) | Cod sursa (job #715373) | Cod sursa (job #1452345)
#include <cstdio>
#include <vector>
#include <cstring>
#include <algorithm>
#define DIM 50100
#define INF (1<<30)
#define f first
#define s second
using namespace std;
int N, M, T, Pos[DIM], Fin[DIM], Cost[DIM], X, Y, Z, K, S;
vector < int > V[DIM];
pair < int , int > Heap[DIM];
void Shift(int poz){
if(poz != 1 && Heap[poz].s < Heap[poz/2].s){
swap(Heap[poz], Heap[poz/2]);
Pos[Heap[ poz ].f] = poz;
Pos[Heap[poz/2].f] = poz/2;
Shift(poz/2);
}
return;
}
void Percolate(int poz){
int poz2 = poz * 2;
if(poz2 < K && Heap[poz2].s > Heap[poz2+1].s)
poz2 ++;
if(poz2 <= K && Heap[poz].s > Heap[poz2].s){
swap(Heap[poz], Heap[poz2]);
Pos[Heap[poz ].f] = poz;
Pos[Heap[poz2].f] = poz2;
Percolate(poz2);
}
return;
}
void Dijkstra(){
while(K != 0){
Cost[Heap[1].f] = Heap[1].s;
for(int i = 0; i < V[Heap[1].f].size(); i += 2){
int poz = Pos[V[Heap[1].f][i]], cost = V[Heap[1].f][i+1];
if(Heap[poz].s > Heap[1].s + cost){
Heap[poz].s = Heap[1].s + cost;
Shift(poz);
}
}
swap(Heap[1], Heap[K]);
Pos[Heap[1].f] = 1;
Pos[Heap[K].f] = K;
K --;
Percolate(1);
}
return;
}
int main(){
freopen("distante.in" ,"r", stdin );
freopen("distante.out","w", stdout);
scanf("%d", &T);
for(T = T; T; T --){
scanf("%d %d %d", &N, &M, &S);
K = 0;
memset(Pos , 0, sizeof(Pos ));
for(int i = 1; i < DIM; i ++)
V[i].resize(0);
for(int i = 1; i <= N; i ++)
scanf("%d", &Fin[i]);
for(int i = 1; i <= N; i ++){
if(i != S){
Heap[++K] = make_pair(i, INF);
Pos[i] = K; Shift(K);
}
else{
Heap[++K] = make_pair(i, 0 );
Pos[i] = K; Shift(K);
}
}
for(int i = 1; i <= M; i ++){
scanf("%d %d %d", &X, &Y, &Z);
V[X].push_back(Y); V[X].push_back(Z);
V[Y].push_back(X); V[Y].push_back(Z);
}
Dijkstra(); int ok = 1;
for(int i = 1; i <= N; i ++)
if(Cost[i] != Fin[i])
ok = 0;
switch(ok){
case 1:{printf("DA\n");break;}
case 0:{printf("NU\n");break;}
}
}
return 0;
}