Pagini recente » Cod sursa (job #1824080) | Cod sursa (job #1732746) | Cod sursa (job #1791850) | Cod sursa (job #3319016) | Cod sursa (job #3357131)
#include <iostream>
#include <fstream>
#include <queue>
#include <vector>
using namespace std;
using PI = pair<int, int>;
using VI = vector<int>;
using VPI = vector<PI>;
using VVPI = vector<VPI>;
ifstream fin("distante.in");
ofstream fout("distante.out");
struct Cell {
int node, w;
Cell(int node = 0, int w = 0) : node {node}, w {w}
{}
bool operator < (Cell const& other) const noexcept
{
return w > other.w;
}
};
const int INF = 0x3f3f3f3f;
int T, n, m, p;
VI d, dOrig;
VVPI adj;
void ReadGraph();
void Solve();
void Dijkstra(int node, VI& d);
int main()
{
fin >> T;
while (T--)
Solve();
return 0;
}
void Solve()
{
ReadGraph();
Dijkstra(p, d);
for (int i = 1; i <= n; ++i)
if (dOrig[i] != d[i])
{
fout << "NU\n";
return;
}
fout << "DA\n";
}
void Dijkstra(int node, VI& d)
{
priority_queue<Cell> Q;
d = VI(n + 1, INF);
Q.emplace(node, 0);
d[node] = 0;
while (!Q.empty())
{
auto [x, w] = Q.top();
Q.pop();
if (w > d[x])
continue;
for (PI y : adj[x])
{
int currNode = y.first, currW = y.second;
if (d[currNode] > d[x] + currW)
{
d[currNode] = d[x] + currW;
Q.emplace(currNode, d[currNode]);
}
}
}
}
void ReadGraph()
{
fin >> n >> m >> p;
dOrig = VI(n + 1);
adj = VVPI(n + 1);
for (int i = 1; i <= n; ++i)
fin >> dOrig[i];
int x, y, w;
while (m--)
{
fin >> x >> y >> w;
adj[x].emplace_back(y, w);
adj[y].emplace_back(x, w);
}
}