Pagini recente » Cod sursa (job #1950774) | Cod sursa (job #2474323) | Cod sursa (job #3004062) | Cod sursa (job #2752177) | Cod sursa (job #3265119)
#include <bits/stdc++.h>
using namespace std;
ifstream f("distante.in");
ofstream g("distante.out");
int n, m, s, x, y, c, cost[50002], t, rez[50001];
vector <pair<int, int>>v[50002];
void dijkrsta(int p, int n)
{
priority_queue <pair<int, int>> pq;
for(int i=1; i<=n; i++)
cost[i]=INT_MAX;
cost[p]=0;
pq.push({0, p});
while(!pq.empty())
{
pair<int, int> cr=pq.top();
pq.pop();
if(-cr.first>cost[cr.second])
continue;
for(pair<int, int> i:v[cr.second])
if(cost[i.first]>-cr.first+i.second)
cost[i.first]=-cr.first+i.second, pq.push({-cost[i.first], i.first});
}
}
int main()
{
f>>t;
for(int o=1; o<=t; o++)
{
f>>n>>m>>s;
for(int i=1; i<=n; i++)
f>>rez[i];
for(int i=1; i<=m; i++)
{
f>>x>>y>>c;
v[x].push_back({y, c});
v[y].push_back({x, c});
}
dijkrsta(s, n);
sort(cost+1, cost+n+1);
sort(rez+1, rez+n+1);
int ok=1;
for(int i=1; i<=n; i++)
{
if(rez[i]!=cost[i])
{ok=0, g<<"NU"<<'\n'; break;}
}
if(ok==1)
g<<"DA"<<'\n';
for(int i=1; i<=n; i++)
v[i].clear();
}
return 0;
}