Cod sursa(job #3380261)

Utilizator PuternicDezvoltatPuternicSRL PuternicDezvoltat Data 1 decembrie 2026 22:08:28
Problema Distante Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.86 kb
// Link: https://infoarena.ro/problema/distante
// Descriere: O(T * (N + M) log N), din Dijkstra cu heap. Rulez Dijkstra din
// sursa S si compar rezultatul cu vectorul de distante propus din fisier.

#include <bits/stdc++.h>
using namespace std;

static char buf[1 << 16];
static int poz = 0, lung = 0;
static FILE *fin;
static inline char cc() {
    if (poz == lung) { lung = fread(buf, 1, sizeof(buf), fin); poz = 0; if (lung <= 0) return -1; }
    return buf[poz++];
}
static inline int ci() {
    char c = cc();
    while (c < '0' || c > '9') c = cc();
    int x = 0;
    while (c >= '0' && c <= '9') { x = x * 10 + (c - '0'); c = cc(); }
    return x;
}

const int INF = 1e9;

int main() {
    fin = fopen("distante.in", "r");
    FILE *fout = fopen("distante.out", "w");

    int T = ci();
    while (T--) {
        int N = ci(), M = ci(), S = ci();

        vector<int> propuse(N + 1);
        for (int i = 1; i <= N; i++) propuse[i] = ci();

        vector<vector<pair<int,int>>> muchii(N + 1);
        for (int i = 0; i < M; i++) {
            int a = ci(), b = ci(), c = ci();
            muchii[a].push_back({b, c});
            muchii[b].push_back({a, c});
        }

        vector<int> d(N + 1, INF);
        d[S] = 0;
        priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> coada;
        coada.push({0, S});
        while (!coada.empty()) {
            auto [dist, nod] = coada.top(); coada.pop();
            if (dist > d[nod]) continue;
            for (auto [vec, cost] : muchii[nod])
                if (dist + cost < d[vec]) {
                    d[vec] = dist + cost;
                    coada.push({d[vec], vec});
                }
        }

        bool ok = true;
        for (int i = 1; i <= N && ok; i++)
            if (d[i] != propuse[i]) ok = false;
        fputs(ok ? "DA\n" : "NU\n", fout);
    }
    fclose(fin); fclose(fout);
    return 0;
}