Cod sursa(job #2464560)

Utilizator aurelionutAurel Popa aurelionut Data 28 septembrie 2019 16:10:19
Problema Distante Scor 50
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.81 kb

#include <fstream>
#include <vector>
#include <set>
#include <tuple>

#define mp make_pair

using namespace std;

const int NMAX = 50005;
const int INF = (1 << 30);
int a[NMAX], dist[NMAX];
vector < pair <int, int> > graph[NMAX];
set < pair <int, int> > pq;

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;
        for (int i = 1;i <= nodes;++i)
        {
            fin >> a[i];
            dist[i] = INF;
            graph[i].clear();
        }
        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});
        }
        pq.clear();
        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);
                }
            }
        }
        fout << ((good == true) ? "DA\n" : "NU\n");
    }
    fin.close();
    fout.close();
    return 0;
}