Cod sursa(job #2103576)

Utilizator andreigasparoviciAndrei Gasparovici andreigasparovici Data 10 ianuarie 2018 15:14:13
Problema Distante Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.1 kb
#include <bits/stdc++.h>
using namespace std;

#define NMAX 50005
#define INFINIT 0x3f

int T, N, M, S;
int D[NMAX];
bool checked[NMAX];

struct Muchie {
  int x, y, c;
};

vector<Muchie> graf;

void citire() {
  scanf("%d %d %d ", &N, &M, &S);

  graf.clear();

  for (int i = 1; i <= N; i++) {
    scanf("%d ", &D[i]);
    checked[i] = false;
  }

  for (int i = 1; i <= M; i++) {
    int a, b, c;
    scanf("%d %d %d ", &a, &b, &c);
    graf.push_back({ a, b, c });
    graf.push_back({ b, a, c });
  }
}

bool verificare() {

  if (D[S]) return false;

  for (auto muchie: graf) {
    int x = muchie.x, y = muchie.y, c = muchie.c;

    if (D[x] > D[y] + c || D[y] > D[x] + c)
      return false;

    if (D[x] == D[y] + c)
      checked[x] = true;
    if (D[y] == D[x] + c)
      checked[x] = true;
  }

  for (int i = 1; i <= N; i++)
    if (!checked[i])
      return false;

  return true;
}

int main() {
  freopen("distante.in", "r", stdin);
  freopen("distante.out", "w", stdout);

  scanf("%d ", &T);

  while (T--) {
    citire();
    if(verificare())
      printf("DA\n");
    else printf("NU\n");
  }

  return 0;
}