Cod sursa(job #2775325)

Utilizator cyg_dawidDavid Ghiberdic cyg_dawid Data 15 septembrie 2021 12:59:51
Problema Distante Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.89 kb
#include <fstream>
#include <climits>
#include <vector>
#include <queue>

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

int const nmax = 50000;
int d[nmax + 5];
int ans[nmax + 5];
bool seen[nmax + 5];
struct velem {
    int nod, drum;
};
std::vector <velem> adj[nmax + 5];

struct pqelem {
    int ind;
    int val;

    bool operator < (pqelem const other) const {
        return val > other.val;
    }
};
std::priority_queue <pqelem> pq;

int main()
{
    int t;
    fin >> t;
    for (int i = 0; i <= nmax; i++)
        ans[i] = INT_MAX;
    while (t--) {
        int n, m, s;
        fin >> n >> m >> s;
        for (int i = 1; i <= n; i++)
            fin >> d[i];
        for (int i = 1; i <= m; i++) {
            int x, y, cost;
            fin >> x >> y >> cost;
            adj[x].push_back({y, cost});
            adj[y].push_back({x, cost});
        }
        ans[s] = 0;
        pq.push({s, 0});
        while (!pq.empty()) {
            pqelem aux = pq.top();
            pq.pop();
            if (seen[aux.ind]) continue;
            seen[aux.ind] = true;
            int cnt = adj[aux.ind].size();
            for (int i = 0; i < cnt; i++) {
                if (ans[adj[aux.ind][i].nod] > aux.val + adj[aux.ind][i].drum) {
                    ans[adj[aux.ind][i].nod] = aux.val + adj[aux.ind][i].drum;
                    pq.push({adj[aux.ind][i].nod, ans[adj[aux.ind][i].nod]});
                }
            }
        }
        bool ok = true;
        for (int i = 1; i <= n; i++) {
            if (d[i] != ans[i])
                ok = false;
            ans[i] = INT_MAX;
        }
        for (int i = 0; i <= nmax; i++) {
            while (!adj[i].empty())
                adj[i].pop_back();
        }
        if (ok)
            fout << "DA\n";
        else
            fout << "NU\n";
    }
    return 0;
}