Cod sursa(job #1433992)

Utilizator EpictetStamatin Cristian Epictet Data 10 mai 2015 11:34:42
Problema Algoritmul lui Dijkstra Scor 80
Compilator cpp Status done
Runda Arhiva educationala Marime 1.16 kb
#include <fstream>
#include <vector>
#include <set>
#include <cstring>
#define Max_Size 50010
#define Max_Val 0x3f3f3f3f
#define nod first
#define cost second
using namespace std;
ifstream fin ("dijkstra.in");
ofstream fout ("dijkstra.out");
typedef pair < int, int > NOD;
int N, M, D[Max_Size];
vector < NOD > V[Max_Size];
multiset < NOD > Heap;

int main()
{
    fin >> N >> M;
    for (int i = 1, x, y, c; i <= M; i++)
    {
        fin >> x >> y >> c;
        V[x].push_back( { y, c } );
    }

    memset(D, Max_Val, sizeof(D));
    D[1] = 0;
    Heap.insert( { 1, 0 } );

    while (!Heap.empty())
    {
        NOD aux = *Heap.begin();
        Heap.erase(aux);

        vector < NOD > :: iterator it;
        for (it = V[aux.nod].begin(); it != V[aux.nod].end(); it++)
        {
            if (D[aux.nod] + it->cost < D[it->nod])
            {
                Heap.erase( { it->nod, D[it->nod] } );
                D[it->nod] = D[aux.nod] + it->cost;
                Heap.insert( { it->nod, D[it->nod] } );
            }
        }
    }

    for (int i = 2; i <= N; i++) {
        fout << (D[i] == Max_Val ? 0 : D[i]) << ' ';
    }
    fout << '\n';
    fout.close();
    return 0;
}