Cod sursa(job #3251508)

Utilizator devilexeHosu George-Bogdan devilexe Data 26 octombrie 2024 10:20:20
Problema Algoritmul lui Dijkstra Scor 60
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.08 kb
#include <bits/stdc++.h>
using namespace std;

ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
#define ll unsigned long long
int N, M;
const ll MAXN = 5e4, INF = 0x3f3f3f3f3f3f3f3fULL;
vector<pair<int, ll>> G[MAXN + 1];
ll d[MAXN + 1];

int main()
{
    fin >> N >> M;
    int a, b, c;
    while (M--)
    {
        fin >> a >> b >> c;
        G[a].emplace_back(b, c);
    }
    for (int i = 2; i <= N; i++)
        d[i] = INF;
    priority_queue<pair<ll, int>> Q;
    Q.emplace(0, 1);
    while (!Q.empty())
    {
        pair<ll, int> n = Q.top();
        Q.pop();
        ll cost;
        int node;
        tie(cost, node) = n;
        if (cost != d[node])
            continue;
        for (const auto &x : G[node])
        {
            ll xcost;
            int xnode;
            tie(xnode, xcost) = x;
            if (d[node] + xcost < d[xnode])
            {
                d[xnode] = d[node] + xcost;
                Q.emplace(d[xnode], xnode);
            }
        }
    }
    for (int i = 2; i <= N; i++)
    {
        fout << (d[i] == INF ? 0 : d[i]) << ' ';
    }
    return 0;
}