Pagini recente » Cod sursa (job #2963961) | Cod sursa (job #2646120) | Cod sursa (job #1124801) | Cod sursa (job #1917292) | Cod sursa (job #2648061)
#include <bits/stdc++.h>
#define newline '\n'
#define STOP fout.close(); return 0;
using namespace std;
ifstream fin("distante.in");
ofstream fout("distante.out");
///***********************
const int NMAX = 5e4 + 3;
struct Edge {
int to, cost;
bool operator < (const Edge &other) const {
return cost > other.cost;
}
};
int n, m, s;
vector<Edge> graph[NMAX];
vector<int> dis, trueDis;
void reset() {
trueDis = dis = vector<int>(n, 2e9);
for (int i = 0; i < NMAX; i++)
graph[i].clear();
}
void read() {
fin >> n >> m >> s;
reset();
for (int &it : dis)
fin >> it;
for (int a, b, c, i = 0; i < m; i++) {
fin >> a >> b >> c;
a--, b--;
graph[a].push_back({b, c});
graph[b].push_back({a, c});
}
}
bool vis[NMAX];
void dijkstra(int start) {
fill(vis, vis + n + 1, false);
priority_queue<Edge> q;
q.push({start, 0});
trueDis[start] = 0;
while (!q.empty()) {
int node = q.top().to;
q.pop();
if (vis[node])
continue;
vis[node] = true;
for (auto it : graph[node]) {
if (trueDis[it.to] > trueDis[node] + it.cost) {
trueDis[it.to] = trueDis[node] + it.cost;
it.cost = trueDis[it.to];
q.push(it);
}
}
}
}
void solveT() {
read();
dijkstra(s - 1);
if (dis == trueDis)
fout << "DA";
else
fout << "NU";
fout << newline;
}
int main() {
int t;
for (fin >> t; t--;)
solveT();
STOP
}