Cod sursa(job #1933094)

Utilizator BaweeLazar Vlad Bawee Data 20 martie 2017 13:47:46
Problema Algoritmul lui Dijkstra Scor 90
Compilator cpp Status done
Runda Arhiva educationala Marime 3.33 kb
#include <fstream>
#include <vector>
#include <algorithm>

using namespace std;

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];
}

class parser{
    public:
        parser() {}
        parser(const char *file_name){
            input_file.open(file_name,ios::in | ios::binary);
            input_file.sync_with_stdio(false);
            index&=0;
            input_file.read(buffer,SIZE);}
        inline parser &operator >>(int &n){
            for (;buffer[index]<'0' or buffer[index]>'9';inc());
            n&=0;
            sign&=0;
            sign|=(buffer[index-1]=='-');
            for (;'0'<=buffer[index] and buffer[index]<='9';inc())
                n=10*n+buffer[index]-'0';
            n^=((n^-n)&-sign);
            return *this;}
        ~parser(){
            input_file.close();}
    private:
        fstream input_file;
        static const int SIZE=0x200000; ///2MB
        char buffer[SIZE];
        int index,sign;
        inline void inc(){
            if(++index==SIZE)
                index=0,input_file.read(buffer,SIZE);}
};

class writer{
    public:
        writer() {};
        writer(const char *file_name){
            output_file.open(file_name,ios::out | ios::binary);
            output_file.sync_with_stdio(false);
            index&=0;}
        inline writer &operator <<(int target){
            aux&=0;
            sign=1;
            if (target<0)
                sign=-1;
            n=target;
            if (n==0)
                nr[aux++]='0';
            for (;n;n/=10)
                nr[aux++]=(sign*n)%10+'0';
            if (sign==-1)
                buffer[index]='-',inc();
            for(;aux;inc())
                buffer[index]=nr[--aux];
            return *this;}
        inline writer &operator <<(const char *target){
            aux&=0;
            while (target[aux])
                buffer[index]=target[aux++],inc();
            return *this;}
        ~writer(){
            output_file.write(buffer,index);output_file.close();}
    private:
        fstream output_file;
        static const int SIZE=0x200000; ///2MB
        int index,aux,n,sign;
        char buffer[SIZE],nr[24];
        inline void inc(){
            if(++index==SIZE)
                index&=0,output_file.write(buffer,SIZE);}
};

parser f("dijkstra.in");
writer g("dijkstra.out");

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;
}