Cod sursa(job #3207741)

Utilizator vlad_dimuVlad Dimulescu vlad_dimu Data 26 februarie 2024 20:22:02
Problema Distante Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.46 kb
#include <bits/stdc++.h>
#define INF 1 << 30
#define MAXN 50000
using namespace std;

ifstream fin( "distante.in" );
ofstream fout( "distante.out" );

struct nod {
  int v, c;
};

vector <nod> graph[MAXN + 1];
int d[MAXN + 1];
int marked[MAXN + 1];
int v[MAXN + 1];

class Compare {
public:
  bool operator()(nod a, nod b) {
    return a.c > b.c;
  }
};

priority_queue <nod, vector<nod>, Compare> pq;

void dijkstra( int s ) {
  d[s] = 0;
  pq.push( {s, 0} );

  while( !pq.empty() ) {
    int u = pq.top().v;
    pq.pop();
    marked[u] = 1;
    for( int i = 0; i < graph[u].size(); i++ ) {
      int vecin, cost;
      vecin = graph[u][i].v, cost = graph[u][i].c;
      if( marked[vecin] == 0 && d[vecin] > d[u] + cost ) {
        d[vecin] = d[u] + cost;
        pq.push( {vecin, d[vecin]} );
      }
    }
  }
}

int main() {
  int T, N, M, s, i, x, y, c;
  bool ok;
  fin >> T;
  while( T-- ) {
    fin >> N >> M >> s;

    for( i = 1; i <= N; i++ )
      fin >> v[i];

    for( i = 1; i <= M; i++ ) {
      fin >> x >> y >> c;
      graph[x].push_back( {y, c} );
      graph[y].push_back( {x, c} );
    }
    for( i = 1; i <= N; i++ )
      d[i] = INF, marked[i] = 0;
    dijkstra( s );
    ok = true;
    for( i = 1; i <= N; i++ )
      if( v[i] != d[i] )
        ok = false;

    if( ok == false )
      fout << "NU\n";
    else
      fout << "DA\n";

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