Cod sursa(job #2971213)

Utilizator TudosieRazvanTudosie Marius-Razvan TudosieRazvan Data 26 ianuarie 2023 20:27:44
Problema Distante Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.21 kb
#include <fstream>
#include <climits>
#include <vector>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <bitset>
#include <map>
#include <cstring>
#include <algorithm>

#define NMAX 50003 

using namespace std;

int teste;
int n, m;


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

long long preconizat[NMAX];
long long distMin[NMAX];
bool viz[NMAX];

struct muchie {
    int nod;
    long long cost;
    bool operator()(const muchie& a, const muchie& b) 
    {
        return a.cost > b.cost;
    }
};

vector<muchie>graf[NMAX];
priority_queue<muchie, vector<muchie>, muchie>q;

void resetareDistante()
{
    for (int i = 1; i <= n; i++)
    {
        graf[i].clear();
        distMin[i] = LLONG_MAX;
    }
}


void djikstra(int sursa)
{
    distMin[sursa] = 0;
    q.push({sursa, 0});
    while (!q.empty())
    {
        int nodPrec = q.top().nod;
        int costPrec = q.top().cost;
        q.pop();
        if (viz[nodPrec])
        {
            continue;
        }
        viz[nodPrec] = true;
        for (int i = 0; i < graf[nodPrec].size(); i++)
        {
            int nd = graf[nodPrec][i].nod;
            int costMuchie = graf[nodPrec][i].cost;
            if (!viz[nd] && distMin[nd] > costPrec + costMuchie)
            {
                distMin[nd] = costPrec + costMuchie;
                q.push({ nd,distMin[nd] });
            }
        }
    }
}

int main()
{

    fin >> teste;
    for (int i = 1; i <= teste; i++)
    {
        int sursa;
        fin >> n >> m>>sursa;
        for (int j = 1; j <= n; j++)
        {
            fin >> preconizat[j];
            
        }
        resetareDistante();
        for (int j = 1; j <= m; j++)
        {
            int x, y,cost;
            fin >> x >> y>>cost;
            graf[x].push_back({ y,cost });
            graf[y].push_back({ x,cost });
        }

       
        djikstra(sursa);
        bool ok = true;
        for (int j = 1; j <= n; j++)
        {
            if (preconizat[j] != distMin[j])
            {
                ok = false;
            }
        }

        fout << (ok?"DA":"NU") << "\n";
    }
    return 0;
}