Pagini recente » Cod sursa (job #1940061) | Cod sursa (job #2508250) | Cod sursa (job #1786037) | Cod sursa (job #748920) | Cod sursa (job #2368185)
#include <iostream>
#include <vector>
#include <set>
#include <fstream>
#define MAX 100000
#define INF 99999999
using namespace std;
int main(){
ifstream be("distante.in");
ofstream ki("distante.out");
int t;
be >> t;
while(t){
int n, m, s;
be >> n >> m >> s;
vector<pair<int, int>> graph[MAX];
long long dist[MAX];
int dist1[n];
for(int i = 0; i < n; i++){
be >> dist1[i];
}
int from, to, cost;
for(int i = 0; i < m; i++){
be >> from >> to >> cost;
graph[from].push_back(make_pair(to, cost));
dist[i] = INF;
}
dist[1] = 0;
set<pair<int, int>> heap;
heap.insert(make_pair(0, s));
while(!heap.empty()){
int node = heap.begin() -> second;
heap.erase(heap.begin());
for(vector<pair<int, int>>::iterator it = graph[node].begin(); it != graph[node].end(); it++){
to = it -> first;
cost = it-> second;
if(dist[to] > dist[node] + cost){
if(dist[to] != INF){
heap.erase(heap.find(make_pair(dist[to], to)));
}
dist[to] = dist[node] + cost;
heap.insert(make_pair(dist[to], to));
}
}
}
bool ok = true;
for(int i = 2; i <= n; i++){
if(dist[i] == INF){
dist[i] = 0;
}
if(dist1[i - 1] != dist[i])
ok = false;
}
/* for(int i = 1; i <= n; i++){
cout << dist1[i - 1] << " ";
}
cout << endl;
for(int i = 1; i <= n; i++){
cout << dist[i] << " ";
}
*/
if(ok){
ki << "DA" << endl;
}
else{
ki << "NU" << endl;
}
t--;
}
}