Cod sursa(job #2829814)

Utilizator robertanechita1Roberta Nechita robertanechita1 Data 8 ianuarie 2022 23:46:57
Problema Distante Scor 0
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");

vector <pair <int, int >>h[50005];
int T, n, m, s, d[50005], dist[50005], viz[50005];

void Djs(int start)
{
    for(int i = 1; i <= n; i++)
        viz[i] = 0;
    for(int i = 1; i <= n; i++)
        dist[i] = 2e9;
    priority_queue<pair<int, int> >q;
    q.push({0, start});
    dist[start] = 0;
    while(!q.empty())
    {
        int nod = q.top().second;
        int cost = q.top().first;
        q.pop();
        if(viz[nod] == 0)
        {
            viz[nod] = 1;
            for(auto j : h[nod])
                if(cost + j.second < dist[j.first])
                {
                    dist[j.first] = cost + j.second;
                    q.push({dist[j.first], j.first});
                }
        }
    }
}

int main()
{
    int a, b, c;
    fin >> T;
    for(int pas = 1; pas <= T; pas++)
    {
        fin >> n >> m >> s;
        for(int i = 1; i <= n; i++)
            fin >> d[i];
        for(int i = 1; i <= m; i++)
        {
            fin >> a >> b >> c;
            h[a].push_back({b, c});
            h[b].push_back({a, c});
        }
        Djs(s);
        int i = 1, v = 1;
        for( ; i <= n; i++)
            if(d[i] != dist[i])
                v = 0;
        if(v == 0)
            fout << "NU\n";
        else
            fout << "DA\n";

    }
    return 0;
}