Cod sursa(job #2211569)

Utilizator FunnyStockyMihnea Andreescu FunnyStocky Data 10 iunie 2018 21:54:50
Problema Algoritmul lui Dijkstra Scor 90
Compilator cpp Status done
Runda Arhiva educationala Marime 3.71 kb
#include <bits/stdc++.h>

using namespace std;

__attribute__((always_inline)) void read(int &num)
{
    static char inBuffer[0x30D40];

    static unsigned int p = 0x30D3F; num = 0x0;

    while(inBuffer[p] < 0x30 | inBuffer[p] > 0x39)
    {
        ++p != 0x30D40 || (fread(inBuffer, 0x1, 0x30D40, stdin), p = 0x0);
    }

    while(inBuffer[p] > 0x2F & inBuffer[p] < 0x3A)
    {
        num = num * 0xA + inBuffer[p] - 0x30;

        ++p != 0x30D40 || (fread(inBuffer, 0x1, 0x30D40, stdin), p = 0x0);
    }
}

char outBuffer[0x61A80]; unsigned int p;

__attribute__((always_inline)) void write(unsigned int x)
{
    unsigned int digits = x > 0x3B9AC9FF ? 0xA :
                 x > 0x5F5E0FF  ? 0x9 :
                 x > 0x98967F   ? 0x8 :
                 x > 0xF423F    ? 0x7 :
                 x > 0x1869F    ? 0x6 :
                 x > 0x270F     ? 0x5 :
                 x > 0x3E7      ? 0x4 :
                 x > 0x63       ? 0x3 :
                 x > 0x9        ? 0x2 : 0x1;

    for(unsigned int i = ~-digits; ~i; --i)
    {
        outBuffer[p + i] = x % 0xA + 0x30;

        x = x / 0xA;
    }

    p = p + digits; outBuffer[p++] = 0x20;
}
class OutParser {
private:
    FILE *fout;
    char *buff;
    int sp;

    void write_ch(char ch) {
        if (sp == 70000) {
            fwrite(buff, 1, 70000, fout);
            sp = 0;
            buff[sp++] = ch;
        } else {
            buff[sp++] = ch;
        }
    }


public:
    OutParser(const char* name) {
        fout = fopen(name, "w");
        buff = new char[70000]();
        sp = 0;
    }
    ~OutParser() {
        fwrite(buff, 1, sp, fout);
        fclose(fout);
    }

    OutParser& operator << (int vu32) {
        if (vu32 <= 9) {
            write_ch(vu32 + '0');
        } else {
            (*this) << (vu32 / 10);
            write_ch(vu32 % 10 + '0');
        }
        return *this;
    }

    OutParser& operator << (long long vu64) {
        if (vu64 <= 9) {
            write_ch(vu64 + '0');
        } else {
            (*this) << (vu64 / 10);
            write_ch(vu64 % 10 + '0');
        }
        return *this;
    }

    OutParser& operator << (char ch) {
        write_ch(ch);
        return *this;
    }
    OutParser& operator << (const char *ch) {
        while (*ch) {
            write_ch(*ch);
            ++ch;
        }
        return *this;
    }
};

struct info
{
    int nod,cost;
};

struct graf
{
    int nod;
};

const int N=50000;
const long long inf=(1LL<<60);
long long best[N+5];

bool operator<(graf a,graf b)
{
    return best[a.nod]>best[b.nod];
}

int n,m;
vector<info>v[N+5];

priority_queue<graf>q;
bool pus[N+5];

int main()
{
    freopen("dijkstra.in","r",stdin);
    OutParser fout("dijkstra.out");
    read(n);
    read(m);
    while(m--)
    {
        int a,b,c;
        read(a);
        read(b);
        read(c);
        v[a].push_back({b,c});
    }
    for(int i=2;i<=n;i++)
        best[i]=inf;
    best[1]=0;
    pus[1]=1;
    q.push({1});
    while(!q.empty())
    {
        int nod=q.top().nod;
        q.pop();
        for(int i=0;i<v[nod].size();i++)
        {
            int nod_nou=v[nod][i].nod;
            long long cost=best[nod]+v[nod][i].cost;
            if(cost<best[nod_nou])
            {
                best[nod_nou]=cost;
                if(pus[nod_nou]==0)
                {
                    q.push({nod_nou});
                    pus[nod_nou]=0;
                }
            }
        }
    }
    for(int i=2;i<=n;i++)
    {
        if(best[i]==inf)
            fout<<0<<" ";
        else
            fout<<best[i]<<" ";
    }
    return 0;
}
/**
**/