Cod sursa(job #2244810)

Utilizator TheNextGenerationAyy LMAO TheNextGeneration Data 23 septembrie 2018 18:56:11
Problema Distante Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.45 kb
#include <bits/stdc++.h>

using namespace std;
ifstream in("distante.in");
ofstream out("distante.out");
typedef pair<int,int> pii;
const int NMAX = 50005;
int dist[NMAX],test[NMAX];
vector<pii>v[NMAX];
priority_queue<pii, vector<pii>, greater<pii> > q;

int main()
{
    int t;
    in >> t;
    while (t--)
    {
        memset(dist,-1,sizeof(dist));
        memset(test,0,sizeof(test));
        int n,m,s;
        in >> n >> m >> s;
        for (int i = 1; i<=m; i++)
            v[i].clear();
        for (int i = 1; i<=n; i++)
            in >> test[i];
        for (int i = 1; i<=m; i++)
        {
            int x,y,z;
            in >> x >> y >> z;
            v[x].push_back({y,z});
            v[y].push_back({x,z});
        }
        q.push({0,s});
        dist[s] = 0;
        while (!q.empty())
        {
            int now = q.top().second;
            for (auto it: v[now])
            {
                int next = it.first;
                int cost = it.second;
                if (dist[next] == -1 || dist[next]>dist[now]+cost)
                {
                    dist[next] = dist[now]+cost;
                    q.push({dist[next],next});
                }
            }
            q.pop();
        }
        bool ok = 1;
        for (int i = 1; i<=n; i++)
            if (test[i] != dist[i])
                ok = 0;
        if (ok)
            out << "DA\n";
        else
            out << "NU\n";
    }
}