Cod sursa(job #2556447)

Utilizator radumihaisirbuSirbu Radu-Mihai radumihaisirbu Data 24 februarie 2020 21:42:14
Problema Distante Scor 40
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.63 kb
#include <bits/stdc++.h>

using namespace std;

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

#define pb push_back

const int nmax = 5e4 + 2;

struct muchie
{
    int End, cost;
}a;

vector <muchie> v[nmax];

bitset <nmax> viz;

priority_queue <pair <int, int>, vector <pair<int, int> >, greater <pair <int, int> > > q;

vector <int> dist(nmax, 2e9);

bool solution = true;

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

int d[nmax];

void read ()
{
    fin >> n >> m >> s;
    for (int i=1; i<=n; ++i) fin >> d[i];
    while(m--)
    {
        fin >> x >> y >> a.cost;
        a.End = y;
        v[x].pb(a);
        a.End = x;
        v[y].pb(a);
    }
}

void reset ()
{
    viz.reset();
    for (int i=1; i<=n; ++i) v[i].clear(), dist[i] = 2e9;
}

void write ()
{
    if (solution) fout << "DA\n";
    else fout << "NU\n";
}

void solve ()
{
    int nod;

    q.push(make_pair(0, s));
    dist[s] = 0;

    while(!q.empty())
    {
        nod = q.top().second;
        viz[nod] = 1;
        q.pop();
        for (int i=0; i<v[nod].size(); ++i)
        {
            a = v[nod][i];
            if (dist[nod] + a.cost < dist[a.End])
            {
                dist[a.End] = dist[nod] + a.cost;
                if (viz[a.End] == 0)
                {
                    q.push(make_pair(dist[a.End], a.End));
                }
            }
        }
    }
    for (int i=1; i<=n; ++i)
        if (dist[i] != d[i]) solution = false;
}

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