Pagini recente » Cod sursa (job #2490817) | Cod sursa (job #3176266) | Cod sursa (job #1959235) | Cod sursa (job #891272) | Cod sursa (job #3221458)
//SIMULARE ONI ANDREI distante --
#include <bits/stdc++.h>
using namespace std;
const int nmax = 50005, inf = 1e9 + 7;
ifstream fin("distante.in");
ofstream fout("distante.out");
int t, n, m, s, v[nmax], dist[nmax], vis[nmax], ok;
int x, y, z;
typedef struct poz{
int nod, cost;
}student;
vector < student > adj[nmax];
struct coord{
int node, cost;
const bool operator <(const coord &other)const{
return cost > other.cost;
}
};
priority_queue < coord > pq;
int main()
{
fin>>t;
while(t){
ok = 0;
fin>>n>>m>>s;
for(int i=1;i<=n;i++){
fin>>v[i];
}
for(int i=1;i<=m;i++){
fin>>x>>y>>z;
adj[x].push_back({y , z});
adj[y].push_back({x , z});
}
//Dijkstra
for(int i=1;i<=n;i++){
dist[i] = inf;
vis[i] = 0;
}
dist[s] = 0;
pq.push({s , 0});
while(!pq.empty()){
int node = pq.top().node , pret = pq.top().cost;
pq.pop();
if(vis[node] == 1) continue;
vis[node] = 1;
for(auto u : adj[node]){
if(vis[u.nod] == 0 && dist[u.nod] > dist[node] + u.cost){
dist[u.nod] = dist[node] + u.cost;
pq.push({u.nod , dist[u.nod]});
}
}
}
for(int i=1;i<=n;i++){
if(v[i] != dist[i]){
ok = 1;
break;
}
}
if(ok == 1){
fout<<"NU"<<"\n";
}else{
fout<<"DA"<<"\n";
}
t --;
}
}