Cod sursa(job #2908149)

Utilizator Costin_PintilieHoratiu-Costin-Petru Pintilie Costin_Pintilie Data 1 iunie 2022 18:21:02
Problema Distante Scor 60
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3.02 kb
#include <bits/stdc++.h>
using namespace std;


class minHeapComp{
public:
    bool operator()(pair<int, int> n1, pair<int, int> n2){
        return n1.second > n2.second;
    }
};

struct Edge{
    int src;
    int dest;
    int cost;

    Edge(int src, int dest, int cost){
        this->src = src;
        this->dest = dest;
        this->cost = cost;
    }
};

vector<int> Djikstra(int node_nbr, int src_node, vector<vector<Edge>> adj){
    priority_queue<pair<int,int>, vector<pair<int,int>>,minHeapComp > heap;
    vector<bool> visited(node_nbr + 1, 0);
    vector<int> my_dist(node_nbr + 1, 0);

    heap.push({src_node,0});

    while(!heap.empty()){
        pair<int, int> curr_node = heap.top();
        heap.pop();

        if(!visited[curr_node.first]){
            visited[curr_node.first] = 1;
            my_dist[curr_node.first] = curr_node.second;

            for(auto next_node: adj[curr_node.first]){
                heap.push({next_node.dest, curr_node.second + next_node.cost});
            }
        }
    }

    return my_dist;

}

int graphs_nbr, node_nbr, edge_nbr, src_node, dist, src, dest, cost;
vector<int> distances;
vector<vector<Edge>> adj;

int main()
{
    ifstream fin("distante.in");
    ofstream fout("distante.out");
    fin>>graphs_nbr; //T
    for (int i = 0; i < graphs_nbr; i++){

        fin>>node_nbr>>edge_nbr>>src_node; //N,M,S

        distances.assign(node_nbr + 1,0);
        adj.resize(node_nbr + 1);

        for(int j = 1; j <= node_nbr; j++){

            fin>>dist;
            distances[j] = dist;
        }


        for(int j = 0; j < edge_nbr; j++){
            fin>>src>>dest>>cost;
            Edge e(src, dest, cost);
            adj[e.src].push_back(e);

            Edge e1(dest, src, cost);
            adj[e1.src].push_back(e1);
        }

//        cout<<"info graf:\n"<<node_nbr<<" "<<edge_nbr<<" "<<src_node<<'\n';

        vector<int> my_dist = Djikstra(node_nbr, src_node, adj);
//        cout<<"muchii:\n";
//        for(auto it: adj){
//            for(auto e: it){
//                cout<<e.src<<' '<<e.dest<<' '<<e.cost<<'\n';
//            }
//        }
//
//        cout<<"distante date:\n";
//        for(auto it: distances){
//            cout<<it<<' ';
//        }
//        cout<<'\n';
//
//        cout<<"distante calculate:\n";
//        for(auto it: my_dist){
//            cout<<it<<' ';
//        }
//        cout<<'\n';
//        cout<<'\n';


//        if(equal(distances.begin(),distances.end(),my_dist.begin())){
//            fout<<"DA\n";
//
//        }else{
//
//            fout<<"NU\n";
//        }
        bool ok = 1;
        for(int j = 1; j <= node_nbr; j++){
            if(my_dist[j] != distances[j]){
                ok = 0;
                break;
            }
        }
        if(ok)
            fout<<"DA\n";
        else
            fout<<"NU\n";

        adj.clear();
        my_dist.clear();
    }

    fin.close();
    fout.close();

    return 0;
}