Pagini recente » Cod sursa (job #2670340) | Cod sursa (job #3262617) | Cod sursa (job #1142348) | Cod sursa (job #2881266) | Cod sursa (job #2930113)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <bitset>
using namespace std;
const int N = 5e4 + 1;
vector <pair<int, int>> L[N];
vector<int> dist;
vector<int> b;
bitset<N> vis;
priority_queue< pair<int, int>, vector<pair <int, int> >, greater<pair<int, int> > > pq;
int n,t,m,k,c,a;
ifstream fin("distante.in");
ofstream fout("distante.out");
void Read()
{
int x,y;
fin>>n>>m>>k;
b.resize(n+1);
for(int i=1;i<=n;i++)
fin>>b[i];
for(int i=1;i<=n;i++)
L[i].clear();
while(m--)
{
fin>>x >> y>> c;
L[x].push_back({y,c});
L[y].push_back({x,c});
}
}
void Dijkstra()
{
dist.resize(n+1, 2e9);
vis.reset();
dist[k] = 0;
pq.push({0,k});
while(!pq.empty())
{
pair<int, int> cur = pq.top();
pq.pop();
if (!vis[cur.second])
{
vis[cur.second] = 1;
for (auto next: L[cur.second]) {
if (dist[next.first] > next.second + cur.first)
{
dist[next.first] = next.second + cur.first;
pq.push({dist[next.first], next.first});
}
}
}
}
for(int i=1;i<=n;i++)
{
if(dist[i] !=b[i])
{
fout<<"NU"<<"\n";
return;
}
}
fout<<"DA"<<"\n";
}
int main() {
fin>>t;
while(t--)
{
Read();
Dijkstra();
}
return 0;
}