Pagini recente » Cod sursa (job #2839769) | Cod sursa (job #2035114) | Cod sursa (job #2410614) | Cod sursa (job #675074) | Cod sursa (job #2731930)
#include <fstream>
#include <vector>
#include <queue>
#define INF 0x3f3f3f3f
using namespace std;
ifstream in("distante.in");
ofstream out("distante.out");
using PI = pair <int, int>;
const int N = 50001;
vector <PI> G[N];
int T, n, m, sursa, dist[N], D[N];
void citire()
{
in >> n >> m >> sursa;
for(int i = 1; i <= n; i++)
in >> dist[i];
for(int i = 0; i < m; i++)
{
int a, b, c;
in >> a >> b >> c;
G[a].push_back({b, c});
G[b].push_back({a, c});
}
}
void dijkstra(int nod)
{
priority_queue <PI, vector <PI>, greater <PI>> pq;
for(int i = 1; i <= n; i++) D[i] = INF;
D[nod] = 0;
pq.push({0, nod});
while(!pq.empty())
{
int x = pq.top().first, y = pq.top().second;
pq.pop();
if(x > D[y]) continue;
for(auto i: G[y])
{
int nod_nou = i.first, cost_nou = i.second;
if(D[nod_nou] > D[y] + cost_nou)
{
D[nod_nou] = D[y] + cost_nou;
pq.push({D[nod_nou], nod_nou});
}
}
}
}
bool comp()
{
for(int i = 1; i <= n; i++)
if(D[i] != dist[i]) return false;
return true;
}
int main()
{
in >> T;
while(T--)
{
citire();
dijkstra(sursa);
if(comp())
out << "DA\n";
else
out << "NU\n";
}
return 0;
}