Cod sursa(job #3195391)

Utilizator oanantoniaSut Oana oanantonia Data 20 ianuarie 2024 18:03:28
Problema Distante Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.4 kb
#include <bits/stdc++.h>

using namespace std;

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

struct per{
       int nod, cost;
       bool operator<(const per& other) const {
        return (*this).cost > other.cost;
    }
};

int j, k, w, x, y, l, s, n, m, cost[105], d[105];
vector<per> v[105];
priority_queue<per> q;

void dj( int st ){
     for ( int i = 1; i <= n; i++ ) cost[i] = 1e9;
     cost[st] = 0;
     q.push({st, 0});
     while ( !q.empty() ){
             per x = q.top();
             q.pop();
             if ( x.cost != cost[x.nod] ) continue;
             for ( auto el:v[x.nod] ){
                   if ( cost[el.nod] > x.cost + el.cost ){
                        cost[el.nod] = x.cost + el.cost;
                        q.push({el.nod, x.cost + el.cost});
                   }
             }
     }
}

int main()
{
    fin >> w;
    for ( int i = 1; i <= w; i++ ){
          fin >> n >> m >> s;
          for ( j = 1; j <= n; j++ ) fin >> d[j];
          for ( j = 1; j <= m; j++ ){
                fin >> x >> y >> l;
                v[x].push_back({y, l});
                v[y].push_back({x, l});
          }
          dj(s);
          k = 1;
          for ( j = 1; j <= n; j++ ){
                if ( d[j] != cost[j] ) k = 0;
          }
          if( k == 0 ) fout << "NU";
          else fout << "DA";
          fout << endl;
    }
}