Cod sursa(job #2464196)

Utilizator aurelionutAurel Popa aurelionut Data 28 septembrie 2019 14:07:35
Problema Distante Scor 50
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.6 kb
#include <fstream>
#include <vector>
#include <bitset>
#include <queue>

using namespace std;

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

bitset <50005> seen;

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];
        seen.reset();
        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});
        }
        priority_queue <el> pq;
        pq.push({start, 0});
        dist[start] = 0;
        while (!pq.empty())
        {
            el aux = pq.top();
            pq.pop();
            if (seen[aux.node] == 0)
            {
                seen[aux.node] = 1;
                for (auto &x : graph[aux.node])
                    if (dist[x.node] > dist[aux.node] + x.cost)
                    {
                        dist[x.node] = dist[aux.node] + x.cost;
                        pq.push({x.node, dist[x.node]});
                    }
            }
        }
        dist[0] = 0;
        fout << ((dist == a) ? "DA\n" : "NU\n");
    }
    fin.close();
    fout.close();
    return 0;
}