library-rs

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

View the Project on GitHub naoya675/library-rs

:heavy_check_mark: Bellman Ford
(graph/bellman-ford/src/lib.rs)

Description

Verified with

Code

#[derive(Debug, Clone, Copy)]
pub struct Edge {
    from: usize,
    to: usize,
    cost: i64,
}

impl Edge {
    pub fn new(from: usize, to: usize, cost: i64) -> Self {
        Self { from, to, cost }
    }
}

#[derive(Debug, Clone)]
pub struct BellmanFord {
    size: usize,
    edge: Vec<Edge>,
}

impl BellmanFord {
    pub fn new(size: usize) -> Self {
        Self { size, edge: vec![] }
    }

    pub fn add_edge(&mut self, from: usize, to: usize, cost: i64) {
        self.edge.push(Edge::new(from, to, cost));
    }

    pub fn bellman_ford(&mut self, s: usize) -> (bool, Vec<i64>) {
        let mut dist = vec![i64::MAX / 4; self.size];
        dist[s] = 0;
        for _ in 0..self.size {
            let mut update = false;
            for edge in &self.edge {
                if dist[edge.from] == i64::MAX / 4 {
                    continue;
                }
                if dist[edge.from] + edge.cost < dist[edge.to] {
                    dist[edge.to] = dist[edge.from] + edge.cost;
                    update = true;
                }
            }
            if !update {
                return (false, dist);
            }
        }
        for _ in 0..self.size {
            for edge in &self.edge {
                if dist[edge.from] == i64::MAX / 4 {
                    continue;
                }
                if dist[edge.from] + edge.cost < dist[edge.to] {
                    dist[edge.to] = -(i64::MAX / 4);
                }
            }
        }
        (true, dist)
    }
}

pub fn bellman_ford(size: usize, edge: &Vec<(usize, usize, i64)>, s: usize) -> (bool, Vec<i64>) {
    let mut dist = vec![i64::MAX / 4; size];
    dist[s] = 0;
    for _ in 0..size {
        let mut update = false;
        for &(from, to, cost) in edge {
            if dist[from] == i64::MAX / 4 {
                continue;
            }
            if dist[from] + cost < dist[to] {
                dist[to] = dist[from] + cost;
                update = true;
            }
        }
        if !update {
            return (false, dist);
        }
    }
    for _ in 0..size {
        for &(from, to, cost) in edge {
            if dist[from] == i64::MAX / 4 {
                continue;
            }
            if dist[from] + cost < dist[to] {
                dist[to] = -(i64::MAX / 4);
            }
        }
    }
    (true, dist)
}
Traceback (most recent call last):
  File "/opt/hostedtoolcache/Python/3.11.12/x64/lib/python3.11/site-packages/onlinejudge_verify/documentation/build.py", line 71, in _render_source_code_stat
    bundled_code = language.bundle(stat.path, basedir=basedir, options={'include_paths': [basedir]}).decode()
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/hostedtoolcache/Python/3.11.12/x64/lib/python3.11/site-packages/onlinejudge_verify/languages/rust.py", line 288, in bundle
    raise NotImplementedError
NotImplementedError
Back to top page