Cod sursa(job #3310222)

Utilizator amunnumeVlad Patrascu amunnume Data 12 septembrie 2025 10:23:02
Problema Distante Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.1 kb
#include <bits/stdc++.h>
using namespace std;
ifstream fin("distante.in");
ofstream fout("distante.out");
const int N=5e4+5,inf=2e9;
struct elem
{
  int nod,cost;
  bool operator<(const elem& e) const
  {
    return e.cost<cost;
  }
};
int C,n,m,s,i,j,x,y,cost,vrf[N],d[N];
vector<elem> e[N];
priority_queue<elem> q;
bool solve()
{
  fin>>n>>m>>s;
  for(i=1;i<=n;++i)
  {
    fin>>vrf[i];
    d[i]=inf;
  }
  for(i=1;i<=m;++i)
  {
    fin>>x>>y>>cost;
    e[x].push_back({y,cost});
    e[y].push_back({x,cost});
  }
  q.push({s,0});
  d[s]=0;
  while(!q.empty())
  {
    int nod=q.top().nod;
    int cost=q.top().cost;
    q.pop();
    for(auto j:e[nod])
    {
      if(d[j.nod]>d[nod]+j.cost)
      {
        d[j.nod]=d[nod]+j.cost;
        q.push(j);
      }
    }
  }
  bool ok=1;
  for(int i=1;i<=n;++i)
  {
    if(vrf[i]!=d[i])
    {
      ok=0; break;
    }
  }
  for(int i=1;i<=n;++i)
  {
    e[i].clear();
    d[i]=inf;
  }
  return ok;
}
int main()
{
  fin>>C;
  while(C--)
  {
    if(solve()) fout<<"DA\n";
    else fout<<"NU\n";
  }
  return 0;
}