Pagini recente » Cod sursa (job #1237971) | Cod sursa (job #1904428) | Cod sursa (job #724039) | Cod sursa (job #1498018) | Cod sursa (job #2464246)
#include <fstream>
#include <vector>
#include <set>
using namespace std;
struct el
{
int node, cost;
inline bool operator<(const el &other) const
{
return this->cost < other.cost;
}
};
int main()
{
ifstream fin("distante.in");
ofstream fout("distante.out");
int tests;
fin >> tests;
while (tests-- > 0)
{
int nodes, edges, start;
fin >> nodes >> edges >> start;
vector <int> a(nodes + 1, 0);
vector <int> dist(nodes + 1, (1 << 30));
vector <el> graph[nodes + 1];
for (int i = 1;i <= nodes;++i)
fin >> a[i];
for (int i = 1;i <= edges;++i)
{
int x, y, z;
fin >> x >> y >> z;
graph[x].push_back({y, z});
graph[y].push_back({x, z});
}
set <el> pq;
pq.insert({start, 0});
dist[start] = 0;
while (!pq.empty())
{
el aux = *pq.begin();
pq.erase(*pq.begin());
int node = aux.node;
int cost = aux.cost;
for (auto &x : graph[node])
{
if (dist[x.node] > dist[aux.node] + x.cost)
{
el next;
next.node = x.node;
next.cost = dist[x.node];
pq.erase(next);
next.cost = dist[aux.node] + x.cost;
dist[x.node] = dist[aux.node] + x.cost;
pq.insert(next);
}
}
}
dist[0] = 0;
fout << ((dist == a) ? "DA\n" : "NU\n");
}
fin.close();
fout.close();
return 0;
}