Pagini recente » Cod sursa (job #2789529) | Cod sursa (job #1401020) | Cod sursa (job #3180933) | Cod sursa (job #233030) | Cod sursa (job #1894043)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#define nmax 50005
#define inf 1e9
using namespace std;
ifstream f("distante.in");
ofstream g("distante.out");
vector < vector < pair <int, int> > > a(nmax);
priority_queue < pair <int, int> > PQ;
int v[nmax], dist[nmax], viz[nmax];
int t, n, m, s;
void init()
{
for(int i=1; i<=n; ++i)
{
viz[i]=0;
dist[i]=inf;
}
dist[s]=0;
}
void dijkstra()
{
PQ.push(make_pair(0, s));
while(PQ.size())
{
pair <int,int> aux;
aux=PQ.top();
PQ.pop();
int nod=aux.second;
if(viz[nod]==1)
{
continue ;
}
viz[nod]=1;
for(int i=0; i<a[nod].size(); ++i)
{
int sum=dist[nod]+a[nod][i].second;
if(sum<dist[a[nod][i].first])
{
dist[a[nod][i].first]=sum;
PQ.push(make_pair(-sum, a[nod][i].first));
}
}
}
for(int i=1; i<=n; ++i)
{
if(dist[i]==inf) dist[i]=0;
}
}
bool verif()
{
for(int i=1; i<=n; ++i)
{
if(v[i]!=dist[i]) return false;
}
return true;
}
void citire()
{
f>>t;
while(t--)
{
f>>n>>m>>s;
int x, y, z;
for(int i=1; i<=n; ++i)
{
f>>v[i];
}
for(int i=1; i<=m; ++i)
{
f>>x>>y>>z;
a[x].push_back(make_pair(y, z));
}
init();
dijkstra();
if(verif()) g<<"DA\n";
else g<<"NU\n";
}
}
int main()
{
citire();
return 0;
}