Pagini recente » Istoria paginii runda/eusebiu_oji_2015_cls10/clasament | Cod sursa (job #2627792) | Cod sursa (job #1713143) | Cod sursa (job #150719) | Cod sursa (job #2686264)
//ALEXANDRU MICLEA
#include <vector>
#include <algorithm>
#include <string>
#include <string.h>
#include <cstring>
#include <queue>
#include <map>
#include <set>
#include <unordered_map>
#include <time.h>
#include <iomanip>
#include <deque>
#include <math.h>
#include <cmath>
#include <assert.h>
#include <stack>
#include <bitset>
#include <random>
#include <chrono>
#include <assert.h>
using namespace std;
using ll = long long;
#include <fstream>
//ifstream cin("input.in"); ofstream cout("output.out");
ifstream cin("distante.in"); ofstream cout("distante.out");
//VARIABLES
class cmp {
public:
bool operator () (pair <int, int> &a, pair <int,int> &b){
return a.second > b.second;
}
};
const int MAXM = 100005;
vector <vector <pair <int, int>>> gr(2 * MAXM);
priority_queue <pair <int, int>, vector <pair <int, int>>, cmp> q;
int dp[50005];
const int INF = 1e9;
//FUNCTIONS
//MAIN
int main() {
int t; cin >> t;
while (t--){
int n, m, s, ok = 1;
cin >> n >> m >> s;
vector <int> inans(n);
for (auto & x : inans) cin >> x;
for (int i = 1; i <= n; i++) dp[i] = INF;
dp[s] = 0;
for (int i = 1; i <= m; i++){
int a, b, val;
cin >> a >> b >> val;
gr[a].push_back({b, val});
gr[b].push_back({a, val});
}
if (inans[s - 1]) ok = 0;
q.push({s, 0});
while (!q.empty()){
pair <int, int> now = q.top();
q.pop();
if (dp[now.first] != now.second) continue;
for (auto& x : gr[now.first]){
if (dp[x.first] > now.second + x.second){
dp[x.first] = now.second + x.second;
q.push({x.first, dp[x.first]});
}
}
}
vector <int> ans;
for (int i = 1; i <= n; i++){
if (dp[i] == INF) ans.push_back(0);
else ans.push_back(dp[i]);
}
for (int i = 0; i < n; i++){
if (ans[i] != inans[i]) ok = 0;
}
if (ok) cout << "DA";
else cout << "NU";
cout << '\n';
}
return 0;
}