Pagini recente » Cod sursa (job #2607331) | Cod sursa (job #2709812)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("distante.in");
ofstream fout("distante.out");
const int Inf=(1<<30);
const int Nmax=50001;
using PI=pair<int, int>;
int v[Nmax];
int n, m, s, x, y, w, t;
int D[Nmax];
vector<PI>G[Nmax];
void dijkstra(int nod)
{
priority_queue< PI, vector<PI>, greater<PI>>Q;
D[nod]=0;
Q.push({0, nod});
while(!Q.empty())
{
x=Q.top().first;
y=Q.top().second;
Q.pop();
if(x>D[y])continue;
for(auto& q:G[y])
{
int nodnou=q.first;
int costnou=q.second;
if(D[nodnou]>D[y]+costnou)
{
D[nodnou]=D[y]+costnou;
Q.push({D[nodnou], nodnou});
}
}
}
}
int main()
{
fin>>t;
while(t--)
{
fin>>n>>m>>s;
for(int i=1; i<=n; i++)
fin>>v[i];
while(m--)
{
fin>>x>>y>>w;
G[x].push_back({y, w});
G[y].push_back({x, w});
}
for(int i=1; i<=n; i++)
{
D[i]=Inf;
}
dijkstra(s);
bool ok=true;
for(int i=1; i<=n; i++)
{
if(D[i]!=v[i])
{
ok=false;
}
}
if(ok)
fout<<"DA";
else
fout<<"NU";
fout<<'\n';
}
}