Cod sursa(job #2498391)

Utilizator Kln1000Ciobanu Bogdan Kln1000 Data 23 noiembrie 2019 20:55:02
Problema Algoritmul Bellman-Ford Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 8.94 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <bitset>
#include <queue>

#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>

#define MAX_N 50010
#define INF 0x7FFFFFFF

using namespace std;

/**

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

*/

#define SIGNED

class parser {
public:
    inline parser() {
        /// default c-tor
    }

    inline parser(const char* file_name) {
        int fd = open(file_name, O_RDONLY);
        index &= 0;
        fstat(fd, &sb);
        buffer = (char*)mmap(0, sb.st_size, PROT_READ, MAP_PRIVATE | MAP_NORESERVE, fd, 0);
        close(fd);
    }

    template <typename T>
    inline parser& operator >> (T& n) {
        n &= 0;
        for (; buffer[index] < '0' or buffer[index] > '9'; ++index);
#ifdef SIGNED
        sign &= 0;
        sign |= (buffer[(index ? index - 1 : 0)] == '-');
#endif
        for (; '0' <= buffer[index] and buffer[index] <= '9'; ++index)
            n = (n << 3) + (n << 1) + buffer[index] - '0';
#ifdef SIGNED
        n ^= ((n ^ -n) & -sign);
#endif
        return *this;
    }

    ~parser() {
        munmap(buffer, sb.st_size);
    }

private:
    struct stat sb;
    int index;
#ifdef SIGNED
    int sign;
#endif
    char* buffer;
};
 

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);
    }
};
/*
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,
            buffer[index] = '-',
            target = -target,
            inc();
        else if (target == 0)
            nr[aux++] = '0';
        for (; target; target /= 10)
            nr[aux++] = target % 10 + '0';
        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("bellmanford.in");
writer t("bellmanford.out");
/*
int n, m;
bool negative = false;
std::vector <std::pair <int, int> > graph[MAX_N];
std::vector <int> distance(MAX_N, INF), relaxations(MAX_N, 0);

void bellman_ford (int start){
    std::bitset<MAX_N> in_queue;
    std::queue<unsigned> q;

    q.push(start);
    in_queue[start] = true;
    distance[start] = 0;

    while (!q.empty()) {
        auto current = q.front();
        q.pop();
        in_queue[current] = false;

        for (auto iterator : graph[current]) {
            if (distance[current] + iterator.second < distance[iterator.first]) {
                if (++relaxations[iterator.first] == n) {
                    negative = true;
                    return;
                }
                else {
                    if (!in_queue[iterator.first])
                        q.push(iterator.first);
                    distance[iterator.first] = distance[current] + iterator.second;
                }
            }
        }
    }
}

int main() {
    f >> n >> m;
    for (int x, y, c, i = 0; i < m; ++i) {
        f >> x >> y >> c;
        graph[x].push_back({ y, c });
    }
    bellman_ford(1);
    if (!negative)
        for (int i = 2; i <= n; ++i)
            t << distance[i] << " ";
    else
        t << "Ciclu negativ!";
    return 0;
}

*/

int n, m;
bool cycle = false;
vector <pair <int, int> > v[MAX_N];
vector <int> dist(MAX_N, INF), counter(MAX_N, 0);


void bellman_ford(int nod) {
    queue <int> q;
    bitset <MAX_N> in_queue;
    dist[nod] = 0;
    q.push(nod);
    in_queue[nod] = 1;
    while (!q.empty() and !cycle) {
        int x = q.front();
        q.pop();
        in_queue[x] = 0;
        for (auto i : v[x])
            if (dist[x] < INF)
                if (dist[i.first] > dist[x] + i.second) {
                    dist[i.first] = dist[x] + i.second;
                    if (!in_queue[i.first])
                        if (counter[i.first] > n)
                            cycle = true;
                        else {
                            q.push(i.first);
                            in_queue[i.first] = 1;
                            ++counter[i.first];
                        }
                }
    }
}

int main()
{
    f >> n >> m;
    for (int x, y, c, i = 0; i < m; ++i) {
        f >> x >> y >> c;
        v[x].push_back({ y,c });
    }
    bellman_ford(1);
    if (!cycle)
        for (int i = 2; i <= n; ++i)
            t << dist[i] << " ";
    else
        t << "Ciclu negativ!";
    return 0;
}