Cod sursa(job #1605024)

Utilizator BaweeLazar Vlad Bawee Data 18 februarie 2016 18:52:18
Problema Algoritmul lui Dijkstra Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 1.16 kb
#include <fstream>
#include <vector>
#include <algorithm>

using namespace std;

ifstream f("dijkstra.in");
ofstream g("dijkstra.out");

vector<int> heap;
vector< pair< int , int > > G[10001];
int d[10001] , n , m , x , y , c;

bool cmp(const int& a , const int& b)
{
    return d[a] > d[b];
}

int main()
{
    f >> n >> m;
    for(int i = 1; i <= m; ++i)
    {
        f >> x >> y >> c;
        G[x].push_back(make_pair(y,c));
    }

    for(int i = 2; i <= n; ++i) d[i] = 1 << 20;

    heap.push_back(1);
    //make_heap(heap.begin() , heap.end() , cmp);

    while(heap.size())
    {
        int nod = heap.front();
        pop_heap(heap.begin() , heap.end() , cmp);
        heap.pop_back();

        for(auto it = G[nod].begin(); it != G[nod].end(); ++it)
            if(d[it -> first] > d[nod] + it -> second)
            {
                d[it -> first] = d[nod] + it -> second;
                heap.push_back(it -> first);
                push_heap(heap.begin() , heap.end() , cmp);
            }
    }

    for(int i = 2; i <= n; ++i)
    {
        if(d[i] == 1 << 20) d[i] = 0;
        g << d[i] << " ";
    }
    return 0;
}