Cod sursa(job #3350901)

Utilizator David_RadavoiRadavoi David Alexandru David_Radavoi Data 14 aprilie 2026 17:39:07
Problema Distante Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.69 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <utility>
#include <queue>
#include <climits>

using namespace std;

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

#define NMAX 50000

vector <pair<int, int>> graph[NMAX + 1];
int rezBronz[NMAX + 1];

vector <int> dijkstra(vector<pair<int, int>> graph[], int start, int N){
    vector <int> dist(N + 1);
    for (int i = 1; i <= N; i++){
        dist[i] = INT_MAX;
    }
    priority_queue <pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
    pq.push({0, start});
    dist[start] = 0;
    while (!pq.empty()){
        auto [currentDist, node] = pq.top();
        pq.pop();
        for (auto muchie : graph[node]){
            int neighbour = muchie.first;
            int cost = muchie.second;
            if (currentDist + cost < dist[neighbour]){
                dist[neighbour] = currentDist + cost;
                pq.push({currentDist + cost, neighbour});
            }
        }
    }
    return dist;
}

void doTest(int N, int M, int S){
    for (int i = 1; i <= N; i++){
        fin >> rezBronz[i];
        graph[i].clear();
    }
    for (int i = 1; i <= M; i++){
        int a, b, c;
        fin >> a >> b >> c;
        graph[a].push_back({b, c});
        graph[b].push_back({a, c});
    }
    vector <int> dist = dijkstra(graph, S, N);
    for (int i = 1; i <= N; i++){
        if (dist[i] != rezBronz[i]){
            fout << "NU\n";
            return;
        }
    }
    fout << "DA\n";
}

int main()
{
    int T;
    fin >> T;
    while (T--){
        int N, M, S;
        fin >> N >> M >> S;
        doTest(N, M, S);
    }
    return 0;
}