Cod sursa(job #2476628)

Utilizator uvIanisUrsu Ianis Vlad uvIanis Data 19 octombrie 2019 10:39:58
Problema Distante Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2 kb
#include <bits/stdc++.h>
using namespace std;

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


int T, N, M, S;
vector<vector<pair<int, int>>> graph(50001, vector<pair<int, int>>());
vector<int> wall_dist(50001);

void read_test_data()
{
    fin >> N >> M >> S;

    for(int i = 1; i <= N; ++i)
    {
        fin >> wall_dist[i];
    }

    fill(graph.begin(), graph.end(), vector<pair<int, int>>());

    for(int i = 1; i <= M; ++i)
    {
        int x, y, c;
        fin >> x >> y >> c;

        graph[x].push_back(make_pair(y, c));
        graph[y].push_back(make_pair(x, c));
    }
}

vector<int> dist(50001);
vector<int> visited(50001);

struct comp{
    bool operator () (pair<int, int>& A, pair<int, int>& B)
    {
        if(A.second > B.second) return true;
        return false;
    }
};

void dijkstra()
{
    fill(dist.begin(), dist.end(), 2000000000);
    fill(visited.begin(), visited.end(), false);

    priority_queue<pair<int,int>, vector<pair<int,int>>, comp> pq;

    dist[S] = 0;
    pq.push(make_pair(S, 0));

    while(!pq.empty())
    {
        int node = pq.top().first;
        pq.pop();

        if(visited[node] == true) continue;

        visited[node] = true;

        for(auto& next : graph[node])
        {
            if(visited[next.first] == true) continue;

            if(dist[node] + next.second < dist[next.first])
            {
                dist[next.first] = dist[node] + next.second;

                pq.push(make_pair(next.first, dist[next.first]));
            }
        }
    }
}


int main()
{
    fin >> T;

    for(int t = 1; t <= T; ++t)
    {
        read_test_data();
        dijkstra();

        bool ok = true;

        for(int i = 1; i <= N; ++i)
        {
            if(dist[i] != wall_dist[i])
            {
                ok = false;
                break;
            }
        }

        if(ok == true) fout << "DA" << '\n';
        else fout << "NU" << '\n';
    }
}