Cod sursa(job #2609985)

Utilizator daniel.stefanStoica Daniel daniel.stefan Data 3 mai 2020 22:14:28
Problema Algoritmul lui Dijkstra Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 3.16 kb
#include <iostream>
#include <fstream>
#include <assert.h>
#include <vector>
#define DIM 50010
#define INF 100000000

using namespace std;

ifstream fin ("dijkstra.in");
ofstream fout ("dijkstra.out");

int nodes, connections, D[DIM], pos[DIM], nr_elem;
vector< pair<int,int> > list_of_adjacents[DIM];
int heap[DIM];

inline int father(int node_index){
    return node_index / 2;
}

inline int left_son(int node_index){
    return 2 * node_index;
}

inline int right_son(int node_index){
    return 2 * node_index + 1;
}

void up(int index){
    bool flag = true;

    while(flag){
        if(index == 1 || D[heap[index]] >= D[heap[father(index)]]){
            flag = false;
        }
        else{
            swap(heap[index], heap[father(index)]);
            pos[heap[index]] = index;
            pos[heap[father(index)]] = father(index);
            index = father(index);
        }
    }
}

void down(int index){

    int son;

    do{
        son = 0;

        if(left_son(index) <= nr_elem){
            son = left_son(index);

            if(right_son(index) <= nr_elem && D[heap[right_son(index)]] < D[heap[left_son(index)]])
                son = right_son(index);
            if(D[son] >= D[index])
                son = 0;
        }

        if(son){
            swap(heap[index], heap[son]);
            pos[heap[son]] = son;
            pos[heap[index]] = index;
            index = son;
        }

    }while(son);

}


void insert_element(int insert_elem){
    int index = nr_elem;

    heap[index] = insert_elem;

    up(index);
}

void delete_element(int delete_elem){
    heap[delete_elem] = heap[nr_elem--];
    pos[heap[delete_elem]] = delete_elem;

    int position = delete_elem;

    if(position > 1  && D[heap[position]] < D[heap[father(position)]])
        up(position);
    else
        down(position);
}

int main()
{
    int Anode, Bnode, cost;

    fin >> nodes;
    assert(1 <= nodes && nodes <= 50000);

    fin >> connections;
    assert(1 <= connections && connections <= 250000);

    for(int i = 1; i <= connections; i++){
        fin >> Anode >> Bnode >> cost;
        list_of_adjacents[Anode].push_back(make_pair(Bnode, cost));
    }

    D[1] = 0;
    pos[1] = ++nr_elem;
    insert_element(1);

    for(int i = 2; i <= nodes; i++){
        D[i] = INF;
        pos[i] = ++nr_elem;
        insert_element(i);
    }

    for(int i = 1; i <= nodes; i++){
        int min_node = heap[1];
        delete_element(1);

        for(int j = 0; j < list_of_adjacents[min_node].size(); j++){
            int cost = list_of_adjacents[min_node][j].second;
            int current_node = list_of_adjacents[min_node][j].first;
            if(D[min_node] + cost < D[current_node]){
                D[current_node] = D[min_node] + cost;
                int index = current_node;
                if(D[index] < D[father(index)]){
                    up(index);
                }
            }
        }

    }

    for(int i = 2; i <= nodes; i++){
        if(D[i] == INF)
            fout << "0 ";
        else
            fout << D[i] << " ";
    }

    return 0;
}