Cod sursa(job #1795393)

Utilizator Alexa2001Alexa Tudose Alexa2001 Data 2 noiembrie 2016 12:31:18
Problema Algoritmul lui Dijkstra Scor 80
Compilator cpp Status done
Runda Arhiva educationala Marime 0.97 kb
#include <cstdio>
#include <queue>
#define Pair pair<int,int>

using namespace std;

const int NM = 5e4+5;
const int inf = (1<<30);

int A,B,C, n,m, dist[NM], i, node;
vector< Pair > v[NM];
priority_queue< Pair, vector< Pair >, greater< Pair > > heap;

int main()
{
    freopen("dijkstra.in", "r", stdin);
    freopen("dijkstra.out", "w", stdout);

    scanf("%d%d", &n, &m);

    for(i=1; i<=m; ++i)
    {
        scanf("%d%d%d", &A, &B, &C);
        v[A].push_back({B,C});
    }

    for(i=2; i<=n; ++i) dist[i] = inf;

    heap.push({0,1});

    while(heap.size())
    {
        node = heap.top().second;
        heap.pop();

        for(auto it : v[node])
            if(dist[it.first] >= dist[node] + it.second)
            {
                dist[it.first] = dist[node] + it.second;
                heap.push({ dist[it.first], it.first });
            }
    }

    for(i=2; i<=n; ++i) printf("%d ", dist[i]==inf ? 0 : dist[i] );

    return 0;
}