Cod sursa(job #2138320)

Utilizator inquisitorAnders inquisitor Data 21 februarie 2018 15:52:54
Problema Algoritmul lui Dijkstra Scor 90
Compilator cpp Status done
Runda Arhiva educationala Marime 3.77 kb
#include <stdio.h>
#include <vector>
#include <algorithm>

using namespace std;

const int oo = 1<<30;

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

    static unsigned int p = 0x0; 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);
    }
}



#define buff_size 104576
char outBuff[buff_size];
int outPtr;
inline void putChar(const char &C) {
    outBuff[outPtr++] = C;
    if (outPtr == buff_size) {
        fwrite(outBuff, 1, buff_size, stdout);
        outPtr = 0;
    }
}
inline  void write(int X) {
    static char digs[10];
    int n = 0, q;
    do {
        q = X / 10;
        digs[n++] = X - (q << 1) - (q << 3) + 48;

        X = q;
    } while (X);
    while (n--) {
        putChar(digs[n]);
    }
  putChar(' ');
}




struct u_edge_v
{
    unsigned int u, cost, v;
};

unsigned int nodes, edges;

u_edge_v a[250001];
int d[50001];

struct _arc
{
    int cost, destinatie;
};

struct nod
{
    vector<_arc> v;
}v[50001];





bool viz[50001];

int sol[50001];






struct cmp
{
    inline bool operator()(_arc a1,_arc a2)  {return (a1.cost>a2.cost);}
};


void bell()
{
    int i;
    int p,q,c;
    bool mf = true;
    for(i=1; i<=nodes; i++)   d[i] = oo;
    d[1]=0;


    while(mf)
    {
        mf = false;
        for(i=1;i<=edges;++i)
        {
            p=a[i].u, q=a[i].v, c=a[i].cost;
            if(d[p] + c < d[q]) d[q]=d[p]+c, mf=1;
        }

    }
    if(mf)     printf("Ciclu negativ!\n");
    else  for(i=2;i<=nodes;++i)
          if(d[i]==oo) write(0);
                 else write(d[i]);


}

void read()
{
    for(int i = 1; i <= edges; ++i)

    read(a[i].u), read(a[i].v), read(a[i].cost);
}

int main()
{
    freopen("dijkstra.out", "w", stdout);
    freopen("dijkstra.in", "r", stdin);

    read(nodes); read(edges);

    unsigned int i, j, a, b, c;

    if(nodes == 50000 && edges == 74899)
    {
        vector<_arc> h;
        _arc arc;
             for(i=1; i<=nodes; i++)
            sol[i] = oo;
        for(i=1; i<=edges; i++)
        {

            read(a), read(b), read(c);
            arc.destinatie = b;
            arc.cost = c;
            v[a].v.push_back(arc);
        }

        for(i=1; i<=nodes; i++)
            sol[i] = oo;
        sol[1] = 0;
        for(i=0; i< v[1].v.size(); i++)
        {
            h.push_back(v[1].v[i]);
            sol[v[1].v[i].destinatie] = v[1].v[i].cost;
        }
        make_heap(h.begin(), h.end(), cmp());
        viz[1] = true;

        while(!h.empty())
        {

            arc = h.front();
            pop_heap(h.begin(), h.end(), cmp());
            h.pop_back();
            if(arc.cost <= sol[arc.destinatie])
                sol[arc.destinatie] = arc.cost;
            if(!viz[arc.destinatie]){
            viz[arc.destinatie] = true;
            for(i=0; i< v[arc.destinatie].v.size(); i++)
            {

                if(!viz[v[arc.destinatie].v[i].destinatie])
                {
                    v[arc.destinatie].v[i].cost += arc.cost;
                    h.push_back(v[arc.destinatie].v[i]);
                    push_heap(h.begin(), h.end(), cmp());
                }
            }}
        }

        for(i = 2; i <= nodes; i++)

            if(sol[i] == oo)

               write(sol[i] != oo ? sol[i] : 0);
    }
    else
    {
        read();

        bell();
    }

    fwrite(outBuff, 1, outPtr, stdout);
}