Cod sursa(job #2829761)

Utilizator vlad_dimaVlad Dima vlad_dima Data 8 ianuarie 2022 22:23:45
Problema Distante Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.89 kb
#include <bits/stdc++.h>
 
using namespace std;
 
class ComparePair
{
public:
    bool operator() (pair<int,int> a, pair<int,int> b)
    {
        return a.second > b.second;   // compar costurile
    }
};
 
int main()
{
    ifstream in("distante.in");
    ofstream out("distante.out");
    int T, N, M, S;
 
    in >> T;
 
    for (int i = 0; i < T; i++)
    {   
        in >> N >> M >> S;
        vector<int> distFoaie, distMele(N+1);
        vector<vector<pair<int,int>>> lsAdCost(N+1);
 
        for (int j = 0; j < N; j++)
            {
                int x;
                in >> x;
                distFoaie.push_back(x);
            }
 
        for (int j = 0; j < M; j++)
        {
            int x, y, c;
            in >> x >> y >> c;
            lsAdCost[x].push_back({y,c});
            lsAdCost[y].push_back({x,c});
        }
 
        priority_queue<pair<int,int>,vector<pair<int,int>>,ComparePair> heap; //folosesc un priority queue drept heap
 
        vector<bool> vizitat(N+1, false);
 
        heap.push(make_pair(S,0));
        vizitat[S] = true;
 
        while (!heap.empty())
        {
            pair<int,int> tupluCrt = heap.top(); //in tuplu am nodul si costul catre nod
            heap.pop();

            distMele[tupluCrt.first] = tupluCrt.second;

            for (auto vecin : lsAdCost[tupluCrt.first])
            {
                //pentru fiecare vecin il adaug in heap cu costul curent plus costul muchiei
                if (!vizitat[vecin.first])
                    {
                        heap.push(make_pair(vecin.first,tupluCrt.second + vecin.second));
                        vizitat[vecin.first] = true;
                    }
            }
        }
        distMele.erase(distMele.begin());
        if (distMele==distFoaie)
            out << "DA\n";
        else
            out << "NU\n";
    }
 
    
}