Pagini recente » Cod sursa (job #2849158) | Cod sursa (job #134330) | Cod sursa (job #2207934) | Cod sursa (job #2833470) | Cod sursa (job #3355093)
#include <fstream>
#include <vector>
#include <unordered_map>
#include <queue>
using namespace std;
const int INF = 1e7;
int main(void){
ifstream fin("distante.in");
ofstream fout("distante.out");
int t;
fin >> t;
for(int i = 0; i < t; ++i){
int n, m, s;
fin >> n >> m >> s;
s--;
vector<int> res(n);
for(int j = 0; j < n; ++j)
fin >> res[j];
unordered_map<int, vector<pair<int, int>>> graph;
for(int j = 0; j < m; ++j){
int a, b, c;
fin >> a >> b >> c;
a--; b--;
graph[a].push_back(make_pair(b, c));
graph[b].push_back(make_pair(a, c));
}
vector<int> distances(n, INF);
distances[s] = 0;
priority_queue<
pair<int, int>,
vector< pair<int, int>>,
greater< pair<int, int>>
> q;
q.push({0, s});
while(!q.empty()){
auto [dis, node] = q.top();
q.pop();
if(distances[node] < dis)
continue;
for(auto [to, cst] : graph[node]){
if(dis + cst < distances[to]){
distances[to] = dis + cst;
q.push({distances[to], to});
}
}
}
bool ok = 1;
for(int j = 0; j < n; ++j){
if(res[j] != distances[j]){
ok = 0;
break;
}
}
if(ok){
fout << "DA" << endl;
} else {
fout << "NU" << endl;
}
}
return 0;
}