Pagini recente » Cod sursa (job #1609609) | Cod sursa (job #2962362) | Cod sursa (job #2582937) | Cod sursa (job #2838095) | Cod sursa (job #2684378)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream fin("distante.in");
ofstream fout("distante.out");
int n, m, t, s;
const int inf = (1 << 30);
const int nMax = 40000;
vector <pair <int, int>> v[50000];
priority_queue <pair <int, int>> q;
bool viz[nMax];
int dist[nMax];
int distb[nMax];
void Dijkstra(int nod)
{
for (int i = 1; i <= n; i++)
dist[i] = inf;
dist[nod] = 0;
q.push({ 0, nod });
while (!q.empty())
{
int nod = q.top().second;
int cost = -q.top().first;
q.pop();
if (!viz[nod])
{
viz[nod] = 1;
for (auto it : v[nod])
{
int newNod = it.first;
int newCost = it.second;
if (dist[newNod] > dist[nod] + newCost)
{
dist[newNod] = dist[nod] + newCost;
q.push({ -dist[newNod],newNod });
}
}
}
}
}
int Verificare(int a[], int b[])
{
for (int i = 1; i <= n; i++)
if (a[i] != b[i]) return 0;
return 1;
}
void Citire()
{
int x, y, d;
fin >> t;
while (t--)
{
fin >> n >> m >> s;
for (int i = 1; i <= n; i++)
fin >> distb[i];
for (int i = 1; i <= m; i++)
{
fin >> x >> y >> d;
v[x].push_back({ y,d });
v[y].push_back({ x,d });
}
Dijkstra(s);
if (Verificare(dist,distb))fout << "DA";
else fout << "NU";
fout << "\n";
v[nMax].clear();
}
}
int main()
{
Citire();
return 0;
}