Pagini recente » Cod sursa (job #2083541) | Cod sursa (job #46529) | Istoria paginii runda/jc2018-runda-1/clasament | Cod sursa (job #266678) | Cod sursa (job #798018)
Cod sursa(job #798018)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
const int inf=16000000;
const int maxn=50001;
vector< pair<int, int> >::iterator it;
vector< pair<int, int> > v[maxn];
queue<int> q;
int t, x, y, z, n, m, s, i, d[maxn], a[maxn];
bool block[maxn];
void compute(int s)
{
int x;
d[s]=0;
q.push(s);
block[s]=1;
while(!q.empty())
{
x=q.front();
q.pop();
block[x]=0;
for(it=v[x].begin(); it!=v[x].end(); ++it)
if(d[it->first] > it->second + d[x])
{
d[it->first] = it->second + d[x];
if(!block[it->first])
{
q.push(it->first);
block[it->first]=1;
}
}
}
}
bool check()
{
for(int i=1; i<=n; ++i)
if(d[i]!=a[i]) return 0;
return 1;
}
void init()
{
for(int i=1; i<=n; ++i) d[i]=inf, block[i]=0, v[i].clear();
}
int main()
{
freopen("distante.in", "r", stdin);
freopen("distante.out", "w", stdout);
scanf("%d", &t);
for(int k=1; k<=t; ++k)
{
scanf("%d%d%d", &n, &m, &s);
for(i=1; i<=n; ++i) scanf("%d", &a[i]);
init();
for(; m; --m)
{
scanf("%d%d%d", &x, &y, &z);
v[x].push_back(make_pair(y, z));
v[y].push_back(make_pair(x, z));
}
compute(s);
if(check()) printf("DA\n");
else printf("NU\n");
}
}