Cod sursa(job #1933086)

Utilizator BaweeLazar Vlad Bawee Data 20 martie 2017 13:44:43
Problema Algoritmul lui Dijkstra Scor 90
Compilator cpp Status done
Runda Arhiva educationala Marime 1.18 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[50001];
int x , y , c , n , m;
int d[50001];

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));
       // G[y].push_back(make_pair(x,c));
    }

    int startNode = 1;
    for(int i = 1; i <= n; ++i) d[i] = 1 << 25;
    d[startNode] = 0;

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

        for(const auto &iter : G[node])
            if(d[iter.first] > d[node] + iter.second)
            {
                d[iter.first] = d[node] + iter.second;
                heap.push_back(iter.first);
                push_heap(heap.begin() , heap.end() , cmp);
            }
    }

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

    return 0;
}