Cod sursa(job #3351021)

Utilizator Rares_MihaescuRares-Andrei Mihaescu Rares_Mihaescu Data 15 aprilie 2026 18:12:13
Problema Distante Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.35 kb
#include <iostream>
#include <vector>
#include <cstdio>

using namespace std;

int main() {
    freopen("distante.in", "r", stdin);
    freopen("distante.out", "w", stdout);

    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int T;
    cin >> T;

    while (T--) {
        int N, M, S;
        cin >> N >> M >> S;

        vector<long long> D(N + 1);
        for (int i = 1; i <= N; i++) {
            cin >> D[i];
        }

        vector<int> okPred(N + 1, 0);
        bool good = true;

        if (D[S] != 0) {
            good = false;
        }

        for (int i = 0; i < M; i++) {
            int a, b, c;
            cin >> a >> b >> c;

            if (D[a] + c < D[b]) {
                good = false;
            }
            if (D[b] + c < D[a]) {
                good = false;
            }

            if (D[a] + c == D[b]) {
                okPred[b] = 1;
            }
            if (D[b] + c == D[a]) {
                okPred[a] = 1;
            }
        }

        for (int i = 1; i <= N; i++) {
            if (i == S) {
                continue;
            }
            if (!okPred[i]) {
                good = false;
            }
        }

        if (good) {
            cout << "DA\n";
        }
        else {
            cout << "NU\n";
        }
    }

    return 0;
}