Pagini recente » Cod sursa (job #1976343) | Cod sursa (job #2437790) | Cod sursa (job #1097800) | Cod sursa (job #400039) | Cod sursa (job #2464196)
#include <fstream>
#include <vector>
#include <bitset>
#include <queue>
using namespace std;
struct el
{
int node, cost;
inline bool operator<(const el &other) const
{
return this->cost > other.cost;
}
};
bitset <50005> seen;
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];
seen.reset();
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});
}
priority_queue <el> pq;
pq.push({start, 0});
dist[start] = 0;
while (!pq.empty())
{
el aux = pq.top();
pq.pop();
if (seen[aux.node] == 0)
{
seen[aux.node] = 1;
for (auto &x : graph[aux.node])
if (dist[x.node] > dist[aux.node] + x.cost)
{
dist[x.node] = dist[aux.node] + x.cost;
pq.push({x.node, dist[x.node]});
}
}
}
dist[0] = 0;
fout << ((dist == a) ? "DA\n" : "NU\n");
}
fin.close();
fout.close();
return 0;
}