Cod sursa(job #2562573)

Utilizator PatrascuAdrian1Patrascu Adrian Octavian PatrascuAdrian1 Data 29 februarie 2020 15:40:16
Problema Distante Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.65 kb
#include <bits/stdc++.h>

using namespace std;

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

using pii = pair<int,int>;
const int INF = 1e9 + 5;

int main()
{
    ios_base::sync_with_stdio(0);
    in.tie(0);
    int T,N,M,start;
    in >> T;
    while(T--)
    {
        in >> N >> M >> start;

        vector<int> maybe_sp(N + 1);

        for(int i = 1; i <= N; ++i)
            in >> maybe_sp[i];

        vector<pii> G[N + 1];
        int x, y, c;

        while(M--)
        {
            in >> x >> y >> c;
            G[x].push_back({y,c});
            G[y].push_back({x,c});
        }

        vector<int> dp(N + 1, INF);

priority_queue <pii,vector<pii>,greater<pii> > Q;

        for(auto i : G[start])
        {
            dp[i.first] = i.second;
            Q.push({dp[i.first],i.first});
        }

        vector<bool> seen(N + 1);

        while(!Q.empty())
        {
            while(!Q.empty() && seen[Q.top().second])
                Q.pop();
            if(!Q.empty())
            {
                int node = Q.top().second;
                seen[node] = 1;
                Q.pop();
                for(auto i : G[node])
                    if(dp[node] + i.second < dp[i.first])
                    {
                        dp[i.first] = dp[node] + i.second;
                        Q.push({dp[i.first],i.first});
                    }
            }
        }

        bool OK = true;
        dp[start] = 0;
        for(int i = 1; i <= N && OK; ++i)
            if(dp[i] != maybe_sp[i])
                OK = false;

        out << (OK ? "DA" : "NU") << '\n';
    }
    return 0;
}