Pagini recente » Cod sursa (job #2352902) | Cod sursa (job #1478017) | Cod sursa (job #3287195) | Cod sursa (job #3165882) | Cod sursa (job #2596081)
#include <iostream>
#include <fstream>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;
const int NMAX = 50005;
ifstream fin("distante.in");
ofstream fout("distante.out");
int m, n, s, x, a, b, cost;
int dist[NMAX];
int desired[NMAX];
bool viz[NMAX];
struct node {
int v, cost;
};
struct muchie {
int v, cost;
muchie() {}
muchie(int x, int cst)
{
v = x;
cst = cost;
}
bool operator<(const muchie& other) const {
return cost > other.cost;
}
};
vector<node>G[NMAX];
priority_queue<muchie>pq;
void dijkstra(int nod)
{
muchie temp(nod,0);
viz[nod] = 1;
pq.push(temp);
while (!pq.empty())
{
temp = pq.top();
pq.pop();
for (int i = 0; i < G[temp.v].size(); i++)
{
int nnod = G[temp.v][i].v;
int cost = G[temp.v][i].cost;
if (!viz[nnod])
{
viz[nnod] = true;
dist[nnod] = dist[temp.v] + cost;
muchie temp1(nnod, temp.cost + cost);
pq.push(temp1);
}
}
}
}
int main()
{
int t;
fin >> t;
while (t--)
{
fin >> n >> m >> s;
while (!pq.empty())pq.pop();
for (int i = 1; i <= n; i++) G[i].clear();
for (int i = 1; i <= n; i++)
{
fin >> desired[i];
dist[i] = 0;
viz[i] = 0;
}
while (m--)
{
fin >> 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)
fout << "DA\n";
else
fout << "NU\n";
}
return 0;
}