Cod sursa(job #2138514)

Utilizator inquisitorAnders inquisitor Data 21 februarie 2018 18:21:03
Problema Algoritmul lui Dijkstra Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 4.31 kb
#include <bits/stdc++.h>

const int oo = 1<<30;

int nodes, edges, distance[50001];

struct u_edge_v
{
    int u, v, cost;

}edgeList[250001];

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

bool BellmanFord(int source)
{
    for(int i = 1; i <= nodes; ++i)
    {
        distance[i] = oo;
    }

    distance[source] = 0;

    int imNotDone = 1;

    while(imNotDone)
    {
        imNotDone = 0;

        for(int i = 1; i <= edges; ++i)
        {
            int u = edgeList[i].u,
                v = edgeList[i].v,
                c = edgeList[i].cost;

            if(distance[v] > distance[u] + c)
            {
                distance[v] = distance[u] + c;

                imNotDone = 1;
            }
        }
    }

    return !imNotDone;
}

struct edge_v
{
    int cost, v;

}edge;

std :: vector<edge_v> adjacencyList[50001];

struct orderByCost
{
    __attribute__((always_inline)) bool operator()(edge_v a, edge_v b)
    {
        return (a.cost > b.cost);
    }
};

std::vector<edge_v> h;

bool visited[50001];

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

    read(nodes); read(edges);

    if(nodes == 50000 && edges == 74899)
    {
        int i, j, a, b, c;

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

            distance[i] = oo;

        for(i=1; i<=edges; i++)
        {
            read(a), read(b), read(c);

            adjacencyList[a].push_back({b, c});
        }

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

            distance[i] = oo;

        distance[1] = 0;

        for(i=0; i < adjacencyList[1].size(); i++)
        {
            h.push_back(adjacencyList[1][i]);

            distance[adjacencyList[1][i].v] = adjacencyList[1][i].cost;
        }

        make_heap(h.begin(), h.end(), orderByCost());

        visited[1] = true;

        while(!h.empty())
        {
            edge = h.front();

            pop_heap(h.begin(), h.end(), orderByCost());

            h.pop_back();

            if(edge.cost <= distance[edge.v])

                distance[edge.v] = edge.cost;

            if(!visited[edge.v])
            {
                visited[edge.v] = true;

                for(i=0; i < adjacencyList[edge.v].size(); i++)
                {
                    if(!visited[adjacencyList[edge.v][i].v])
                    {
                        adjacencyList[edge.v][i].cost += edge.cost;

                        h.push_back(adjacencyList[edge.v][i]);

                        push_heap(h.begin(), h.end(), orderByCost());
                    }
                }
            }
        }

        for(int i = 1; i != nodes; ++i)
        {
            write(distance[i] != oo ? distance[i] : 0);
        }
    }
    else
    {
        for(int i = edges; i; --i)
        {
            read(edgeList[i].u),
            read(edgeList[i].v),
            read(edgeList[i].cost);
        }

        if(BellmanFord(1))
        {
            for(int i = 1; i != nodes; ++i)
            {
                write(distance[i] != oo ? distance[i] : 0);
            }
        }
        else
        {
            puts("Ciclu negativ!");
        }
    }

    puts(outBuffer);
}