Cod sursa(job #1929246)

Utilizator Kln1000Ciobanu Bogdan Kln1000 Data 17 martie 2017 12:49:21
Problema Algoritmul lui Dijkstra Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 5.92 kb
#include <iostream>
#include <fstream>
#include <queue>
#include <vector>
#include <cmath>

#define oo 1<<30

using namespace std;

/**

                                          `-.`'.-'
                                       `-.        .-'.
                                    `-.    -./\.-    .-'
                                        -.  /_|\  .-
                                    `-.   `/____\'   .-'.
                                 `-.    -./.-""-.\.-      '
                                    `-.  /< (()) >\  .-'
                                  -   .`/__`-..-'__\'   .-
                                ,...`-./___|____|___\.-'.,.
                                   ,-'   ,` . . ',   `-,
                                ,-'   ________________  `-,
                                   ,'/____|_____|_____\
                                  / /__|_____|_____|___\
                                 / /|_____|_____|_____|_\
                                ' /____|_____|_____|_____\
                              .' /__|_____|_____|_____|___\
                             ,' /|_____|_____|_____|_____|_\
,,---''--...___...--'''--.. /../____|_____|_____|_____|_____\ ..--```--...___...--``---,,
                           '../__|_____|_____|_____|_____|___\
      \    )              '.:/|_____|_____|_____|_____|_____|_\               (    /
      )\  / )           ,':./____|_____|_____|_____|_____|_____\             ( \  /(
     / / ( (           /:../__|_____|_____|_____|_____|_____|___\             ) ) \ \
    | |   \ \         /.../|_____|_____|_____|_____|_____|_____|_\           / /   | |
 .-.\ \    \ \       '..:/____|_____|_____|_____|_____|_____|_____\         / /    / /.-.
(=  )\ `._.' |       \:./ _  _ ___  ____  ____ _    _ _ _ _ _  _ __\        | `._.' /(  =)
 \ (_)       )        \/                                            \       (       (_) /
  \    `----'          """"""""""""""""""""""""""""""""""""""""""""""        `----'    /
   \   ____\__                                                              __/____   /
    \ (=\     \                                                            /     /-) /
     \_)_\     \                                                         /     /_(_/
          \     \                                                        /     /
           )     )  _                                                _  (     (
          (     (,-' `-..__                                    __..-' `-,)     )
           \_.-''          ``-..____                  ____..-''          ``-._/
            `-._                    ``--...____...--''                    _.-'
                `-.._                                                _..-'
                     `-..__       FORTIS FORTUNA ADIUVAT       __..-'
                           ``-..____                  ____..-''
                                    ``--...____...--''

*/

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 t ("dijkstra.out");

struct edge{
    int nod,c;
    bool operator <(const edge &aux) const
        {return c>aux.c;}
};

vector <edge> v[50010];
priority_queue <edge> heap;
int d[50010],n;

void dijkstra(int nod){
    for(int i=1;i<=n;++i) d[i]=oo;
    d[nod]=0;
    heap.push({nod,0});
    while(!heap.empty()){
        int nod=heap.top().nod,c=heap.top().c;
        heap.pop();
        if(d[nod]!=c) continue;
        for(vector <edge>::iterator i=v[nod].begin();i<v[nod].end();advance(i,1))
            if(d[i->nod]>d[nod]+i->c)
                d[i->nod]=d[nod]+i->c,heap.push({i->nod,d[i->nod]});
    }
}

int main()
{
    int x,y,c,m;
    f>>n>>m;
    for (int i=0;i<m;++i)
        f>>x>>y>>c,v[x].push_back({y,c});
    dijkstra(1);
    for(int i=2;i<=n;++i)
        if(d[i]==oo) t<<"0 ";
        else t<<d[i]<<" ";
    return 0;
}