Cod sursa(job #1542644)

Utilizator tudorcomanTudor Coman tudorcoman Data 5 decembrie 2015 15:42:48
Problema Algoritmul lui Dijkstra Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1 kb

#include <fstream>
#include <vector>
using namespace std;

struct muchie {
  int s, d, c;
};

const int maxN = 50000;
const int maxM = 100000;
const int infi = 1 << 30;
muchie G[(maxM << 1) + 1];
int v[maxN + 1], T, N, M, S;

bool query() {
  if(v[S])
    return false;
  vector<bool> b(N + 1, false);
  b[S] = true;
  for(int i = 1; i <= (M << 1); ++ i) {
    if(v[G[i].s] + G[i].c == v[G[i].d])
      b[G[i].d] = true;
    if(v[G[i].s] + G[i].c < v[G[i].d])
      return false;
  }
  for(int i = 1; i <= N; ++ i)
    if(!b[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[i] = {a, b, c};
      G[i + M] = {b, a, c};
    }
    out << (query() ? "DA" : "NU") << '\n';
  }
  in.close();
  out.close();
  return 0;
}