Pagini recente » Cod sursa (job #3273990) | Cod sursa (job #1087451) | Cod sursa (job #3238383) | Cod sursa (job #2261089) | Cod sursa (job #1510361)
#include <iostream>
#include<fstream>
#include<vector>
#include<queue>
#define inf 1<<30
#define maxn 50001
using namespace std;
ifstream in("distante.in");
ofstream out("distante.out");
int n, m, nod;
int Cost[maxn];
int val[maxn];
vector <pair <int, int> > G[maxn];
queue <pair <int, int> > q;
void read()
{
int a, b, c;
in>>n>>m>>nod;
for(int i=1; i<=n; i++)
in>>val[i];
for(int i=1; i<=m; i++)
{
in>>a>>b>>c;
G[a].push_back(make_pair(b, c));
}
}
void init()
{
q.push(make_pair(nod, 0));
for(int i=nod+1; i<=n; i++)
Cost[i]=inf;
for(int i=1; i<nod; i++)
Cost[i]=inf;
}
void dijkstra()
{
init();
while(!q.empty())
{
int nodCurent=q.front().first;
int costCurent=q.front().second;
q.pop();
for(int i=0; i<G[nodCurent].size(); i++)
{int vecin= G[nodCurent][i].first;
int costVecin = G[nodCurent][i].second;
if(costVecin + costCurent< Cost[vecin])
{Cost[vecin] = costVecin + costCurent;
q.push(make_pair(vecin, Cost[vecin]));
}
}
}
}
void write()
{ bool ok=true;
for(int i=1; i<=n; i++)
if(Cost[i]!=val[i]) ok=false;
if(ok==false) out<<"NU"<<"\n";
else out<<"DA"<<"\n";
}
int main()
{ int t;
in>>t;
for(int i=1; i<=t; i++)
{ read();
dijkstra();
write();
}
return 0;
}