Pagini recente » Cod sursa (job #270701) | Cod sursa (job #3238089) | Clasamentul arhivei de probleme | Cod sursa (job #1482062) | Cod sursa (job #2360500)
#include <bits/stdc++.h>
using namespace std;
ifstream in("distante.in");
ofstream out("distante.out");
typedef pair <int, int> PII;
#define NMax 50000
#define pb push_back
int N, M, P, S, drum[NMax], D[NMax];
const int oo = (1 << 30);
vector <PII> v[NMax];
void Citire(){
for(int i = 1; i<=N; i++)
in >> drum[i];
for(int i = 1; i<=M; i++){
int x, y, d;
in >> x >> y >> d;
v[x].pb({y,d});
}
}
void Dijkstra(int k){
priority_queue<PII,vector<PII>,greater<PII>> Q;
Q.push({0,k});
D[k] = 0;
while(!Q.empty()){
int len = Q.top().first, nod = Q.top().second;
Q.pop();
if(len!=D[nod])
continue;
for(auto it : v[nod])
if(len + it.second < D[it.first]){
D[it.first] = len + it.second;
Q.push({D[it.first], it.first});
}
}
}
int main(){
in.sync_with_stdio(false);
in >> P;
for(int i = 1; i<=P; i++){
in >> N >> M >> S;
Citire();
for(int i = 1; i<=N ; i++)
D[i] = oo;
Dijkstra(S);
int ok = 1;
for(int i = 1; i<=N; i++){
if(D[i] != drum[i])
ok = 0;
}
if(ok)
out <<"DA" <<"\n";
else
out <<"NU" <<"\n";
}
}