Cod sursa(job #2766976)

Utilizator vlad2009Vlad Tutunaru vlad2009 Data 4 august 2021 12:20:02
Problema Distante Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.85 kb
#include <fstream>
#include <vector>
#include <queue>

using namespace std;

const int Nmax = 50000;
const long long INF = (1LL << 60);
vector <pair <int, int>> adj[Nmax + 1];
priority_queue <pair <int, int>> q;
bool viz[Nmax + 1];
long long dist[Nmax + 1];
int a[Nmax + 1];

void Dijkstra(int node)
{
    for (int i = 1; i <= Nmax; i++)
    {
        dist[i] = INF;
    }
    dist[node] = 0;
    q.push({0, node});
    while (!q.empty())
    {
        int s = q.top().second;
        q.pop();
        if (viz[s])
        {
            continue;
        }
        viz[s] = true;
        for (auto u : adj[s])
        {
            int b = u.first, c = u.second;
            if (dist[s] + c < dist[b])
            {
                dist[b] = dist[s] + c;
                q.push({-dist[b], b});
            }
        }
    }
}

void clearAll()
{
    for (int i = 1; i <= Nmax; i++)
    {
        adj[i].clear();
    }
    while (!q.empty())
    {
        q.pop();
    }
}

int main()
{
    ifstream fin("distante.in");
    ofstream fout("distante.out");
    int t;
    fin >> t;
    while (t--)
    {
        int n, m, s;
        fin >> n >> m >> s;
        for (int i = 1; i <= n; i++)
        {
            fin >> a[i];
        }
        for (int i = 1; i <= m; i++)
        {
            int u, v, c;
            fin >> u >> v >> c;
            adj[u].push_back({v, c});
            adj[v].push_back({u, c});
        }
        Dijkstra(s);
        bool ok = 1;
        for (int i = 1; i <= n; i++)
        {
            if (dist[i] != a[i])
            {
                ok = 0;
                break;
            }
        }
        if (ok)
        {
            fout << "DA\n";
        }
        else
        {
            fout << "NU\n";
        }
        clearAll();
    }
    return 0;
}