Pagini recente » Cod sursa (job #1051398) | Cod sursa (job #2083119) | Cod sursa (job #17800) | Cod sursa (job #1553070) | Cod sursa (job #2971072)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("distante.in");
ofstream fout("distante.out");
const int DIM = 50010;
const int INF = 0x3f3f3f3f;
int h,n,m,p;
using PI = pair<int,int>;
vector <PI> v[DIM];
bool djikstra(int nod,int dist[],int co[])
{
priority_queue <PI,vector<PI>,greater<PI>>pq;
pq.emplace(make_pair(0,nod));
dist[nod] = 0;
while(!pq.empty())
{
int node = pq.top().second;
int cost = pq.top().first;
pq.pop();
//vis[node] = 0;
if(cost>dist[node])
continue;
for(auto x:v[node])
{
if(dist[x.first] > dist[node] + x.second)
{
dist[x.first] = dist[node]+x.second;
if(co[x.first]>dist[x.first])
return 0;
pq.emplace(make_pair(dist[x.first],x.first));
}
}
}
return 1;
}
int main()
{
fin>>h;
while(h--)
{
fin>>n>>m>>p;
for(int i=1;i<=n;i++)
v[i].clear();
int co[n+10],dist[n+10];
memset(dist,INF,sizeof dist);
for(int i=1;i<=n;i++)
fin>>co[i];
for(int i=1;i<=m;i++)
{
int x,y,z;
fin>>x>>y>>z;
v[x].push_back(make_pair(y,z));
v[y].push_back(make_pair(x,z));
}
if(co[p]!=0)
fout<<"NU\n";
else if(djikstra(p,dist,co)==true)
fout<<"DA\n";
else
fout<<"NU\n";
}
return 0;
}