Cod sursa(job #2464246)

Utilizator aurelionutAurel Popa aurelionut Data 28 septembrie 2019 14:17:24
Problema Distante Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.7 kb
#include <fstream>
#include <vector>
#include <set>

using namespace std;

struct el
{
    int node, cost;
    inline bool operator<(const el &other) const
    {
        return this->cost < other.cost;
    }
};

int main()
{
    ifstream fin("distante.in");
    ofstream fout("distante.out");
    int tests;
    fin >> tests;
    while (tests-- > 0)
    {
        int nodes, edges, start;
        fin >> nodes >> edges >> start;
        vector <int> a(nodes + 1, 0);
        vector <int> dist(nodes + 1, (1 << 30));
        vector <el> graph[nodes + 1];
        for (int i = 1;i <= nodes;++i)
            fin >> a[i];
        for (int i = 1;i <= edges;++i)
        {
            int x, y, z;
            fin >> x >> y >> z;
            graph[x].push_back({y, z});
            graph[y].push_back({x, z});
        }
        set <el> pq;
        pq.insert({start, 0});
        dist[start] = 0;
        while (!pq.empty())
        {
            el aux = *pq.begin();
            pq.erase(*pq.begin());
            int node = aux.node;
            int cost = aux.cost;
            for (auto &x : graph[node])
            {
                if (dist[x.node] > dist[aux.node] + x.cost)
                {
                    el next;
                    next.node = x.node;
                    next.cost = dist[x.node];
                    pq.erase(next);
                    next.cost = dist[aux.node] + x.cost;
                    dist[x.node] = dist[aux.node] + x.cost;
                    pq.insert(next);
                }
            }
        }
        dist[0] = 0;
        fout << ((dist == a) ? "DA\n" : "NU\n");
    }
    fin.close();
    fout.close();
    return 0;
}