Cod sursa(job #2677995)

Utilizator FrostfireMagirescu Tudor Frostfire Data 27 noiembrie 2020 22:06:59
Problema Distante Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.88 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#define NMAX 50000
#define inf 2000000000

using namespace std;

ifstream f("distante.in");
ofstream g("distante.out");

int t, ans[NMAX+10], dist[NMAX+10], viz[NMAX+10];
vector <pair <int, int> > nod[NMAX+10];
priority_queue <pair <int, int>, vector <pair <int, int> >, greater <pair <int, int> > > pq;

int main()
{
    f >> t;
    while(t--)
        {   int n, m, s, ok = 1;
            f >> n >> m >> s;
            for(int i=1; i<=n; i++) f >> ans[i], nod[i].clear(), dist[i] = inf, viz[i] = 0;
            for(int i=1; i<=m; i++)
                {   int nod1, nod2, cost;
                    f >> nod1 >> nod2 >> cost;
                    nod[nod1].push_back({cost, nod2});
                    nod[nod2].push_back({cost, nod1});
                }
            while(!pq.empty()) pq.pop();
            pq.push({0, s});
            dist[s] = 0;
            while(!pq.empty())
                {   pair <int, int> x = pq.top();
                    pq.pop();
                    if(viz[x.second]) continue;
                    viz[x.second] = 1;
                    if(ans[x.second] != x.first)
                        {   ok = 0;
                            break;
                        }
                    for(int i=0; i<nod[x.second].size(); i++)
                        if(!viz[nod[x.second][i].second])
                            {   int d = x.first + nod[x.second][i].first;
                                if(d < dist[nod[x.second][i].second])
                                    {   dist[nod[x.second][i].second] = d;
                                        pq.push({d, nod[x.second][i].second});
                                    }
                            }
                }
            if(ok) g << "DA" << '\n';
            else g << "NU" << '\n';
        }
    return 0;
}