Pagini recente » Cod sursa (job #1190996) | Cod sursa (job #2113599) | Cod sursa (job #1861508) | Cod sursa (job #2478535) | Cod sursa (job #2654117)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream f("distante.in");
ofstream g("distante.out");
const int NMAX = 50'001;
int T, N, M, SOURCE, dist[NMAX];
vector < pair < int, int > > G[NMAX];
struct Pq_Comparator {
bool operator()(const pair < int, int >& a, const pair < int, int >& b) const {
return a.second > b.second;
}
};
bool solve() {
f >> N >> M >> SOURCE;
for(int i = 1;i <= N;++i)
f >> dist[i];
while(M--) {
int x, y, c;
f >> x >> y >> c;
G[x].push_back( { y, c } );
G[y].push_back( { x, c } );
}
vector < int > d(N + 1, (1 << 30));
priority_queue < pair < int, int >, vector < pair < int, int > >, Pq_Comparator > pq;
pq.push({ SOURCE, 0 });
d[SOURCE] = 0;
while(!pq.empty()) {
const int node = pq.top().first;
const int cost = pq.top().second;
pq.pop();
if(d[node] == cost) {
for(pair < int, int >& neighbour : G[node]) {
if(d[neighbour.first] > d[node] + neighbour.second) {
d[neighbour.first] = d[node] + neighbour.second;
pq.push({ neighbour.first, d[neighbour.first] });
}
}
}
}
// end dijkstra
for(int i = 1;i <= N;++i)
G[i].clear();
for(int i = 1;i <= N;++i)
if(d[i] != dist[i])
return false;
return true;
}
int main() {
f >> T;
while(T--) {
g << (solve() ? "DA" : "NU") << '\n';
}
}