Pagini recente » Cod sursa (job #2796995) | Cod sursa (job #2416075) | Cod sursa (job #1567059) | Cod sursa (job #2440351) | Cod sursa (job #2797031)
#include <queue>
#include <fstream>
using namespace std;
#define N_MAX 50000
#define INF (1 << 30)
priority_queue<pair<int, int>> dijkstra;
vector<pair<int, int>> G[N_MAX];
int ans[N_MAX], expected_ans[N_MAX];
void DIJKSTRA() {
while ( !dijkstra.empty() ) {
pair<int, int> elem = dijkstra.top();
dijkstra.pop();
if ( ans[elem.second] == -INF || ans[elem.second] == elem.first ) {
for ( pair<int, int> copil : G[elem.second] ) {
if ( ans[copil.first] < elem.first + copil.second ) {
ans[copil.first] = elem.first + copil.second;
dijkstra.push( {ans[copil.first], copil.first} );
}
}
}
}
}
ifstream cin ( "distante.in" );
ofstream cout ( "distante.out" );
int main() {
int t, i, j, n, m, s, a, b, cost;
cin >> t;
ios::sync_with_stdio(true);
cin.tie(0);
cout.tie(0);
for ( i = 0; i < t; i++ ) {
cin >> n >> m >> s;
for ( j = 0; j <= n; j++ )
ans[j] = -INF;
ans[s] = -1;
for ( j = 1; j <= n; j++ ) {
cin >> expected_ans[j];
G[j].clear();
}
for ( j = 1; j <= m; j++ ) {
cin >> a >> b >> cost;
G[a].emplace_back(b, cost * -1);
G[b].emplace_back(a, cost * -1);
}
dijkstra.push({-1, s});
DIJKSTRA();
j = 1;
while ( j <= n && ( ans[j] + 1 ) * -1 == expected_ans[j] ) {
j++;
}
if ( j <= n )
cout << "NU\n";
else
cout << "DA\n";
}
return 0;
}