Cod sursa(job #2840610)

Utilizator Stefan_GhinescuGhinescu Stefan-George Stefan_Ghinescu Data 28 ianuarie 2022 14:42:04
Problema Distante Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.17 kb
#include <vector>
#include <queue>
#include <cstring>

#define notlocal 69

#if !notlocal

#include <iostream>
#define fin std::cin
#define fout std::cout

#else

#include <fstream>
std::ifstream fin("catun.in");
std::ofstream fout("catun.out");

#endif

using namespace std;

const int NMAX = 50000;

struct MUCHIE
{
    int nod, cost;
};

bool operator<(const MUCHIE& a, const MUCHIE& b)
{
    if (a.cost == b.cost)
    {
        return a.nod > b.nod;
    }
    return a.cost > b.cost;
}

std::vector <MUCHIE>G[NMAX + 5];
int n, viz[NMAX + 5], cost[NMAX + 5], input[NMAX + 5];

inline void Dijkstra(int start)
{
    memset(cost, 0b01111111, sizeof(cost));
    MUCHIE m;
    std::priority_queue <MUCHIE> pq;
    pq.push({start, 0});
    while (!pq.empty())
    {
        while (!pq.empty() && viz[pq.top().nod])
        {
            pq.pop();
        }
        if (pq.empty())
        {
            break;
        }
        m = pq.top();
        pq.pop();
        viz[m.nod] = 1;
        for (auto it : G[m.nod])
        {
            if (!viz[it.nod] && cost[it.nod] > m.cost + it.cost)
            {
                cost[it.nod] = m.cost + it.cost;
                pq.push({it.nod, cost[it.nod]});
            }
        }
    }
}

int main()
{
    bool ok;
    int t;
    fin >> t;
    int m, s, x, y, z;
    while (t--)
    {
        ok = 1;
        fin >> n >> m >> s;
        for (int i = 1; i <= n; ++i)
        {
            fin >> input[i];
        }
        for (int i = 0; i < m; ++i)
        {
            fin >> x >> y >> z;
            G[x].push_back({y, z});
            G[y].push_back({x, z});
        }
        Dijkstra(s);
        for (int i = 1; i <= n; ++i)
        {
            if (cost[i] >= 1 << 30)
            {
                cost[i] = 0;
            }
            if (cost[i] != input[i])
            {
                fout << "NU\n";
                ok = 0;
                break;
            }
        }
        if (ok)
        {
            fout << "DA\n";
        }
        for (int i = 1; i <= n; ++i)
        {
            G[i].clear();
        }
    }
    return 0;
}