Cod sursa(job #2546673)

Utilizator radumihaisirbuSirbu Radu-Mihai radumihaisirbu Data 14 februarie 2020 13:05:33
Problema Distante Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.43 kb
#include <bits/stdc++.h>

using namespace std;

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

#define pb push_back

const int nmax = 5e4 + 3;

queue <int> q;

int d[nmax], a[nmax];

int n, m, s, x, y, p;

struct muchie
{
    int End, cost;
}A;

vector <muchie> v[nmax];

void read ()
{
    fin >> n >> m >> s;

    for (int i=1; i<=n; ++i) fin >> a[i];

    while (m--)
    {
        fin >> x >> y >> A.cost;
        A.End = y;
        v[x].pb(A);
        A.End = x;
        v[y].pb(A);
    }
}

void bfs ()
{
    int nod;

    muchie curent;

    q.push(s);
    d[s] = 0;

    while(!q.empty())
    {
        nod = q.front();
        q.pop();

        for (int i=0; i<v[nod].size(); ++i)
        {
            curent = v[nod][i];

            if (d[curent.End] == 0 && curent.End != s || d[nod] + curent.cost < d[curent.End])
            {
                d[curent.End] = d[nod] + curent.cost;
                q.push(curent.End);
            }
        }

    }
}

void solve ()
{
    bool ok = false;

    bfs();

    for (int i=1; i<=n; ++i)
        if (a[i] != d[i]) ok = 1;

    if (ok) fout << "NU\n";
    else fout << "DA\n";
}

void reset ()
{
    for (int i=1; i<=n; ++i) v[i].clear();
    memset(d, 0, sizeof(d));
}

int main()
{
    fin >> p;
    while (p--)
    {
        read();
        solve();
        reset();
    }
    return 0;
}