Cod sursa(job #2797009)

Utilizator YusyBossFares Yusuf YusyBoss Data 9 noiembrie 2021 09:53:15
Problema Distante Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.76 kb
#include <fstream>
#include <queue>
#include <vector>

#define NMAX 50000

using namespace std;

ifstream cin ("distante.in");
ofstream cout ("distante.out");

struct hatz {
  int node;
  int cost;
  bool operator < (const hatz& A) const {
    return cost > A.cost;
  }
};

int vdist[NMAX + 1], vd[NMAX + 1];
priority_queue <hatz> pq;
vector <hatz> vnext[NMAX + 1];

bool bfs(int source_node) {
  int n, i;
  hatz hatz_crt, hatz_new;

  while (!pq.empty())
    pq.pop();
  pq.push({source_node, 0});
  while (!pq.empty()) {
    hatz_crt = pq.top();
    pq.pop();

    if (vdist[hatz_crt.node] == -1) {
      vdist[hatz_crt.node] = hatz_crt.cost;

      if (vd[hatz_crt.node] != vdist[hatz_crt.node])
        return 0;

      n = vnext[hatz_crt.node].size();
      for (i = 0; i < n; ++i) {
        hatz_new = vnext[hatz_crt.node][i];
        if (vdist[hatz_new.node] == -1)
          pq.push({hatz_new.node, hatz_crt.cost + hatz_new.cost});
      }
    }
  }

  return 1;
}

int getnr() {
  int nr;
  char ch;

  ch = cin.get();
  while (!isdigit(ch))
    ch = cin.get();

  nr = 0;
  while (isdigit(ch)) {
    nr = nr * 10 + ch - '0';
    ch = cin.get();
  }

  return nr;
}

int main() {
  int q, n, m, source_node, i, a, b, c, val;
  cin >> q;

  while (q--) {
    n = getnr();
    m = getnr();
    source_node = getnr();
    for (i = 1; i <= n; i++) {
      vd[i] = getnr();
      vdist[i] = -1;
      vnext[i].clear();
    }

    for (i = 0; i < m; i++) {
      a = getnr();
      b = getnr();
      c = getnr();
      vnext[a].push_back({b, c});
      vnext[b].push_back({a, c});
    }

    val = bfs(source_node);
    if (!val)
      cout << "NU\n";
    else
      cout << "DA\n";
  }
  return 0;
}