Pagini recente » Rating Irina Istrate (irinaaaaaaaaaaaaaaa) | Cod sursa (job #2120126) | Cod sursa (job #545283) | Cod sursa (job #2554677) | Cod sursa (job #3195391)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("distante.in");
ofstream fout("distante.out");
struct per{
int nod, cost;
bool operator<(const per& other) const {
return (*this).cost > other.cost;
}
};
int j, k, w, x, y, l, s, n, m, cost[105], d[105];
vector<per> v[105];
priority_queue<per> q;
void dj( int st ){
for ( int i = 1; i <= n; i++ ) cost[i] = 1e9;
cost[st] = 0;
q.push({st, 0});
while ( !q.empty() ){
per x = q.top();
q.pop();
if ( x.cost != cost[x.nod] ) continue;
for ( auto el:v[x.nod] ){
if ( cost[el.nod] > x.cost + el.cost ){
cost[el.nod] = x.cost + el.cost;
q.push({el.nod, x.cost + el.cost});
}
}
}
}
int main()
{
fin >> w;
for ( int i = 1; i <= w; i++ ){
fin >> n >> m >> s;
for ( j = 1; j <= n; j++ ) fin >> d[j];
for ( j = 1; j <= m; j++ ){
fin >> x >> y >> l;
v[x].push_back({y, l});
v[y].push_back({x, l});
}
dj(s);
k = 1;
for ( j = 1; j <= n; j++ ){
if ( d[j] != cost[j] ) k = 0;
}
if( k == 0 ) fout << "NU";
else fout << "DA";
fout << endl;
}
}