Cod sursa(job #2916499)

Utilizator Iordache_CezarIordache Cezar Iordache_Cezar Data 30 iulie 2022 10:02:10
Problema Distante Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.79 kb
#include <bits/stdc++.h>
#define NMAX 50008
#define INF 1e9

using namespace std;
ifstream fin ("distante.in");
ofstream fout ("distante.out");

struct Muchie
{
    int vf, cost;
};

struct comparare
{
    bool operator () (Muchie & a, Muchie & b)
    {
        return a.cost > b.cost;
    }
};

int t, n, m, start;
int dist[NMAX], dp[NMAX];
vector < pair <int, int> > G[NMAX];
priority_queue <Muchie, vector<Muchie>, comparare> H;

void Dijkstra(int start);

int main()
{
    int a, b, c;
    fin >> t;
    while (t--)
    {
        fin >> n >> m >> start;
        for (int i = 1; i <= n; i++)
        {
            fin >> dist[i];
            dp[i] = INF;
            G[i].clear();
        }
        for (int i = 1; i <= m; i++)
        {
            fin >> a >> b >> c;
            G[a].push_back({b, c});
            G[b].push_back({a, c});
        }
        Dijkstra(start);
    }
    return 0;
}

void Dijkstra(int start)
{
    Muchie M;
    H.push({start, 0});
    dp[start] = 0;

    while (!H.empty())
    {
        M = H.top();
        H.pop();

        if (M.cost > dp[M.vf])
            continue;

        if (M.cost < dist[M.vf])
        {
            fout << "NU\n";
            while (!H.empty())
                H.pop();
            return;
        }

        for (int i = 0; i < G[M.vf].size(); i++)
        {
            int vf = G[M.vf][i].first;
            int cost = G[M.vf][i].second;
            if (M.cost + cost < dp[vf])
            {
                dp[vf] = M.cost + cost;
                H.push({vf, dp[vf]});
            }
        }
    }

    int i;
    for (i = 1; i <= n; i++)
        if (dp[i] != dist[i])
            break;
    if (i > n)
        fout << "DA\n";
    else
        fout << "NU\n";
}