spl

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub Forestedf/spl

:warning: graph/dense_dijkstra.hpp

Depends on

Code

#pragma once
#include <limits>
#include <queue>
#include "graph.hpp"
// (dist, from)
template <typename T, bool DIR>
std::pair<std::vector<T>, std::vector<int>> dense_dijkstra(
    const Graph<T, DIR> &g, int s, T inf = std::numeric_limits<T>::max()) {
    assert(0 <= s && s < g.v());
    std::vector<T> dist(g.v(), inf);
    std::vector<int> par(g.v(), -1);
    std::vector<int> used(g.v(), 0);
    dist[s] = 0;
    while (true) {
        T d = inf;
        int v = -1;
        for (int i = 0; i < g.v(); ++i) {
            if (!used[i] && dist[i] < d) {
                d = dist[i];
                v = i;
            }
        } 
        if (v == -1) {
            break;
        }
        used[v] = 1;
        for (const Edge<T> &e : g[v]) {
            if (d + e.weight < dist[e.to]) {
                dist[e.to] = d + e.weight;
                par[e.to] = v;
            }
        }
    }
    return std::make_pair(dist, par);
}
#line 2 "graph/dense_dijkstra.hpp"
#include <limits>
#include <queue>
#line 2 "graph/graph.hpp"
#include <iostream>
#include <cassert>
#include <vector>
template <typename T>
struct Edge {
    using W = T;
    int from, to, id;
    W weight;
    Edge<T> rev() const {
        return Edge<T>{to, from, id, weight};
    }
};
template <typename T>
void debug(const Edge<T> &e) {
    std::cerr << e.from << " -> " << e.to << " id = " << e.id << std::cerr << " weight = ";
    debug(e.weight);
}
template <typename T = int, bool DIR = false>
class Graph {
public:
    using E = Edge<T>;
    using W = T;
    static constexpr bool DIRECTED = DIR;
    struct Adjacency {
        using Iter = typename std::vector<E>::iterator;
        Iter be, en;
        Iter begin() const { return be; }
        Iter end() const { return en; }
        int size() const { return (int)std::distance(be, en); }
        E &operator[](int idx) const { return be[idx]; }
    };
    struct ConstAdjacency {
        using Iter = typename std::vector<E>::const_iterator;
        Iter be, en;
        Iter begin() const { return be; }
        Iter end() const { return en; }
        int size() const { return (int)std::distance(be, en); }
        const E &operator[](int idx) const { return be[idx]; }
    };

private:
    int n, m;
    std::vector<E> edges, csr;
    std::vector<int> sep;
    bool built;

public:
    Graph(int n) : n(n), m(0), built(false) {}
    int v() const { return n; }
    int e() const { return m; }
    int add_vertex() {
        return n++;
    }
    void add_edge(int from, int to, W weight = 1) {
        assert(0 <= from && from < n && 0 <= to && to < n);
        edges.emplace_back(E{from, to, m++, weight});
    }
    void build() {
        sep.assign(n + 1, 0);
        csr.resize(DIRECTED ? m : 2 * m);
        for (const E &e : edges) {
            ++sep[e.from + 1];
            if (!DIRECTED) {
                ++sep[e.to + 1];
            }
        }
        for (int i = 0; i < n; ++i) {
            sep[i + 1] += sep[i];
        }
        std::vector<int> c = sep;
        for (const E &e : edges) {
            csr[c[e.from]++] = e;
            if (!DIRECTED) {
                csr[c[e.to]++] = e.rev();
            }
        }
        built = true;
    }
    Adjacency operator[](int v) {
        assert(built && 0 <= v && v < n);
        return Adjacency{csr.begin() + sep[v], csr.begin() + sep[v + 1]};
    }
    ConstAdjacency operator[](int v) const {
        assert(built && 0 <= v && v < n);
        return ConstAdjacency{csr.begin() + sep[v], csr.begin() + sep[v + 1]};
    }
};
#line 5 "graph/dense_dijkstra.hpp"
// (dist, from)
template <typename T, bool DIR>
std::pair<std::vector<T>, std::vector<int>> dense_dijkstra(
    const Graph<T, DIR> &g, int s, T inf = std::numeric_limits<T>::max()) {
    assert(0 <= s && s < g.v());
    std::vector<T> dist(g.v(), inf);
    std::vector<int> par(g.v(), -1);
    std::vector<int> used(g.v(), 0);
    dist[s] = 0;
    while (true) {
        T d = inf;
        int v = -1;
        for (int i = 0; i < g.v(); ++i) {
            if (!used[i] && dist[i] < d) {
                d = dist[i];
                v = i;
            }
        } 
        if (v == -1) {
            break;
        }
        used[v] = 1;
        for (const Edge<T> &e : g[v]) {
            if (d + e.weight < dist[e.to]) {
                dist[e.to] = d + e.weight;
                par[e.to] = v;
            }
        }
    }
    return std::make_pair(dist, par);
}
Back to top page