Pagini recente » Cod sursa (job #1204854) | Cod sursa (job #2612744) | Cod sursa (job #2560391) | Cod sursa (job #2461305) | Cod sursa (job #3319438)
#include <fstream>
#include <vector>
#include <queue>
#define NMAX 50002
#define INF (1<<30)
using namespace std;
ifstream fin("distante.in");
ofstream fout("distante.out");
int T,N,M,S,D[NMAX],cost[NMAX],viz[NMAX];
vector<pair<int,int>> graph[NMAX];
priority_queue<pair<int,int>>q;
void citire()
{
fin>>N>>M>>S;
for(int i=1; i<=N; i++)
{
fin>>D[i];
cost[i]=INF;
viz[i]=0;
graph[i].clear();
}
int a,b,c;
for(int i=1; i<=M; i++)
{
fin>>a>>b>>c;
graph[a].push_back({b,c});
graph[b].push_back({a,c});
}
}
int main()
{
fin>>T;
for(int j=1; j<=T; j++)
{
citire();
cost[S]=0;
q.push({cost[S],S});
while(!q.empty())
{
pair<int,int> nod=q.top();
q.pop();
if(!viz[nod.second])
{
viz[nod.second]=1;
for(int i=0; i<graph[nod.second].size(); i++)
{
int nxt=graph[nod.second][i].first;
int val=graph[nod.second][i].second;
if(-nod.first+val<cost[nxt])
{
cost[nxt]=-nod.first+val;
q.push({-cost[nxt],nxt});
}
}
}
}
int ok=1;
for(int i=1; i<=N; i++)
{
if(D[i]!=cost[i])
{
ok=0;
break;
}
}
if(ok)
{
fout<< "DA";
}
else
{
fout<< "NU";
}
fout<< "\n";
}
return 0;
}