Pagini recente » Cod sursa (job #2738080) | Cod sursa (job #1877118) | Cod sursa (job #279604) | Cod sursa (job #531782) | Cod sursa (job #3005135)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream cin("distante.in");
ofstream cout("distante.out");
const int MAX = 5e4 + 1;
int temp[MAX] , dp[MAX] , n , m , st , x , y , c , t;
struct muchie{
int y , c;
};
vector <muchie> g[MAX];
struct cmp{
bool operator()( int a , int b ){
return a > b;
}
};
priority_queue <int,vector<int>,cmp> pq;
void testcase(){
cin >> n >> m >> st;
for(int i = 1 ; i <= n ; i++){
g[i].clear();
dp[i] = 1e9;
cin >> temp[i];
}
for(int i = 1 ; i <= m ; i++){
cin >> x >> y >> c;
g[x].push_back({y,c});
g[y].push_back({x,c});
}
dp[st] = 0;
pq.push(st);
while(!pq.empty()){
x = pq.top();
pq.pop();
for(auto it : g[x]){
if(dp[it.y] > dp[x] + it.c){
dp[it.y] = dp[x] + it.c;
pq.push(it.y);
}
}
}
for(int i = 1 ; i <= n ; i++){
if(dp[i]!=temp[i]){
cout << "NU" << '\n';
return;
}
}
cout << "DA" << '\n';
}
int main(){
cin >> t;
while(t--){
testcase();
}
return 0;
}