Cod sursa(job #2166320)

Utilizator IonelChisIonel Chis IonelChis Data 13 martie 2018 16:36:02
Problema Distante Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.66 kb
#include <fstream>
#include <queue>
#include <vector>
#include <cstring>

#define Nmax 50002
#define cost first
#define nod second

using namespace std;

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

int t, n, m, s, x, y, i, c, v[Nmax], C[Nmax];
vector < pair <int, int> > G[Nmax];
vector < pair <int, int> > :: iterator it;
priority_queue < pair <int, int>, vector < pair <int, int> >, greater < pair <int, int> > > pq;
pair <int, int> aux;
bool ok;

int main()
{
    fin >> t;
    while (t)
    {
        fin >> n >> m >> s;
        for (i = 1; i <= n; i ++)
            fin >> v[i];
        for (i = 1; i <= m; i ++)
        {
            fin >> x >> y >> c;
            G[x].push_back ({c, y});
            G[y].push_back ({c, x});
        }
        pq.push({0, s});


        while (!pq.empty())
        {
            aux = pq.top();
            pq.pop();
            if (!C[aux.nod])
            {
                C[aux.nod] = aux.cost;
                for (it = G[aux.nod].begin(); it != G[aux.nod].end(); it ++)
                {
                    if (!C[(*it).nod])
                    pq.push ({aux.cost + (*it).cost, (*it).nod});
                }
            }
        }

        C[s] = 0;
        ok = true;
        for (i = 1; i <= n && ok; i ++)
        {
            if (C[i] != v[i])
                ok = false;
        }
        if (ok)
            fout << "DA" << '\n';
        else
            fout << "NU" << '\n';

        memset (C, 0, sizeof(C));
        //for (i = 1; i <= n; i ++)
            //G[i].clear();
        t --;
    }


    fin.close();
    fout.close();
    return 0;
}