Cod sursa(job #1542618)

Utilizator tudorcomanTudor Coman tudorcoman Data 5 decembrie 2015 15:15:28
Problema Distante Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.27 kb

#include <fstream>
#include <vector>
#include <queue>

using namespace std;
typedef pair<int, int> muchie;

const int maxN = 50000;
const int infi = 1 << 30;
int v[maxN + 1], d[maxN + 1];
int T, N, M, S;
vector<muchie> G[maxN + 1];

bool dijkstra() {
  priority_queue<muchie, vector<muchie>, greater<muchie>> H;
  for(int i = 1; i <= N; ++ i)
    d[i] = infi;
  d[S] = 0;
  H.push({0, S});
  while(!H.empty()) {
    muchie x = H.top(); H.pop();
    int l = x.first, nod = x.second;
    if(d[nod] == l) {
      for(auto it: G[nod]) {
        if(d[nod] + it.second < d[it.first]) {
          d[it.first] = d[nod] + it.second;
          H.push({d[it.first], it.first});
        }
      }
    }
  }

  for(int i = 1; i <= N; ++ i)
    if(d[i] == infi)
      d[i] = 0;
  for(int i = 1; i <= N; ++ i)
    if(d[i] != v[i])
      return false;
  return true;
}

int main() {
  ifstream in("distante.in");
  ofstream out("distante.out");
  for(in >> T; T; -- T) {
    in >> N >> M >> S;
    for(int i = 1; i <= N; ++ i)
      in >> v[i];
    for(int i = 1; i <= M; ++ i) {
      int a, b, c;
      in >> a >> b >> c;
      G[a].push_back({b, c});
    }
    out << (dijkstra() ? "DA" : "NU") << '\n';
  }
  in.close();
  out.close();
  return 0;
}