Cod sursa(job #2808248)

Utilizator MihaiLazar26Lazar Mihai MihaiLazar26 Data 24 noiembrie 2021 19:29:19
Problema Algoritmul lui Dijkstra Scor 90
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 3.01 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <stack>
#include <algorithm>

using namespace std;

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

#define NMAX 100001
#define MMAX 1000005

class Graf{
    vector<pair<int, int>> nodCosturi[NMAX];
    int nrNoduri, nrMuchi;
    bool orientat = false, neorientat = false;
    int id;


    typedef pair<int, pair<int, int>> costMuchi;

    void setOrientat(bool orientat = true){
        if(orientat) this->orientat = true, this->neorientat = false;
        else this->orientat = false, this->neorientat = true;
    }

    void setNeorientat(bool neorientat = true){
        if(neorientat) this->neorientat = true, this->orientat = false;
        else this->neorientat = false, this->orientat = true;
    }

    void actDijkstra(int S, vector<bool> &viz, vector<int> &dist,
                     priority_queue<costMuchi, vector<costMuchi>, greater<costMuchi>> &pqMuchi);

public:


    void citireCosturi(int nrNoduri, int nrMuchi, vector<pair<int, pair<int, int>>> muchi, bool orientat = true){
        if(orientat) setOrientat();
        else setNeorientat();
        this->nrNoduri = nrNoduri;
        this->nrMuchi = nrMuchi;
        for(auto i : muchi){
            int nodMe = i.second.first;
            int nodT = i.second.second;
            int cost = i.first;

            this->nodCosturi[nodMe].push_back(make_pair(nodT, cost));
            if(this->neorientat) this->nodCosturi[nodT].push_back(make_pair(nodMe, cost));
        }
    }


    vector<int> dijkstra();
};
Graf graf;

void Graf::actDijkstra(int S, vector<bool> &viz, vector<int> &dist,
                     priority_queue<costMuchi, vector<costMuchi>, greater<costMuchi>> &pqMuchi){
    viz[S] = true;

    for(auto vecini : nodCosturi[S]){
        int nod = vecini.first;
        int cost = vecini.second;

        if(!viz[nod]){
            pqMuchi.push(make_pair(dist[S] + cost, make_pair(S, nod)));
        }
    }

    while(!pqMuchi.empty()){
        auto top = pqMuchi.top();
        pqMuchi.pop();

        int target = top.second.second;
        if(!viz[target]){
            dist[target] = top.first;
            actDijkstra(target, viz, dist, pqMuchi);
        }
    }
}


vector<int> Graf::dijkstra(){
    vector<bool> viz;
    viz.assign(nrNoduri+1, false);

    vector<int> dist;
    dist.assign(nrNoduri+1, 0);

    priority_queue<costMuchi, vector<costMuchi>, greater<costMuchi>> pqMuchi;

    actDijkstra(1, viz, dist, pqMuchi);

    return dist;
}



void rezolvareDijkstra(){
    int n, m;
    vector<pair<int, pair<int, int>>> muchi;

    fin>>n>>m;

    for(int i = 1; i <= m; i++){
        int a,b,c;
        fin>>a>>b>>c;

        muchi.push_back(make_pair(c, make_pair(a,b)));
    }

    graf.citireCosturi(n, m, muchi);

    auto res = graf.dijkstra();

    for(int i = 2; i<=n; i++)
        fout<<res[i]<<" ";
}

int main(){

    rezolvareDijkstra();
}