Pagini recente » Cod sursa (job #909703) | Cod sursa (job #525274) | Cod sursa (job #969441) | Cod sursa (job #1425996) | Cod sursa (job #2505229)
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
ifstream in("distante.in");
ofstream out("distante.out");
const int NMAX=5e4 + 10;
int T, n, m, S, DB[NMAX], D[NMAX];
vector < pair < int, int > > Edge[NMAX];
bitset < NMAX > Seen;
void initialize()
{
for(int i=1; i<=n; i++)
D[i]=10000000, Seen[i]=0, Edge[i].clear();
}
bool Dijkstra()
{
priority_queue < pair < int, int > > Q;
Q.push({0, S});
D[S]=0;
int x;
while(!Q.empty())
{
x=Q.top().second;
Q.pop();
if(!Seen[x])
{
for(auto it : Edge[x])
{
if(D[x]+it.second<D[it.first])
D[it.first]=D[x]+it.second, Q.push({-D[it.first], it.first});
if(D[it.first]<DB[it.first])
return 0;
}
Seen[x]=1;
}
}
for(int i=1; i<=n; i++)
if(DB[i]!=D[i])
return 0;
return 1;
}
void Read_Solve()
{
in>>T;
int x, y, c;
for(int i=1; i<NMAX; i++)
D[i]=10000000;
for(int k=1; k<=T; k++)
{
in>>n>>m>>S;
for(int i=1; i<=n; i++)
in>>DB[i];
for(int i=1; i<=m; i++)
in>>x>>y>>c, Edge[x].push_back({y, c}), Edge[y].push_back({x, c});
if(Dijkstra())
out<<"DA\n";
else
out<<"NU\n";
initialize();
}
}
int main()
{
Read_Solve();
return 0;
}