Pagini recente » Cod sursa (job #732977) | Cod sursa (job #807148) | Cod sursa (job #1918884) | Cod sursa (job #753100) | Cod sursa (job #2970411)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("distante.in");
ofstream fout("distante.out");
void solve()
{
int n, m, s;
fin >> n >> m >> s;
vector<int> ver(n + 1);
for (int i = 1; i <= n; i++)
fin >> ver[i];
vector<vector<pair<int, int>>> graph(n + 1);
for (int i = 1; i <= m; i++)
{
int x, y, c;
fin >> x >> y >> c;
graph[x].push_back(make_pair(c, y));
graph[y].push_back(make_pair(c, x));
}
vector<int> dist(n + 1, INT_MAX), vis(n + 1, 0);
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> nodeQ;
nodeQ.push(make_pair(0, s));
dist[s] = 0;
while (!nodeQ.empty())
{
pair<int, int> curr = nodeQ.top();
nodeQ.pop();
if (vis[curr.second] == 1)
continue;
vis[curr.second] = 1;
for (auto &next : graph[curr.second])
if (dist[next.second] > next.first + dist[curr.second])
dist[next.second] = next.first + dist[curr.second], nodeQ.push(make_pair(dist[next.second], next.second));
}
for (int i = 1; i <= n; i++)
{
if (dist[i] == INT_MAX)
dist[i] = 0;
if (dist[i] != ver[i])
{
fout << "NU\n";
return;
}
}
fout << "DA\n";
return;
}
int main()
{
int t;
fin >> t;
while (t--)
{
solve();
}
return 0;
}