Pagini recente » Cod sursa (job #620598) | Cod sursa (job #2467390) | Cod sursa (job #619925) | Cod sursa (job #2464908) | Cod sursa (job #3222748)
#include <fstream>
#include <queue>
using namespace std;
int dist[50005], best[50005], f[50005];
vector <int> cost[50005], v[50005];
priority_queue <pair<int, int > > pq;
pair <int, int> pa, ca;
int main()
{
ifstream cin("distante.in");
ofstream cout("distante.out");
int t, n, m, s, a, b, c, ok=0;
cin>>t;
for(int k=0;k<t;k++)
{
cin>>n>>m>>s;
for(int i=1;i<=n;i++)
cin>>dist[i];
for(int i=0;i<m;i++)
{
cin>>a>>b>>c;
cost[a].push_back(c);
cost[b].push_back(c);
v[a].push_back(b);
v[b].push_back(a);
best[a]=1;
best[b]=1;
f[a]=0;
f[b]=0;
}
pa.first=0;
pa.second=s;
best[s]=0;
pq.push(pa);
while(!pq.empty())
{
pa=pq.top();
pq.pop();
if(f[pa.second]==0)
{
for(int i=0;i<v[pa.second].size();i++)
{
ca.first=cost[pa.second][i]*(-1)+best[pa.second];
ca.second=v[pa.second][i];
if(best[ca.second]==1)
{
best[ca.second]=ca.first;
pq.push(ca);
}
else if(best[ca.second]<ca.first)
{
best[ca.second]=ca.first;
pq.push(ca);
}
}
f[pa.second]=1;
}
}
for(int i=1;i<=n;i++)
{
if(best[i]*(-1)!=dist[i])
{
ok=1;
break;
}
}
if(ok==1)
cout<<"NU"<<'\n';
else
cout<<"DA"<<'\n';
ok=0;
}
return 0;
}