Pagini recente » Cod sursa (job #2868025) | Cod sursa (job #1810093) | Cod sursa (job #836488) | Cod sursa (job #2629021) | Cod sursa (job #886090)
Cod sursa(job #886090)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream in("distante.in");
ofstream out("distante.out");
struct cost
{
int vf,c;
};
const int N=50001,INF=500000000;
vector <cost> a[N];
queue <int> q;
int n,m,s,t,d[N],d1[N];
bool inq[N];
void citire()
{
int x,y,c;
cost aux;
in>>n>>m>>s;
for(int i=1;i<=n;i++)
in>>d1[i];
for(int i=1;i<=m;i++)
{
in>>x>>y>>c;
aux.vf=y;
aux.c=c;
a[x].push_back(aux);
aux.vf=x;
a[y].push_back(aux);
}
}
bool bfs(int x)
{
int y;
q.push(x);
while(!q.empty())
{
x=q.front();
q.pop();
inq[x]=false;
for(size_t i=0;i<a[x].size();i++)
{
y=a[x][i].vf;
if(d[y]>d[x]+a[x][i].c)
{
d[y]=d[x]+a[x][i].c;
if(!inq[y])
{
q.push(y);
inq[y]=true;
}
}
if(d[y]!=d1[y])
return false;
}
}
return true;
}
int main()
{
in>>t;
for(int i=1;i<=t;i++)
{
citire();
for(int j=1;j<=n;j++)
d[j]=INF;
d[s]=0;
if(bfs(s))
out<<"DA"<<'\n';
else
out<<"NU"<<'\n';
}
return 0;
}