Pagini recente » Cod sursa (job #2477695) | Cod sursa (job #1381459) | Cod sursa (job #1226106) | Cod sursa (job #1300795) | Cod sursa (job #2923333)
#include <iostream>
#include <fstream>
#include <queue>
#include <vector>
#include <climits>
using namespace std;
ifstream in ("distante.in");
ofstream out("distante.out");
vector <pair<int,int>> graf[50001];
priority_queue <pair< int,int>> h;
int v[50001],dist[50001];
bool inq[50001];
int main()
{
int T,n,m,s,x,y,cost;
in>>T;
for(int l=1; l<=T; l++)
{
in>>n>>m>>s;
graf[n]= {make_pair(0,0)};
for(int i=1; i<=n; i++)
{
in>>v[i];
dist[i]=INT_MAX;
}
for(int i=1; i<=m; i++)
{
in>>x>>y>>cost;
graf[x].push_back(make_pair(y,cost));
graf[y].push_back(make_pair(x,cost));
}
h.push(make_pair(s,0));
dist[s]=0;
inq[s]=1;
while(h.empty()==false)
{
x=h.top().first;
inq[x]=0;
h.pop();
for(unsigned int i=0; i<graf[x].size(); i++)
{
y=graf[x][i].first;
cost=graf[x][i].second;
if(dist[y]>dist[x]+cost)
{
dist[y]=dist[x]+cost;
if(inq[y]==0)
{
h.push(make_pair(y,-dist[y]));
inq[y]=1;
}
}
}
}
bool ok=1;
for(int i=1; i<=n && ok==1; i++)
{
if(dist[i]!=v[i])
ok=0;
}
if(ok==0)
out<<"NU"<<'\n';
else
out<<"DA"<<'\n';
}
return 0;
}