Cod sursa(job #2632039)

Utilizator MatteoalexandruMatteo Verzotti Matteoalexandru Data 2 iulie 2020 02:08:49
Problema Algoritmul lui Dijkstra Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 3.19 kb
// the return
/*
                `-/oo+/-   ``
              .oyhhhhhhyo.`od
             +hhhhyyoooos. h/
            +hhyso++oosy- /s
           .yoooossyyo:``-y`
            ..----.` ``.-/+:.`
                   `````..-::/.
                  `..```.-::///`
                 `-.....--::::/:
                `.......--::////:
               `...`....---:::://:
             `......``..--:::::///:`
            `---.......--:::::////+/`
            ----------::::::/::///++:
            ----:---:::::///////////:`
            .----::::::////////////:-`
            `----::::::::::/::::::::-
             `.-----:::::::::::::::-
               ...----:::::::::/:-`
                 `.---::/+osss+:`
                   ``.:://///-.
*/
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <cmath>

using namespace std;

const int INF = 2e9;
const int N = 5e4;

vector <pair <int, int>> g[5 + N];
int dp[5 + N];
int n, m;

class Min_Heap {
    private:
        pair <int, int> h[5 + N];
        int sz;

        void Sift_Up(int pos) {
            int parent;
            parent = pos >> 1;

            while(h[pos].second < h[parent].second && parent > 0) {
                swap(h[pos], h[parent]);

                pos = parent;
                parent = pos >> 1;
            }
        }

        void Sift_Down(int pos) {
            int child;
            child = pos << 1;

            if(child + 1 <= sz && h[child].second > h[child + 1].second)
                child++;
            while(child <= sz && h[child].second < h[pos].second) {
                swap(h[pos], h[child]);

                pos = child;
                child = pos << 1;

                if(child + 1 <= sz && h[child].second > h[child + 1].second) child++;
            }
        }

    public:
        void Clear() {
            sz = 0;
        }

        void Push(pair <int, int> val) {
            h[++sz] = val;
            Sift_Up(sz);
        }

        void Pop() {
            swap(h[1], h[sz]);
            sz--;
            Sift_Down(1);
        }

        pair <int, int> Top() {
            return h[1];
        }

        int Size() {
            return sz;
        }
};
Min_Heap heap;

int main() {
    freopen("dijkstra.in", "r", stdin);
    freopen("dijkstra.out", "w", stdout);
    scanf("%d%d", &n, &m);

    for(int i = 1; i <= n; i++) dp[i] = INF;
    for(int i = 1; i <= m; i++) {
        int x, y, cost;
        scanf("%d%d%d", &x, &y, &cost);
        g[x].push_back(make_pair(y, cost));
        g[y].push_back(make_pair(x, cost));
    }

    dp[1] = 0;
    heap.Push(make_pair(1, 0));
    while(heap.Size()) {
        pair <int, int> top = heap.Top();
        heap.Pop();

        for(auto to : g[top.first]) {
            if(dp[to.first] == INF) {
                dp[to.first] = to.second + dp[top.first];
                heap.Push(to);
            }
        }
    }

    for(int i = 2; i <= n; i++) printf("%d ", dp[i]);
    printf("\n");
    fclose(stdin);
    fclose(stdout);
    return 0;
}