Pagini recente » Cod sursa (job #1831191) | Cod sursa (job #1301497) | Cod sursa (job #2485476) | Cod sursa (job #161458) | Cod sursa (job #2830725)
#include <iostream>
#include <vector>
#include <fstream>
#include <queue>
using namespace std;
const int NMAX = 50000;
struct edge {
int node, cost;
edge(int node, int cost)
{
this->node = node;
this->cost = cost;
}
};
int main()
{
ifstream f("distante.in");
ofstream g("distante.out");
int T, m,n,s, x, y ,c;
f >> T;
while (T > 0 )
{
f >> n >> m >> s;
vector<vector<edge>> edges(n+1);
int distances[NMAX];
for(int i = 1; i<= n; i++)
{
f >> distances[i];
}
for(int i = 0; i< m ; i++)
{
f >> x >> y >> c;
edges[x].push_back(edge(y,c));
edges[y].push_back(edge(x,c));
}
vector<int> minDistances(n+1, 1001);
priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> pq;
minDistances[s] = 0;
pq.push(make_pair(0, s));
while (!pq.empty())
{
int current_node = pq.top().second;
pq.pop();
for(auto i : edges[current_node]) {
if (minDistances[current_node] + i.cost < minDistances[i.node]) {
minDistances[i.node] = minDistances[current_node] + i.cost;
pq.push(make_pair(minDistances[i.node], i.node));
}
}
}
int ok =1;
for(int i = 1; i<= n; i++)
{
if (minDistances[i] != distances[i]) {
ok = 0;
break;
}
}
if(ok == 1) {
g << "DA" << "\n";
}
else g << "NU" << "\n";
T--;
}
}