Cod sursa(job #2668419)

Utilizator SochuDarabaneanu Liviu Eugen Sochu Data 4 noiembrie 2020 21:37:18
Problema Algoritmul lui Dijkstra Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.66 kb
#include <bits/stdc++.h>
#define ll long long
#define uint unsigned long long
#define ld long double
#define eb emplace_back
#define pb push_back
#define qwerty1 first
#define qwerty2 second
#define qwerty3 -> first
#define qwerty4 -> second
#define umap unordered_map
#define uset unordered_set
#define pii pair < int , int >
#define pq priority_queue
#define dbg(x) cerr << #x << ": " << x << '\n'
#define FastIO ios_base::sync_with_stdio(false) , cin.tie(0) , cout.tie(0)
#define Files freopen("dijkstra.in" , "r" , stdin) , freopen("dijkstra.out" , "w" , stdout)

using namespace std;

const int N = 5e4 + 10;
const int M = 1e9 + 7;
const ld PI = acos(-1);
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

int n , m , u , v , cost;
set < pii > h;
vector < pii > g[N];
int d[N];

signed main()
{
	#ifndef ONLINE_JUDGE
		FastIO , Files;
    #endif

    int i;

    cin >> n >> m;

    for(i = 1 ; i <= n ; i++)
    {
        cin >> u >> v >> cost;
        g[u].pb({v , cost});
    }

    fill(d + 1 , d + n + 1 , INT_MAX);

    h.insert({0 , 1});
    d[1] = 0;

    while(!h.empty())
    {
        v = (*h.begin()).second;
        h.erase(h.begin());

        for(auto u : g[v])
        {
            int to = u.first;
            cost = u.second;

            if(d[to] > d[v] + cost)
            {
                if(d[to] != INT_MAX)
                    h.erase(h.find({d[to] , to}));

                d[to] = d[v] + cost;
                h.insert({d[to] , to});
            }
        }
    }

    for(i = 2 ; i <= n ; i++)
        cout << (d[i] == INT_MAX ? 0 : d[i]) << ' ';

    return 0;
}