Cod sursa(job #2642019)

Utilizator TheGodFather2131Alexandru Miclea TheGodFather2131 Data 13 august 2020 13:39:19
Problema Algoritmul lui Dijkstra Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.76 kb
//ALEXANDRU MICLEA

#include <vector>
#include <algorithm>
#include <string>
#include <queue>
#include <map>
#include <set>
#include <unordered_map>
#include <time.h>
#include <iomanip>
#include <deque>
#include <math.h>
#include <cmath>
#include <assert.h>
#include <stack>
#include <bitset>
#include <random>
#include <chrono>

using namespace std;

//#include <iostream>
#include <fstream>
//ifstream fin("input.in"); ofstream fout("output.out");
ifstream fin("dijkstra.in"); ofstream fout("dijkstra.out");

//VARIABLES

class cmp {
public:

    bool operator () (pair<int, int>& a, pair<int, int>& b) {
        return a.second > b.second;
    }
};

const int MAXM = 50005;

int dp[MAXM];
int vis[MAXM];

vector < vector <pair <int, int> > > gr(MAXM);

priority_queue < pair <int, int>, vector <pair <int, int> >, cmp> Q;



//FUNCTIONS



//MAIN
int main() {

    int n, m;

    fin >> n >> m;

    for (int i = 2; i <= n; i++) {
        dp[i] = 1e9;
    }

    for (int i = 1; i <= m; i++) {
        int a, b, val;

        fin >> a >> b >> val;

        gr[a].push_back({ b, val });
    }

    Q.push({ 1, 0 });

    while (!Q.empty()) {

        pair <int, int> now = Q.top();
        Q.pop();

        //DEBUG

        //if (now.second != dp[now.first]) continue;
        if (vis[now.first]) continue;
        vis[now.first] = true;

        for (auto& x : gr[now.first]) {

            if (dp[x.first] > now.second + x.second) {
                dp[x.first] = now.second + x.second;
                Q.push({ x.first, dp[x.first]});
            }
        }
    }

    for (int i = 2; i <= n; i++) {
        if (dp[i] == 1e9) fout << "0 ";
        else fout << dp[i] << " ";
    }

    return 0;
}