Pagini recente » Cod sursa (job #2024391) | Cod sursa (job #976742) | Rating Gagea Andrei (Andrei_Gagea) | Cod sursa (job #740858) | Cod sursa (job #2087714)
#include <bits/stdc++.h>
using namespace std;
ifstream in("distante.in");
ofstream out("distante.out");
const int Nmax = 5e4 + 50;
vector < pair < int, int > > G[Nmax];
int dist[Nmax], dist_bronzarel[Nmax];
bool viz[Nmax];
struct cmp{
bool operator() (int a, int b){
return dist[a] > dist[b];
}
};
void Dijkstra(int src)
{
priority_queue < int, vector < int >, cmp > pq;
pq.push(src);
viz[src] = true;
while(!pq.empty()) {
int node = pq.top(); pq.pop();
viz[node] = false;
for(const auto it: G[node]) {
if(dist[node] + it.second < dist[it.first]) {
dist[it.first] = dist[node] + it.second;
if(viz[it.first] == false) {
pq.push(it.first);
viz[it.first] = true;
}
}
}
}
}
int main()
{
int T;
in >> T;
for(int i = 1; i <= T; ++i) {
int n, m, src;
in >> n >> m >> src;
for(int j = 1; j <= n; ++j) in >> dist_bronzarel[j];
for(int j = 1; j <= n; ++j)
G[j].clear();
for(int j = 1; j <= m; ++j) {
int a, b, c;
in >> a >> b >> c;
G[a].push_back({b, c});
G[b].push_back({a, c});
}
for(int j = 1; j <= n; ++j)
if(j != src)
dist[j] = INT_MAX;
Dijkstra(src);
for(int j = 1; j <= n; ++j)
if(dist[j] == INT_MAX)
dist[j] = 0;
bool ok = true;
for(int j = 1; j <= n; ++j)
if(dist[j] != dist_bronzarel[j]) {
out << "NU\n";
ok = false;
break;
}
if(ok == true)
out << "DA\n";
}
return 0;
}