Pagini recente » Cod sursa (job #1055666) | Cod sursa (job #3267748) | Cod sursa (job #1246739) | Cod sursa (job #710921) | Cod sursa (job #1061320)
#include <iostream>
#include <fstream>
#include <queue>
#include <vector>
using namespace std;
#define MAX 50010
FILE *fin=fopen("distante.in", "r");
ofstream fout("distante.out");
vector <pair<int, int> > G[MAX];
queue <int > coada;
int viz[MAX], n, m, t, a, s, b, c, i, rez[MAX], j;
unsigned const maxb=8192;
char buf[maxb];
unsigned ptr=maxb;
int getInt()
{
int nr=0;
while(buf[ptr]>'9' || buf[ptr]<'0')
if(++ptr>=maxb)
fread(buf,maxb,1,fin),ptr=0;
while('0'<=buf[ptr] && buf[ptr]<='9')
{
nr=nr*10+buf[ptr]-'0';
if(++ptr>=maxb)
fread(buf,maxb,1,fin),ptr=0;
}
return nr;
}
bool solve()
{
while(!coada.empty())
coada.pop();
for(i=1;i<=n;i++)
{
viz[i]=0;
}
coada.push(s);
while(coada.size())
{
i=coada.front();
coada.pop();
for(j=0;j<G[i].size();j++)
{
if(viz[i]+G[i][j].second<rez[G[i][j].first])
{
return 0;
}
if(viz[i]+G[i][j].second==rez[G[i][j].first] && rez[G[i][j].first]!=viz[G[i][j].first])
{
viz[G[i][j].first]=viz[i]+G[i][j].second;
coada.push(G[i][j].first);
}
}
}
for(i=1;i<=n;i++)
{
if(viz[i]!=rez[i])
return 0;
}
return 1;
}
int main()
{
t=getInt();
while(t--)
{
n=getInt();
m=getInt();
s=getInt();
for(i=1;i<=n;i++)
{
rez[i]=getInt();
}
for(i=1;i<=n;i++)
{
G[i].clear();
}
while(m--)
{
a=getInt();
b=getInt();
c=getInt();
G[a].push_back(make_pair(b, c));
G[b].push_back(make_pair(a, c));
}
if(solve())
{
fout<<"DA\n";
}
else
fout<<"NU\n";
}
}