Pagini recente » Cod sursa (job #898608) | Cod sursa (job #2722473) | Cod sursa (job #2585694) | Cod sursa (job #1485906) | Cod sursa (job #3171322)
#include <bits/stdc++.h>
using namespace std;
ifstream f("distante.in");
ofstream g("distante.out");
const int NMAX=50005,INF=0x3F3F3F3F;
int T,n,m,source,x,y,c;
bool ok;
bitset<NMAX>used;
vector<int>init_dist,dist;
vector<vector<pair<int,int>>>graph;
priority_queue<pair<int,int>>pq;
void dijkstra()
{
int node;
dist[source]=0;
pq.push({0,source});
while(!pq.empty())
{
node=pq.top().second;
pq.pop();
if(!used[node])
{
for(pair<int,int>next:graph[node])
if(dist[node]+next.second<dist[next.first])
{
dist[next.first]=dist[node]+next.second;
pq.push({-dist[next.first],next.first});
}
used[node]=1;
}
}
}
int main()
{
f>>T;
for(int t=1; t<=T; t++)
{
f>>n>>m>>source;
ok=1;
used.reset();
init_dist.resize(n+1);
dist.assign(n+1,INF);
graph.assign(n+1,vector<pair<int,int>>());
for(int i=1;i<=n;i++)
f>>init_dist[i];
for(int i=1; i<=m; i++)
{
f>>x>>y>>c;
graph[x].push_back({y,c});
graph[y].push_back({x,c});
}
dijkstra();
for(int i=1; i<=n; i++)
if(init_dist[i]!=dist[i])
{
ok=0;
break;
}
if(ok==0)
g<<"NU"<<'\n';
else
g<<"DA"<<'\n';
}
return 0;
}