Cod sursa(job #2722414)

Utilizator mihai03Mihai Grigore mihai03 Data 12 martie 2021 20:18:34
Problema Distante Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.75 kb
#include <fstream>
#include <queue>
#include <iostream>
#define inf 0x3f3f3f3f
using namespace std;

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

const int nmax = 50005;

int t;

int n, m, s;
int bronzDist[nmax];
int actualDist[nmax];

vector < pair < int, int > > G[nmax];
vector < bool > seen;

queue < int > coada;

int main()
{
    fin >> t;
    while(t--)
    {
        fin >> n >> m >> s;

        seen = vector < bool > (n + 1, 0);

        for(int i = 1; i <= n; ++i)
        {
            fin >> bronzDist[i];
        }
        for(int i = 1; i <= m; ++i)
        {
            int x, y, c;
            fin >> x >> y >> c;
            G[x].push_back({y, c});
            G[y].push_back({x, c});
        }

        coada.push(s);
        seen[s] = 1;

        actualDist[s] = 0;

        while(!coada.empty())
        {
            // cout << "there";
            int nodCurent = coada.front();
            coada.pop();

            for(size_t i = 0; i < G[nodCurent].size(); ++i)
            {
                if(!seen[G[nodCurent][i].first])
                {
                    actualDist[G[nodCurent][i].first] = G[nodCurent][i].second + actualDist[nodCurent];
                    coada.push(G[nodCurent][i].first);
                    seen[G[nodCurent][i].first] = 1;
                }
            }
        }

        bool keepGoing = true;

        for(int i = 1; i <= n && keepGoing; ++i)
        {
            if(actualDist[i] != bronzDist[i])
            {
                fout << "NU\n";
                keepGoing = false;
            }
        }

        if(keepGoing) fout << "DA\n";

        for(int i = 1; i <= n; ++i)
            G[i].clear();
    }
    return 0;
}