Pagini recente » Cod sursa (job #1419893) | Cod sursa (job #1232239) | Cod sursa (job #2865773) | Cod sursa (job #815262) | Cod sursa (job #3004594)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("distante.in");
ofstream fout("distante.out");
typedef pair<int, int> pii;
const int NMAX = 5e4;
short tests;
int n, m, s, dp[NMAX + 1], a[NMAX + 1], v[NMAX + 1];
vector<pii> adj[NMAX + 1];
priority_queue<pii, vector<pii>, greater<pii> > pq;
void resetVars() {
for(int i = 0; i < NMAX; ++i) adj[i].clear();
memset(dp, 0, sizeof(dp));
memset(a, 0, sizeof(a));
memset(v, 0, sizeof(v));
}
void dijkstra(int sn) {
for(int i = 1; i <= n; ++i)
dp[i] = 1e9;
dp[sn] = 0;
pq.push({0, sn});
while(!pq.empty()) {
const int cx = pq.top().second;
pq.pop();
if(v[cx]) continue;
v[cx] = 1;
for(const auto &el : adj[cx]) {
const int ncx = el.first, ndist = el.second;
if(dp[ncx] > dp[cx] + ndist) {
dp[ncx] = dp[cx] + ndist;
pq.push({dp[ncx], ncx});
}
}
}
}
void solve() {
resetVars();
fin >> n >> m >> s;
for(int i = 1; i <= n; ++i)
fin >> a[i];
for(int i = 1, x, y, cost; i <= m; ++i) {
fin >> x >> y >> cost;
adj[x].push_back({y, cost});
adj[y].push_back({x, cost});
}
dijkstra(s);
bool ok = 1;
for(int i = 1; i <= n && ok; ++i)
ok = (a[i] == dp[i]);
ok ? fout << "DA\n" : fout << "NU\n";
}
int main()
{
fin >> tests;
while(tests--) solve();
return 0;
}