Pagini recente » Cod sursa (job #3003694) | Cod sursa (job #560293) | tema | Cod sursa (job #950177) | Cod sursa (job #2372378)
#include <fstream>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#define INF 1 << 30
#define MAX 50001
using namespace std;
int main(){
ifstream be("distante.in");
ofstream ki("distante.out");
int t;
be >> t;
while(t){
int n, m, s, from, to, cost;
int dist[MAX];
vector<pair <int, int>> graph[MAX];
be >> n >> m >> s;
int distante[n];
for(int i = 1; i <= n; i++){
be >> distante[i];
dist[i] = INF;
}
dist[s] = 0;
for(int i = 0; i < m; i++){
be >> from >> to >> cost;
graph[from].push_back(make_pair(to, cost));
graph[to].push_back(make_pair(from, cost));
}
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;
// cout << to << " " << cost << endl;
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));
}
}
}
/*
for(int i = 1; i <= n; i++){
cout << dist[i] << " ";
}
cout << endl;
for(int i = 1; i <= n; i++){
cout << distante[i] << " ";
}
cout << endl;
*/
bool ok = true;
for(int i = 1; i <= n; i++){
if(dist[i] != distante[i]){
ok = false;
break;
}
}
if(ok){
ki << "DA" << endl;
}
else{
ki << "NU" << endl;
}
t--;
}
}