Cod sursa(job #2464561)

Utilizator aurelionutAurel Popa aurelionut Data 28 septembrie 2019 16:10:49
Problema Distante Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.57 kb
#include <fstream>
#include <vector>
#include <queue>
#include <bitset>

using namespace std;

bitset <50005> inq;

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, 0);
        vector < pair <int, int> > graph[nodes + 1];
        for (int i = 1;i <= nodes;++i)
        {
            fin >> a[i];
            dist[i] = (1 << 30);
        }
        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});
        }
        dist[start] = 0;
        inq[start] = 1;
        queue <int> q;
        q.push(start);
        bool good = true;
        while (!q.empty())
        {
            int node = q.front();
            q.pop();
            inq[node] = 0;
            for (auto &x : graph[node])
            {
                if (dist[x.first] > dist[node] + x.second)
                {
                    dist[x.first] = dist[node] + x.second;
                    if (inq[x.first] == 0)
                    {
                        inq[x.first] = 1;
                        q.push(x.first);
                    }
                }
            }
        }
        fout << ((a == dist) ? "DA\n" : "NU\n");
        inq.reset();
    }
    fin.close();
    fout.close();
    return 0;
}