Pagini recente » Cod sursa (job #678778) | Cod sursa (job #3148502) | Cod sursa (job #1435234) | Cod sursa (job #244770) | Cod sursa (job #2981052)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
int inf = 2 * 1e9;
ifstream f("distante.in");
ofstream g("distante.out");
vector <pair <int ,int>> graf[50010];
int dist [50010];
bool verif[50010];
struct cmp{
bool operator()(int a, int b)
{
return dist[a] > dist[b];
}
};
void dijkstra(int k)
{
verif[k] = true;
priority_queue <int, vector <int>, cmp> pq;
dist[k] = 0;
pq.push(k);
while(!pq.empty())
{
int nod = pq.top();
pq.pop();
for(auto t : graf[nod])
if(dist[nod] + t.second < dist[t.first])
{
dist[t.first] = dist[nod] + t.second;
if(!verif[t.first])
{
verif[t.first] = true;
pq.push(t.first);
}
}
}
}
int main()
{
int t;
f >> t;
while(t--)
{
int n, m, s;
f >> n >> m >> s;
vector <int> sol(n);
for(int i = 0 ; i < n; ++i)
f >> sol[i];
fill(dist, dist + 50000, inf);
fill(verif, verif + 50000, false);
int x, y, c;
for(int i = 0; i < m; ++i)
{
f >> x >> y >> c;
graf[x].push_back({y, c});
}
dijkstra(s);
bool ok = true;
for(int i = 1; i <= n; ++i)
if(sol[i - 1] != dist[i])
ok = false;
if(ok)
cout << "DA";
else
cout << "NU";
cout << '\n';
}
return 0;
}