Cod sursa(job #2134199)

Utilizator raluca1234Tudor Raluca raluca1234 Data 17 februarie 2018 18:37:56
Problema Distante Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.59 kb
#include <bits/stdc++.h>

using namespace std;

const int maxN = 5e4 + 5, maxM = 1e5 + 5, INF = 0x3f3f3f3f;

int D[maxN], dist[maxN];

struct Edge{
  int node, cost;
};
vector <Edge> G[maxN];

struct PQNode{
  int node, cost;
  bool operator < (const PQNode &aux) const{
    return cost > aux.cost;
  }
};
priority_queue <PQNode> heap;

void dijkstra(int start){
  heap.push({start, 0});

  while (!heap.empty()){
    int node = heap.top().node, cost = heap.top().cost;

    heap.pop();

    if (dist[node] < cost)
      continue;

    for (int i = 0; i < G[node].size(); i++){
      int newNode = G[node][i].node, newCost = G[node][i].cost;
      if (dist[newNode] > cost + newCost){
        dist[newNode] = cost + newCost;
        heap.push({newNode, dist[newNode]});
      }
    }
  }
}

bool check(int N){
  for (int i = 1; i <= N; i++)
    if (D[i] != dist[i])
      return false;
  return true;
}

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

  int T;
  scanf("%d", &T);

  int N, M, S;
  for (int q = 0; q < T; q++){
    scanf("%d %d %d", &N, &M, &S);

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

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

    memset(dist, INF, sizeof(dist));
    dist[S] = 0;

    dijkstra(S);

    if (check(N) == true)
      printf("DA\n");
    else
      printf("NU\n");

    for (int i = 1; i <=N; i++)
      G[i].clear();
  }

  return 0;
}