Cod sursa(job #3285277)

Utilizator PetrudpPetru Paun Petrudp Data 12 martie 2025 17:29:57
Problema Distante Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.81 kb
#include <fstream>
#include <vector>
#include <queue>

using namespace std;

const int INF = 1e9 + 1;

struct muchie
{
    int next, cost;
    bool operator<(const muchie &other) const
    {
        return cost > other.cost;
    }
};

void dijkstra(int start, int n, vector<vector<muchie>> &g, vector<int> &dist)
{
    priority_queue<muchie> q;
    q.push({start, 0});
    dist[start] = 0;
    while (!q.empty())
    {
        int node = q.top().next;
        int cost = q.top().cost;
        q.pop();

        if(cost > dist[node])
        {
            continue;
        }
        for(auto& y : g[node])
        {
            if(cost + y.cost < dist[y.next])
            {
                dist[y.next] = cost + y.cost;
                q.push({y.next, dist[y.next]});
            }
        }
    }
}

int main()
{
    fstream in("distante.in");
    fstream out("distante.out");
    int t;
    in >> t;
    while(t--)
    {
        int n, m, rad;
        in >> n >> m >> rad;
        vector<int> d_start(n + 1), d_crt(n + 1, INF);
        vector<vector<muchie>> g(n + 1);

        for (int i = 1; i <= n; i++)
        {
            in >> d_start[i];
        }

        for (int i = 1; i <= m; i++)
        {
            int x, y, c;
            in >> x >> y >> c;
            g[x].push_back({y, c});
            g[y].push_back({x, c});
        }
        dijkstra(rad, n, g, d_crt);

        bool ok = true;
        for (int i = 1; i <= n; i++)
        {
            if (d_start[i] != d_crt[i])
            {
                ok = false;
                break;
            }
        }
        if (ok)
        {
            out << "DA\n";
        }
        else
        {
            out << "NU\n";
        }
    }

    in.close();
    out.close();
    return 0;
}