Pagini recente » Cod sursa (job #2598707) | Cod sursa (job #3168088) | Cod sursa (job #1450757) | Cod sursa (job #3168059) | Cod sursa (job #2604370)
#include <bits/stdc++.h>
//std::ifstream fin ("input.txt");
//std::ofstream fout ("output.txt");
std::ifstream fin ("distante.in");
std::ofstream fout ("distante.out");
int *dist_in;
int *dist;
std::vector < std::pair < int, int > > *edge;
std::priority_queue < int, std::vector < std::pair < int, int > >, std::greater < std::pair < int, int > > > pq;
void Dijkstra (){
while (!pq.empty()){
int cost = pq.top().first;
int node = pq.top().second;
pq.pop();
//std::cout << node << '\n';
for(int i=0; i<edge[node].size(); i++){
int next_cost = edge[node][i].second;
int next_node = edge[node][i].first;
if (dist[next_node] == 0 or dist[next_node] > cost + next_cost){
dist[next_node] = cost + next_cost;
//std::cout << "node: " << next_node << " new value: " << dist[next_node] << '\n';
pq.push ({dist[next_node], next_node});
}
}
}
}
int main()
{
std::ios_base::sync_with_stdio(false);
std::cin.tie (NULL);
std::cout.tie(NULL);
int Q;
fin >> Q;
while (Q--){
int V, E, src, dest, cost, i;
fin >> V >> E >> src;
edge = new std::vector < std::pair < int, int > > [V+2];
dist_in = new int [V+2];
memset (dist_in, 0, sizeof dist_in);
dist = new int [V+2];
memset (dist, 0, sizeof dist);
pq.push ({0, src});
dist[src] = -1;
for (i=1; i<=V; i++)
fin >> dist_in[i];
for (i=0; i<E; i++){
fin >> src >> dest >> cost;
edge[src].push_back ({dest, cost});
edge[dest].push_back ({src, cost});
}
Dijkstra ();
bool correct = true;
for (i=1; i<=V; i++)
if (dist[i] == -1)
dist[i] = 0;
for (i=1; i<=V; i++)
if (dist[i] != dist_in[i])
correct = false;
if (correct == false)
fout << "NU\n";
else
fout << "DA\n";
}
return 0;
}