Pagini recente » Monitorul de evaluare | Cod sursa (job #2960238) | Cod sursa (job #1114841) | Cod sursa (job #3281889) | Cod sursa (job #2464540)
#include <fstream>
#include <vector>
#include <queue>
#include <bitset>
using namespace std;
bitset <50005> inq;
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, 0);
vector < pair <int, int> > graph[nodes + 1];
for (int i = 1;i <= nodes;++i)
{
fin >> a[i];
dist[i] = (1 << 30);
}
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});
}
dist[start] = 0;
inq[start] = 1;
queue <int> q;
q.push(start);
bool good = true;
while (!q.empty())
{
int node = q.front();
q.pop();
inq[node] = 0;
if (a[node] != dist[node])
{
good = false;
break;
}
for (auto &x : graph[node])
{
if (dist[x.first] > dist[node] + x.second)
{
dist[x.first] = dist[node] + x.second;
if (inq[x.first] == 0)
{
inq[x.first] = 1;
q.push(x.first);
}
}
}
}
fout << ((good == true) ? "DA\n" : "NU\n");
}
fin.close();
fout.close();
return 0;
}