Pagini recente » Cod sursa (job #762262) | Cod sursa (job #3001) | Cod sursa (job #2504867) | Cod sursa (job #718226) | Cod sursa (job #3228646)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#define INF 2000000000
std::ifstream fin {"distante.in", std::ios::in};
std::ofstream fout {"distante.out", std::ios::out};
struct pqNode {
int node;
int dist;
};
struct SmallerPqComparator {
bool operator()(const pqNode &lhs, const pqNode &rhs) {
return lhs.dist > rhs.dist;
}
};
const char *dijkstraVerif(int n, int m, int src, std::vector<int> &distances) {
std::vector<std::vector<std::pair<int, int>>> adj (n + 1, std::vector<std::pair<int, int>> {});
int x, y, c;
for (int i = 0; i < m; i++) {
fin >> x >> y >> c;
adj[x].push_back(std::make_pair(y, c));
adj[y].push_back(std::make_pair(x, c));
}
std::vector<int> dist (n + 1, INF);
dist[src] = 0;
pqNode first = {src, 0};
std::priority_queue<pqNode, std::vector<pqNode>, SmallerPqComparator> pq;
pq.push(first);
while (!pq.empty()) {
pqNode top = pq.top();
pq.pop();
for (const auto &neigh : adj[top.node]) {
if (dist[neigh.first] > dist[top.node] + neigh.second) {
dist[neigh.first] = dist[top.node] + neigh.second;
pq.push({neigh.first, dist[neigh.first]});
}
}
}
bool isCompatible = true;
for (int i = 1; i <= n; i++)
if (dist[i] != distances[i])
isCompatible = false;
return isCompatible == true? "DA" : "NU";
}
int main(void) {
int t, val;
fin >> t;
while (t) {
t--;
std::vector<int> distances;
int n, m, src;
fin >> n >> m >> src;
distances.push_back(-1);
for (int i = 1; i <= n; i++) {
fin >> val;
distances.push_back(val);
}
fout << dijkstraVerif(n, m, src, distances) << '\n';
}
return 0;
}