Pagini recente » Cod sursa (job #2044010) | Cod sursa (job #2193994) | Cod sursa (job #2034946) | Cod sursa (job #1021087) | Cod sursa (job #1981925)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
/// verifica ca nodu pe care il bagi in heap nu e sursa
using namespace std;
ifstream fin("distante.in");
ofstream fout("distante.out");
#define lim 50010
#define inf 1000000010
struct ini{int nod,cost;};
int n,m,s,path[lim],dist[lim];
vector <ini> G[lim];
priority_queue <pair<int,int>> q;
//#define fin cin
//#define fout cout
int main()
{
int t;
fin>>t;
while(t--)
{
int a,b,c;
/// citire
fin>>n>>m>>s;
for(int i=1; i<=n; i++)
fin>>dist[i], G[i].clear();
for(int i=1; i<=m; i++)
{
fin>>a>>b>>c;
G[a].push_back({b,c});
G[b].push_back({a,c});
}
/// initializari
for(int i=1; i<=n; i++)
path[i]=0;
while(!q.empty())
q.pop();
path[s]=0;
q.push(make_pair(0,s));
/// dijkstra
while(!q.empty())
{
pair <int,int> aux=q.top();
q.pop();
for(auto it:G[aux.second])
if(path[aux.second]+it.cost==dist[it.nod] && it.nod!=s)
{
path[it.nod]=path[aux.second]+it.cost;
q.push(make_pair(path[it.nod],it.nod));
}
}
//
// for(int i=1; i<=n; i++)
// fout<<path[i]<<' ';
// fout<<'\n';
/// raspuns
bool ok=true;
for(int i=1; i<=n && ok; i++)
if(path[i]!=dist[i])
ok=false;
if(ok)
fout<<"DA\n";
else
fout<<"NU\n";
}
fin.close();
fout.close();
return 0;
}