Pagini recente » Cod sursa (job #2881865) | Cod sursa (job #2224415) | Cod sursa (job #536382) | Cod sursa (job #600270) | Cod sursa (job #2762722)
#include <bits/stdc++.h>
using namespace std;
const int NMAX(50005);
int d[NMAX], dist[NMAX], a, b, c, n, m, st, nod, newC, i;
bool viz[NMAX], ok;
vector<pair<int, int> > G[NMAX];
set<pair<int, int> > s;
int main()
{
freopen("distante.in", "r", stdin);
freopen("distante.out", "w", stdout);
ios_base::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
int t;
cin >> t;
while(t--)
{
cin >> n >> m >> st;
for(i = 1; i <= n; ++i)
{
cin >> d[i];
G[i].clear();
dist[i] = (1 << 30);
viz[i] = 0;
}
for(i = 1; i <= m; ++i)
{
cin >> a >> b >> c;
G[a].push_back({b, c});
G[b].push_back({a, c});
}
s.clear();
s.insert({0, st});
dist[st] = 0;
while(!s.empty())
{
nod = s.begin()->second;
s.erase(s.begin());
viz[nod] = 1;
for(auto it: G[nod])
if(!viz[it.first])
{
newC = dist[nod] + it.second;
if(newC < dist[it.first])
{
if(dist[it.first] != (1 << 30))
s.erase(s.find({dist[it.first], it.first}));
dist[it.first] = newC;
s.insert({newC, it.first});
}
}
}
ok = 1;
for(i = 1; i <= n; ++i)
if(d[i] != dist[i])
{
ok = 0;
break;
}
cout << (ok == 1? "DA\n" : "NU\n");
}
return 0;
}