Cod sursa(job #3378271)

Utilizator yolanqaStefanescu Yolanda - Gabriela yolanqa Data 25 noiembrie 2026 17:29:16
Problema Distante Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.45 kb
#include <iostream>
#include <fstream>
#include <queue>
#include <vector>
using namespace std;

std::vector<std::vector<std::pair<int,int>>> graf;
std::vector<int> D;
std::vector<bool> vizitat;

bool verifica(int n, int s){

    if(D[s] != 0)
        return false;

    for(int u = 1; u <= n; u++)
        for(auto [v,c] : graf[u])
            if(D[u] + c < D[v])
                return false;

    std::queue<int> q;
    q.push(s);
    vizitat[s] = true;
    int atinse = 1;

    while(!q.empty()){

        int u = q.front();
        q.pop();

        for(auto [v,c] : graf[u]){
            if(!vizitat[v] && D[u] + c == D[v]){
                vizitat[v] = true;
                atinse++;
                q.push(v);
            }
        }
    }

    return atinse == n;
}

int main() {
    ifstream fin("distante.in");
    ofstream fout("distante.out");

    int t;
    fin >> t;

    while(t--){
        int n, m, s;
        fin >> n >> m >> s;

        graf.assign(n + 1, {});
        D.assign(n + 1, 0);
        vizitat.assign(n + 1, false);

        for(int i = 1; i <= n; i++)
            fin >> D[i];

        for(int i = 0; i < m; i++){
            int a, b, c;
            fin >> a >> b >> c;
            graf[a].push_back({b, c});
            graf[b].push_back({a, c});
        }

        if(verifica(n, s))
            fout << "DA\n";
        else
            fout << "NU\n";
    }

    return 0;
}