Cod sursa(job #2821063)

Utilizator MirunaStefaniaLupascu Miruna-Stefania MirunaStefania Data 22 decembrie 2021 08:40:23
Problema Distante Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3.7 kb
#include<bits/stdc++.h>
#define N 50005
#define inf 200000000

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


typedef pair < int, int > Pair; // define our pair for an easier use

class Graph {
private:

    int n, m;
    vector < Pair > adcost[N];  ///adcost[x] = (y,c) where c = cost for the edge from x->y

public:
    Graph() = default;
    Graph(int n  = 0, int m = 0):n(n), m(m){}
    vector<int> dijkstra(int start);
    void readUndirectedCost();
};

void Graph::readUndirectedCost() {
      for(int i = 1; i <= m; ++i) {
        int x, y, cost;
        fin >> x >> y >> cost;
        adcost[x].push_back(make_pair(y,cost));
        adcost[y].push_back(make_pair(x,cost));
    }
}

vector<int> Graph::dijkstra(int start) {
    ///O((e+v)log(v))
    ///the minimum path from a source to all others
    ///a priority queue with (cost, vertex) that keep in ascending order the minimum path to a vertex from the start
    ///at a moment in front of the queue will be a vertex whose path can not be any better
    ///take that node, itarete its neighbours and try to update path --> if one can be updated read the pair(cost, vertex) in the priority queue
    ///viz -- if we already decided the minimum path to that node
        /// --(bcs at a moment we may have entered multiple times a node in the queue) (first time with a worse price, then with a better one)

    priority_queue < Pair, vector < Pair >, greater < Pair > > queue_edges; ///add (for used edges in ascending order of their costs)
                                                                            ///pair(cost, node) where cost = source->node
                                                                            ///for DIJKSTRA
    int viz[N] = {0};
    vector<int> dist;
    dist.clear();
    dist.reserve(n+1);

    for(int i = 1; i <= n; ++i)
        dist[i] = inf;
    int source = start; //the start node
    queue_edges.push(make_pair(0, source)); ///the cost is 0 to arrive at the source
    dist[source] = 0;

    while(!queue_edges.empty())//we will add in the queue all the updated paths (cost, node) --> priority queue ordered by their costs
    {                          //at a moment we will choose the minimum cost existent
        int node = queue_edges.top().second;
        queue_edges.pop();

        if(viz[node] == 1)continue;  //skip this node bcs we already had a path to it
            else viz[node] = 1; //continue from this node

        for(int i = 0; i < adcost[node].size(); ++i)//for all nodes that "node" is connected to
        {
            int vertex = adcost[node][i].first;
            int cost = adcost[node][i].second;

            //try to update minimum cost
            if(cost + dist[node] < dist[vertex])
            {
                dist[vertex] = dist[node] + cost;
                queue_edges.push(make_pair(dist[vertex], vertex));
            }
        }
    }
    return dist;

}

void solveDijkstra() {///THE PATH WITH MINIMUM COST FROM A SPECIFIC NODE

   int t;
   fin >>  t;
   for(int k = 1 ;k <= t; ++k)
   {
   int  n, m, start;
   fin >> n >> m >> start;

   int sol[n+5];
   for(int i = 1; i <= n; ++i)
        fin >>sol[i];

   Graph g(n, m);
   g.readUndirectedCost();
   vector<int> dist = g.dijkstra(start);

   bool adev = true;

   for(int i = 1; i <= n; ++i)
        if(dist[i] == inf)
            dist[i] = 0;

   for(int i = 1; i <= n; ++i)
        if(dist[i] != sol[i])
            {
                adev = false;
                break;
            }
    if(adev == true)
        fout <<"DA\n";
    else fout <<"NU\n";
   }
}

int main() {
    solveDijkstra();
    return 0;
}