Pagini recente » Cod sursa (job #248968) | Cod sursa (job #1180848) | Cod sursa (job #296492) | Cod sursa (job #2001132) | Cod sursa (job #2596095)
#include <algorithm>
#include <queue>
#include <vector>
#include <cstdio>
using namespace std;
const int NMAX = 50005;
const int INF = 2e9 + 5;
int m, n, s, x, a, b, cost;
int dist[NMAX];
int desired[NMAX];
struct node {
int v, cost;
};
struct muchie {
int v, cost;
bool operator<(const muchie& other) const {
return cost > other.cost;
}
};
vector<node>G[NMAX];
inline void dijkstra(int nod)
{
priority_queue<muchie>pq;
for (int i = 1; i <= n; i++)
dist[i] = INF;
muchie temp;
temp.v = nod;
temp.cost = 0;
pq.push(temp);
while (!pq.empty())
{
temp = pq.top();
pq.pop();
if (dist[temp.v] != INF) continue;
dist[temp.v] = temp.cost;
for (int i = 0; i < G[temp.v].size(); i++)
{
int nnod = G[temp.v][i].v;
int cost = G[temp.v][i].cost;
muchie temp1;
temp1.v = nnod;
temp1.cost = temp.cost + cost;
pq.push(temp1);
}
}
for (int i = 1; i <= n; i++)
if (dist[i] == INF)
dist[i] = 0;
}
int main()
{
freopen("distante.in", "r", stdin);
freopen("distante.out", "w", stdout);
int t;
scanf("%d", &t);
while (t--)
{
scanf("%d%d%d", &n, &m, &s);
for (int i = 1; i <= n; i++) G[i].clear();
for (int i = 1; i <= n; i++)
scanf("%d", &desired[i]);
while (m--)
{
scanf("%d%d%d", &a, &b, &cost);
node temp;
temp.v = b;
temp.cost = cost;
G[a].push_back(temp);
temp.v = a;
G[b].push_back(temp);
}
dijkstra(s);
bool ok = true;
for(int i = 1 ; i <= n ; i++)
if (dist[i] != desired[i])
{
ok = false;
break;
}
if (ok)
printf("DA\n");
else
printf("NU\n");
}
return 0;
}