Pagini recente » Cod sursa (job #2891733) | Cod sursa (job #1033361) | Cod sursa (job #1156028) | Cod sursa (job #873225) | Cod sursa (job #2368257)
#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 = 1; i <= n; i++){
be >> dist1[i];
// cout << 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[s] = 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){
dist[to] = dist[node] + cost;
heap.insert(make_pair(dist[to], to));
}
}
}
bool ok = true;
for(int i = 1; i <= n; i++){
if(dist[i] == INF){
dist[i] = 0;
}
if(dist1[i] != dist[i]){
ok = false;
break;
}
}
// cout << endl;
if(ok){
ki << "DA" << endl;
}
else{
ki << "NU" << endl;
}
t--;
}
}