Cod sursa(job #2927867)

Utilizator raresgherasaRares Gherasa raresgherasa Data 21 octombrie 2022 18:25:02
Problema Distante Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.99 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin ("distante.in");
ofstream fout ("distante.out");

int main(){
  int ts; fin >> ts;
  while (ts--){
    int n, m, p; fin >> n >> m >> p;
    vector<pair<int, int>>g[n + 1];
    vector<int>v(n), d(n + 1, 1e9);
    for (int &x : v){
      fin >> x;
    }
    for (int i = 0; i < m; i++){
      int x, y, cost;
      fin >> x >> y >> cost;
      g[x].push_back({y, cost});
      g[y].push_back({x, cost});
    }
    set<pair<int, int>>s;
    s.insert({0, p});
    d[p] = 0;
    while (s.empty() == false){
      auto q = *s.begin();
      s.erase(s.begin());
      int nod = q.second;
      for (pair<int, int>u : g[nod]){
        if (d[u.first] > d[nod] + u.second){
          d[u.first] = d[nod] + u.second;
          s.insert({u.second, u.first});
        }
      }
    }
    bool ok = true;
    for (int i = 0; i < n; i++){
      ok &= (v[i] == d[i + 1]);
    }
    fout << (ok ? "DA" : "NU") << '\n';
  }
}