Pagini recente » Cod sursa (job #1455128) | Cod sursa (job #1227637) | Cod sursa (job #3135933) | Cod sursa (job #769339) | Cod sursa (job #2464348)
#include <fstream>
#include <vector>
#include <set>
#include <tuple>
#define mp make_pair
using namespace std;
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 < pair <int, int> > 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 < pair <int, int> > pq;
pq.insert(mp(0, start));
dist[start] = 0;
bool good = true;
while (!pq.empty())
{
int node, cost;
tie(cost, node) = *pq.begin();
pq.erase(*pq.begin());
if (dist[node] != a[node])
{
good = false;
break;
}
for (auto &x : graph[node])
{
if (dist[x.first] > dist[node] + x.second)
{
pair <int, int> next;
next.first = dist[x.first];
next.second = x.first;
pq.erase(next);
next.first = dist[node] + x.second;
dist[x.first] = dist[node] + x.second;
pq.insert(next);
}
}
}
dist[0] = 0;
fout << ((good == true) ? "DA\n" : "NU\n");
}
fin.close();
fout.close();
return 0;
}