Pagini recente » Cod sursa (job #274549) | Cod sursa (job #2597984) | Cod sursa (job #737556) | Cod sursa (job #2260246) | Cod sursa (job #2596087)
#include <iostream>
#include <fstream>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;
const int NMAX = 50005;
const int INF = 2e9 + 5;
ifstream fin("distante.in");
ofstream fout("distante.out");
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];
priority_queue<muchie>pq;
void dijkstra(int nod)
{
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()
{
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];
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;
}