Pagini recente » Cod sursa (job #323402) | Clasamentul arhivei de probleme | Borderou de evaluare (job #2637112) | Cod sursa (job #1982411) | Cod sursa (job #3238423)
#include <fstream>
#include <vector>
#include <queue>
#include <iostream>
#include <string>
using namespace std;
ifstream fin("distante.in");
ofstream fout("distante.out");
int mod[50004];
vector<pair<int, int>>L[50004];
int dist[50004];
int n, m;
priority_queue <pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
void djikstra(int nod)
{
for (int i = 1; i <= n; i++)
dist[i] = 2e9;
dist[nod] = 0;
pq.push({ 0,nod });
while (!pq.empty())
{
pair<int, int> curr = pq.top();
pq.pop();
if (dist[curr.second] < curr.first)
continue;
for (auto next : L[curr.second])
if (dist[next.first] > curr.first + next.second)
{
dist[next.first] = curr.first + next.second;
pq.push({ dist[next.first], next.first });
}
}
}
int main()
{
int q;
fin >> q;
while (q--)
{
int start;
fin >> n >> m >> start;
for (int i = 1; i <= n; i++)
fin >> mod[i];
for (int i = 1; i <= m; i++)
{
int x, y, c;
fin >> x >> y >> c;
L[x].push_back({ y,c });
L[y].push_back({ x,c });
}
djikstra(start);
bool ok = 1;
for (int i = 1; i <= n; i++)
if (dist[i] != mod[i])
ok = false;
if (ok)
fout << "DA\n";
else
fout << "NU\n";
}
return 0;
}