Cod sursa(job #2646119)

Utilizator Razvan48Capatina Razvan Nicolae Razvan48 Data 30 august 2020 23:12:14
Problema Distante Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.96 kb
#include <fstream>
#include <climits>
#include <vector>
#include <queue>
using namespace std;

const int NMAX = 50000;
int cost_br[1 + NMAX];
int cost_real[1 + NMAX];

vector <pair<int, int>> graf[1 + NMAX];
priority_queue <pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> q;

int n;

void dijkstra(int s)
{
    for (int i = 1; i <= n; i++)
    {
        cost_real[i] = INT_MAX;
    }
    cost_real[s] = 0;
    q.push(make_pair(0, s));

    while (!q.empty())
    {
        int nod = q.top().second;
        int cost = q.top().first;
        q.pop();

        if (cost > cost_real[nod]) continue;

        for (int i = 0; i < graf[nod].size(); i++)
        {
            int vecin = graf[nod][i].first;
            int cost = graf[nod][i].second;

            if (cost_real[vecin] > cost_real[nod] + cost)
            {
                cost_real[vecin] = cost + cost_real[nod];
                q.push(make_pair(cost_real[vecin], vecin));
            }
        }
    }
}

int main()
{
    ifstream in("distante.in");
    ofstream out("distante.out");
    int t, m, s, a, b, c;
    int i, j;
    bool bec;

    in >> t;
    for (i = 1; i <= t; i++)
    {
        in >> n >> m >> s;

        for (j = 1; j <= n; j++)
        {
            in >> cost_br[j];
        }

        for (j = 1; j <= m; j++)
        {
            in >> a >> b >> c;
            graf[a].push_back(make_pair(b, c));
            graf[b].push_back(make_pair(a, c));
        }

        dijkstra(s);

        for (j = 1; j <= n; j++)
        {
            graf[j].clear();
        }

        bec = true;
        for (j = 1; j <= n && bec; j++)
        {
            if(cost_real[j] != cost_br[j])
            {
                bec = false;
            }
        }
        if (bec)
        {
            out << "DA" << '\n';
        }
        else
        {
            out << "NU" << '\n';
        }
    }

    return 0;
}