Cod sursa(job #2801002)

Utilizator teochess2017Togan Teodor-Bogdan teochess2017 Data 14 noiembrie 2021 17:29:27
Problema Distante Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.12 kb
#include <iostream>
#include <queue>
#include <vector>
#include <stdio.h>
#include <ctype.h>

using namespace std;

#define MAXN 50000
#define MAXM 100000
#define MAXVAL 1000
#define INF (MAXN * MAXVAL + 1)

int best[MAXN], v[MAXN];

struct nod{

  int node, cost;
  bool operator < (const nod &other) const{
      return cost > other.cost;
  }
};

priority_queue <nod> pq;
vector <nod>arb[MAXN];

FILE *fin, *fout;

int readInt(){
  int n;
  char ch;
  while(!isdigit(ch = fgetc(fin)));
  n = ch - '0';
  while(isdigit(ch = fgetc(fin))){
    n = n * 10 + ch - '0';
  }
  return n;
}
void dijkstra(int s){
  int i;
  nod nodcoada;
  pq.push({s, 0});
  while(0 < pq.size()){
    nodcoada = pq.top();
    pq.pop();
    if(best[nodcoada.node] == INF){
      best[nodcoada.node] = nodcoada.cost;
      for(i = 0; i < arb[nodcoada.node].size(); i++){
        if(best[arb[nodcoada.node][i].node] == INF){
          pq.push({arb[nodcoada.node][i].node, best[nodcoada.node] + arb[nodcoada.node][i].cost});
        }
      }
    }
  }
}
void reset(int n){
  int i;
  for(i = 0; i < n; i++){
    while(0 < arb[i].size()){
      arb[i].pop_back();
    }
  }
  while(0 < pq.size()){
    pq.pop();
  }
}

int main()
{
    int n, m, a, b, i, cost, t, j, st, s;
    fin = fopen("distante.in", "r");
    t = readInt();
    fout = fopen("distante.out", "w");
    for(j = 0; j < t; j++){
      n = readInt();
      m = readInt();
      s = readInt();
      for(i = 0; i < n; i++){
        v[i] = readInt();
      }
      for(i = 0; i < m; i++){
        a = readInt();
        b = readInt();
        cost = readInt();
        arb[a - 1].push_back({b - 1, cost});
        arb[b - 1].push_back({a - 1, cost});
      }
      for(i = 0; i < n; i++){
        best[i] = INF;
      }
      dijkstra(s - 1);
      st = 0;
      for(i = 0; i < n; i++){
        if(best[i] != v[i]){
          st = 1;
        }
      }
      if(st == 0){
        fprintf(fout, "DA\n");
      }else{
        fprintf(fout, "NU\n");
      }
      reset(n);
    }
    fclose(fin);
    fclose(fout);
    return 0;
}