Cod sursa(job #2464341)

Utilizator aurelionutAurel Popa aurelionut Data 28 septembrie 2019 14:33:01
Problema Distante Scor 50
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.76 kb
#include <fstream>
#include <vector>
#include <set>
#include <tuple>

#define mp make_pair

using namespace std;

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 < pair <int, int> > 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 < pair <int, int> > pq;
        pq.insert(mp(0, start));
        dist[start] = 0;
        bool good = true;
        while (!pq.empty())
        {
            int node, cost;
            tie(cost, node) = *pq.begin();
            pq.erase(*pq.begin());
            if (dist[node] != a[node])
            {
                good = false;
                break;
            }
            for (auto &x : graph[node])
            {
                if (dist[x.first] > dist[node] + x.second)
                {
                    pair <int, int> next;
                    next.first = dist[x.first];
                    next.second = x.first;
                    pq.erase(next);
                    next.first = dist[node] + x.second;
                    dist[x.first] = dist[node] + x.second;
                    pq.insert(next);
                }
            }
        }
        dist[0] = 0;
        fout << ((good == true) ? "DA\n" : "NU\n");
    }
    fin.close();
    fout.close();
    return 0;
}