Cod sursa(job #2686254)

Utilizator TheGodFather2131Alexandru Miclea TheGodFather2131 Data 18 decembrie 2020 19:08:25
Problema Distante Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.05 kb
//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(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;
        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});
        }

        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(n);
        for (int i = 0; i < n; i++){
            if (dp[i + 1] == INF) ans[i] = 0;
            else ans[i] = dp[i + 1];
        } 

        if (inans == ans) cout << "DA";
        else cout << "NU";
        cout << '\n';
    }

    return 0;
}