Pagini recente » Monitorul de evaluare | Borderou de evaluare (job #3347961) | Borderou de evaluare (job #3339542) | Diferente pentru problema/map intre reviziile 18 si 9 | Cod sursa (job #3319210)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("distante.in");
ofstream fout("distante.out");
#define cin fin
#define cout fout
struct el {
long long nod, cost;
bool operator <(const el &other) const {
return cost > other.cost;
}
};
vector<vector<el>> a;
priority_queue<el> pq;
vector<long long> dist;
void dijkstra(int start) {
dist[start] = 0;
pq.push({start, 0});
while(!pq.empty()) {
el curr = pq.top();
pq.pop();
long long u = curr.nod;
long long u_cost = curr.cost;
if(u_cost > dist[u]) continue;
for(auto e : a[u]) {
long long v = e.nod;
long long v_cost = e.cost;
if(u_cost + v_cost < dist[v]) {
dist[v] = u_cost + v_cost;
pq.push({v, dist[v]});
}
}
}
}
int main()
{
int t;
cin >> t;
for(int k = 1; k <= t; k++) {
int n, m, s, aux;
cin >> n >> m >> s;
vector<long long> test(n + 1);
for(int i = 1; i <= n; i++) cin >> aux, test[i] = aux;
a.clear(), a.shrink_to_fit();
a.resize(n + 1);
for(int i = 1; i <= m; i++) {
long long x, y, c;
cin >> x >> y >> c;
a[x].push_back({y, c});
a[y].push_back({x, c});
}
dist.clear(), dist.shrink_to_fit();
dist.assign(n + 1, 1e9);
dist[0] = 0;
dijkstra(s);
cout << (test == dist ? "DA\n" : "NU\n");
}
}