Pagini recente » Cod sursa (job #1951932) | Cod sursa (job #556990) | Cod sursa (job #437881) | Cod sursa (job #472936) | Cod sursa (job #2841394)
#include <bits/stdc++.h>
#define NMAX 50005
using namespace std;
/*******************************/
// INPUT / OUTPUT
ifstream f("distante.in");
ofstream g("distante.out");
/*******************************/
/// GLOBAL DECLARATIONS
int T;
int N, M, S;
bool ok;
bool vis[NMAX];
int dist[NMAX], ans[NMAX];
struct Node
{
int node, dist;
bool operator < (const Node &other) const{
return dist > other.dist;
}
}top;
vector <Node> adj[NMAX];
/*******************************/
/// FUNCTIONS
void ReadInput();
void Solution();
void Output();
/*******************************/
///-------------------------------------
inline void ReadInput()
{
f >> T;
}
///-------------------------------------
void Read()
{
f >> N >> M >> S;
for (int i = 1 ; i <= N ; ++ i)
{
adj[i].clear();
}
memset(vis, 0, sizeof(vis));
for (int i = 1 ; i <= N ; ++ i)
{
f >> dist[i];
}
int a, b, c;
for (int i = 1 ; i <= M ; ++ i)
{
f >> a >> b >> c;
adj[a].push_back({b, c});
adj[b].push_back({a, c});
}
}
///-------------------------------------
void Dijkstra()
{
priority_queue <Node> pq;
pq.push({S, 0});
while (!pq.empty())
{
int node = pq.top().node;
int dist = pq.top().dist;
pq.pop();
if (!vis[node])
{
vis[node] = true;
ans[node] = dist;
for (auto u: adj[node])
{
if (!vis[u.node])
{
pq.push({u.node, dist + u.dist});
}
}
}
}
}
///-------------------------------------
void GetAns()
{
ok = true;
for (int i = 1 ; i <= N ; ++ i)
{
if (ans[i] != dist[i])
{
ok = false;
return;
}
}
}
///-------------------------------------
void Solve()
{
Dijkstra();
GetAns();
}
///-------------------------------------
void TestCase()
{
Read();
Solve();
Output();
}
///-------------------------------------
inline void Solution()
{
while (T --)
{
TestCase();
}
}
///-------------------------------------
inline void Output()
{
if (ok) g << "DA \n";
else g << "NU \n";
}
///-------------------------------------
int main()
{
ReadInput();
Solution();
return 0;
}