Cod sursa(job #2827904)

Utilizator faalaviaFlavia Podariu faalavia Data 6 ianuarie 2022 15:47:21
Problema Distante Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.25 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <climits>
using namespace std;

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

struct weightedEdge
{
    int neighbour;
    int cost;
};

int t, n, m, x, y, d, cost, start;

vector<int> minCost;
vector<vector<weightedEdge>> edges;

void Dijkstra(const int& start, vector<int>& minCost)
{
    vector<bool> extracted(n+1, false);
    priority_queue<pair<int, int>,vector<pair<int, int>>,greater<>>edgePQ;
    minCost[start] = 0;
    edgePQ.push(make_pair(0, start)); //cost has to be first for greater<> to work

    while(!edgePQ.empty())
    {
        int node = edgePQ.top().second;
        edgePQ.pop();

        if(extracted[node])
            continue;

        extracted[node] = true;

        for(auto& it: edges[node])
        {
            int neighbour = it.neighbour;
            int cost = it.cost;
            if(minCost[node] + cost < minCost[neighbour])
            {
                minCost[neighbour] = minCost[node] + cost;
                edgePQ.push(make_pair(minCost[neighbour], neighbour));
            }
        }
    }
}




int main()
{
    fin >> t;
    while(t--)
    {
        fin >> n >> m >> start;

        vector<int> bronzarel;
        minCost.resize(n+1, INT_MAX);

        edges.resize(n+1);

        bool ok = false;

        for(int i = 0; i < n; i++)
        {
            fin >> d;
            bronzarel.push_back(d);
        }

        for(int i = 0; i < m; i++)
        {
            fin >> x >> y >> cost;

            weightedEdge tmp;
            tmp.neighbour = y;
            tmp.cost = cost;
            edges[x].push_back(tmp);

            tmp.neighbour = x;
            edges[y].push_back(tmp);
        }

        Dijkstra(start, minCost);

        for(int i = 1 ; i <= n; i++)
        {
            if(minCost[i] == INT_MAX)
                minCost[i] = 0;

            if(minCost.at(i) != bronzarel.at(i-1))
            {
                fout << "NU\n";
                ok = true;
                break;
            }
        }
        if(!ok)
            fout << "DA\n";

        minCost.clear();
        edges.clear();

    }
    return 0;
}