Pagini recente » Cod sursa (job #1660290) | Cod sursa (job #952130) | Cod sursa (job #761393) | Cod sursa (job #1935666) | Cod sursa (job #3238113)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <climits>
using namespace std;
ifstream fin("distante.in");
ofstream fout("distante.out");
struct Edge {
int node, cost;
};
struct Compare {
bool operator()(pair<int, int> a, pair<int, int> b) {
return a.second > b.second;
}
};
void dijkstra(int N, int S, vector<vector<Edge>>& graph, vector<int>& distances) {
priority_queue<pair<int, int>, vector<pair<int, int>>, Compare> pq;
distances.assign(N + 1, INT_MAX);
distances[S] = 0;
pq.push({S, 0});
while (!pq.empty()) {
int currentNode = pq.top().first;
int currentDist = pq.top().second;
pq.pop();
if (currentDist > distances[currentNode]) continue;
for (auto& edge : graph[currentNode]) {
int nextNode = edge.node;
int nextDist = currentDist + edge.cost;
if (nextDist < distances[nextNode]) {
distances[nextNode] = nextDist;
pq.push({nextNode, nextDist});
}
}
}
}
int main() {
int T;
fin >> T;
while (T--) {
int N, M, S;
fin >> N >> M >> S;
vector<int> bronzarelDistances(N + 1);
for (int i = 1; i <= N; ++i) {
fin >> bronzarelDistances[i];
}
vector<vector<Edge>> graph(N + 1);
for (int i = 0; i < M; ++i) {
int a, b, c;
fin >> a >> b >> c;
graph[a].push_back({b, c});
graph[b].push_back({a, c});
}
vector<int> calculatedDistances(N + 1);
dijkstra(N, S, graph, calculatedDistances);
bool correct = true;
for (int i = 1; i <= N; ++i) {
if (calculatedDistances[i] != bronzarelDistances[i]) {
correct = false;
break;
}
}
if (correct) {
fout << "DA\n";
} else {
fout << "NU\n";
}
}
fin.close();
fout.close();
return 0;
}