Pagini recente » Cod sursa (job #897883) | Cod sursa (job #1145869) | Cod sursa (job #1281238) | Cod sursa (job #2233751) | Cod sursa (job #2830643)
#include <bits/stdc++.h>
#include <limits>
using namespace std;
int infinit = std::numeric_limits<int>::max();
ifstream in("distante.in");
ofstream out("distante.out");
void Dijkstra(int& nr_N, int& nr_M, int& S, vector<int>& comp, vector<int>& dist,vector<vector<pair<int,int>>>& costuri)
{
int V[100001] = {};
for(int i = 1; i <= nr_N; i++)
dist[i] = infinit;
dist[S] = 0;
priority_queue<pair<int,int>> H; /// Va sorta singur in functie de cea mai mica distanta
H.push({0,S});
while(H.size() > 0)
{
int u = H.top().second;
H.pop();
if(V[u])
continue;
else
V[u]++;
for(auto v : costuri[u]) /// Verificam toti vecinii lui u
{
if(dist[v.first] > dist[u] + v.second)
{
dist[v.first] = dist[u] + v.second;
H.push({-dist[v.first], v.first}); /// Folosim -dist[v.first] ca sa avem in top cea mai mica valoare
}
}
}
}
int main()
{
int t, nr_N, nr_M, S;
in >> t;
for(int j = 1; j <= t; j++)
{
in >> nr_N >> nr_M >> S;
vector<int> comp(nr_N + 1, 0), dist(nr_N + 1, infinit);
for(int i = 1; i <= nr_N; i++)
{
int x;
in >> x;
comp[i] = x;
}
vector<vector<pair<int,int>>> costuri(nr_N + 1);
for(int i = 1; i <= nr_M; i++)
{
int x, y, c;
in >> x >> y >> c;
costuri[x].push_back({y,c}); /// Muchia de la x plecand in y are costul c
costuri[y].push_back({x,c});
}
Dijkstra(nr_N, nr_M, S, comp, dist, costuri);
int ok = 1;
for(int i = 1; i <= nr_N; i++)
{
if(dist[i] != comp[i])
{
ok = 0;
break;
}
}
if(ok)
out << "DA" << '\n';
else
out << "NU" << '\n';
}
return 0;
}