Cod sursa(job #1537739)

Utilizator rockerboyHutter Vince rockerboy Data 27 noiembrie 2015 21:34:34
Problema Algoritmul lui Dijkstra Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 3.58 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <list>
#include <climits>

#define NODE(n) (n>>10)
#define COST(n) (n&1023)

template <class elem_t, class Compare = std::less<int> >
class heap
{
    std::vector<int> pos;
    std::vector<elem_t> x;
    Compare comp;

    bool leaf (unsigned p)
    {
        if (p <= (x.size()-1)/2) return false;
        return true;
    }

    void upheap (unsigned currentpos)
    {
        while (comp (x[currentpos].key(), x[currentpos/2].key()) && currentpos != 1) {
            std::swap (x[currentpos], x[currentpos/2]);
            pos[x[currentpos].num()] = currentpos;
            pos[x[currentpos/2].num()] = currentpos/2;
            currentpos /= 2;
        }
    }

    void downheap (unsigned currentpos)
    {
        unsigned minpos;

        while (not leaf (currentpos)) {
            minpos = currentpos;
            if (not comp (x[minpos].key(), x[currentpos*2].key())) {
                minpos = currentpos*2;
            }
            if (currentpos*2+1 <= x.size()-1) {
                if (not comp (x[minpos].key(), x[currentpos*2+1].key())) {
                    minpos = currentpos*2+1;
                }
            }
            if (minpos == currentpos) break;
            std::swap (x[currentpos], x[minpos]);
            pos[x[currentpos].num()] = currentpos;
            pos[x[minpos].num()] = minpos;
            currentpos = minpos;
        }
    }

public:
    heap (unsigned n): pos(n, -1), x(1) {}
    ~heap () {pos.clear(); x.clear();}

    void push (elem_t elem)
    {
        x.push_back (elem);
        unsigned currentpos = x.size()-1;
        pos[elem.num()] = currentpos;
        upheap (currentpos);
    }

    elem_t top ()
    {
        return x[1];
    }

    unsigned size ()
    {
        return x.size()-1;
    }

    bool exists (unsigned p)
    {
        if (pos[p] == -1) return false;
        return true;
    }

    void pop ()
    {
        pos[x[1].num()] = -1;
        x[1] = x.back();
        pos[x[1].num()] = 1;
        x.pop_back();
        downheap (1);
    }

    void change_key (int node, int val)
    {
        x[pos[node]].set_key(val);
        upheap (pos[node]);
    }
};

class tipus
{
    int sorszam, kulcs;

public:
    tipus () {}
    tipus (int s, int k): sorszam(s), kulcs(k) {}
    int key() {return kulcs;}
    int num() {return sorszam;}
    void set_key (int k) {kulcs = k;}
};

std::ifstream in ("dijkstra.in");
std::ofstream out ("dijkstra.out");

int n, m;
std::vector< std::list<int> > x;
std::vector<int> path;

void dijkstra (int node)
{
    path[node] = 0;
    heap<tipus> Min (n+1);
    Min.push (tipus(node, 0));

    tipus akt;
    int pos, newlen;
    std::list<int>::iterator i;
    while (Min.size()) {
        akt = Min.top();
        pos = akt.num();
        Min.pop();
        for (i=x[pos].begin(); i!=x[pos].end(); i++) {
            newlen = path[pos] + COST(*i);
            if (newlen < path[NODE(*i)]) {
                path[NODE(*i)] = newlen;
                if (Min.exists(NODE(*i))) Min.change_key(NODE(*i), newlen);
                else Min.push (tipus(NODE(*i), newlen));
            }
        }
    }
}


int main()
{
    in >> n >> m;
    x.resize (n+1);
    path.resize (n+1, INT_MAX);

    int a, b, c, i;
    for (i=0; i<m; i++) {
        in >> a >> b >> c;
        x[a].push_back ((b << 10) + c);
    }

    dijkstra (1);

    for (i=2; i<=n; i++) {
        if (path[i] != INT_MAX) out << path[i] << " ";
        else out << "0 ";
    }
}