Pagini recente » Cod sursa (job #2607249) | Cod sursa (job #17481) | Cod sursa (job #839489) | Cod sursa (job #2205445) | Cod sursa (job #2059116)
#include <bits/stdc++.h>
using namespace std;
ifstream in("distante.in");
ofstream out("distante.out");
int t,n,m,start;
const int nx=50002;
int dist[nx];
int dcomp[nx];
struct muchie
{
int d;
int c;
};
int main()
{
for(in>>t;t;t--)
{
in>>n>>m>>start;
for(int i=1; i<=n; i++)
{
in>>dcomp[i];
dist[i]=INT_MAX;
}
vector < muchie > v[nx];
for(;m;m--)
{
int i,j,cost;
in>>i>>j>>cost;
v[i].push_back({j,cost});
v[j].push_back({i,cost});
}
queue < int > q;
q.push(start);
dist[start]=0;
while(!q.empty())
{
int i=q.front();
q.pop();
for(vector < muchie > :: iterator it=v[i].begin(); it!=v[i].end(); it++)
if(dist[it->d]>dist[i]+it->c)
{
dist[it->d]=dist[i]+it->c;
q.push(it->d);
}
}
bool ok=1;
for(int i=1; i<=n; i++)
if(dist[i]!=dcomp[i])
{
out<<"NU";
ok=0;
break;
}
if(ok) out<<"DA";
out<<'\n';
}
return 0;
}