Pagini recente » Cod sursa (job #2393918) | Cod sursa (job #2542821) | Cod sursa (job #1861011) | Cod sursa (job #939777) | Cod sursa (job #2411256)
#include <bits/stdc++.h>
#define INF 1000000005
using namespace std;
int n, m, s;
const int nmax=50005;
vector <pair < int, int > > g[nmax];
priority_queue < pair <int, int>, vector < pair <int, int> >, greater < pair <int, int> > > heap;
int dist_date[nmax];
int d[nmax];
void cle()
{
for(int i=1;i<=n;i++)
while(g[i].size())
g[i].pop_back();
}
void citire()
{
scanf("%d%d%d", &n, &m, &s);
for(int i=1;i<=n;i++)
scanf("%d", &dist_date[i]);
for(int i=1;i<=m;i++)
{
int x, y, c;
scanf("%d%d%d", &x, &y, &c);
g[x].push_back({y, c});
g[y].push_back({x, c});
}
}
void dijkstra(int start_node)
{
bool mod=false;
for(int i=1;i<=n;i++)
d[i]=INF;
d[start_node]=0;
heap.push({d[start_node], start_node});
while(heap.size())
{
int nod=heap.top().second;
int dist=heap.top().first;
if(d[nod]!=dist_date[nod])
{
printf("NU\n");
mod=true;
break;
}
heap.pop();
if(dist<=d[nod])
{
for(int i=0;i<g[nod].size();i++)
{
int curent_nod=g[nod][i].first;
int curent_dist=g[nod][i].second;
if(d[curent_nod]>curent_dist+dist)
{
d[curent_nod]=dist+curent_dist;
heap.push({d[curent_nod], curent_nod});
}
}
}
}
if(mod==false)
printf("DA\n");
}
int main()
{
freopen("distante.in", "r", stdin);
freopen("distante.out", "w", stdout);
int t;
scanf("%d", &t);
while(t)
{
citire();
dijkstra(s);
cle();
t--;
}
return 0;
}