Cod sursa(job #3320455)

Utilizator vvalentinCiun Valentin vvalentin Data 5 noiembrie 2025 21:21:50
Problema Distante Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.41 kb
#include <bits/stdc++.h>
#define ll long long
#define INF 0x3f3f3f3f3f3f3f3f
using namespace std;
const int NMAX = 5e4;

struct node {
  int nod;
  ll cost;
};

bool operator < (const node & a, const node & b) {
  return a.cost > b.cost;
}

void solve() {
  int n, m, a, b, c, s;
  cin >> n >> m >> s;
  vector<int> initial(n + 1);
  vector<ll> dist(n + 1, INF);
  vector<vector<node>> v(n + 1);
  for (int i = 1; i <= n; i++) cin >> initial[i];
  while (m--) {
    cin >> a >> b >> c;
    v[a].push_back({b, c});
  }
  priority_queue<node> pq;
  pq.push({s, 0});
  while (pq.size()) {
    while (pq.size() && dist[pq.top().nod] != INF) pq.pop();
    if (pq.size()) {  
      node a = pq.top();
      pq.pop();
      dist[a.nod] = a.cost;
      for (node i : v[a.nod]) {
        if (dist[i.nod] == INF) {
          pq.push({i.nod, i.cost + dist[a.nod]});
        }
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    if (dist[i] != initial[i]) {
      cout << "NU\n";
      return;
    }
  }
  cout << "DA\n";
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0); cout.tie(0);
  #ifndef ONLINE_JUDGE
  freopen("distante.in", "r", stdin);
  freopen("distante.out", "w", stdout);
  #endif
  /*
  freopen("D:\\cf2\\problema\\output.txt", "w", stdout);
  freopen("D:\\cf2\\problema\\input.txt", "r", stdin);
  */
  
  int tt; cin >> tt;
  while (tt--) solve();

  return 0;
}