結果

問題 No.1301 Strange Graph Shortest Path
ユーザー phsplsphspls
提出日時 2022-11-25 01:13:23
言語 Rust
(1.59.0)
結果
AC  
実行時間 231 ms / 3,000 ms
コード長 3,814 Byte
コンパイル時間 1,562 ms
使用メモリ 40,248 KB
最終ジャッジ日時 2022-11-25 01:13:35
合計ジャッジ時間 11,554 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
testcase_00 AC 1 ms
1,980 KB
testcase_01 AC 1 ms
1,888 KB
testcase_02 AC 170 ms
39,948 KB
testcase_03 AC 135 ms
35,368 KB
testcase_04 AC 231 ms
38,536 KB
testcase_05 AC 141 ms
39,960 KB
testcase_06 AC 194 ms
35,560 KB
testcase_07 AC 179 ms
37,380 KB
testcase_08 AC 140 ms
35,892 KB
testcase_09 AC 185 ms
33,792 KB
testcase_10 AC 148 ms
35,604 KB
testcase_11 AC 198 ms
36,840 KB
testcase_12 AC 198 ms
36,884 KB
testcase_13 AC 175 ms
40,124 KB
testcase_14 AC 184 ms
33,780 KB
testcase_15 AC 182 ms
34,980 KB
testcase_16 AC 229 ms
38,700 KB
testcase_17 AC 186 ms
40,124 KB
testcase_18 AC 167 ms
36,448 KB
testcase_19 AC 195 ms
35,960 KB
testcase_20 AC 205 ms
35,196 KB
testcase_21 AC 175 ms
38,520 KB
testcase_22 AC 201 ms
36,260 KB
testcase_23 AC 177 ms
39,520 KB
testcase_24 AC 218 ms
35,764 KB
testcase_25 AC 217 ms
38,364 KB
testcase_26 AC 185 ms
36,676 KB
testcase_27 AC 196 ms
36,960 KB
testcase_28 AC 153 ms
39,416 KB
testcase_29 AC 221 ms
38,404 KB
testcase_30 AC 204 ms
38,128 KB
testcase_31 AC 209 ms
38,228 KB
testcase_32 AC 1 ms
2,016 KB
testcase_33 AC 82 ms
31,168 KB
testcase_34 AC 229 ms
40,248 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: variable `N` should have a snake case name
  --> Main.rs:46:13
   |
46 |         let N = self.paths.len();
   |             ^ help: convert the identifier to snake case: `n`
   |
   = note: `#[warn(non_snake_case)]` on by default

warning: 1 warning emitted

ソースコード

diff #

use std::{cmp::Reverse, collections::BinaryHeap};

const INF: isize = 1isize << 60;

#[derive(Debug, Clone, Copy)]
struct MinCostFlowEdge {
    dest: usize,
    capacity: isize,
    cost: isize,
    rev: usize,
}

impl MinCostFlowEdge {
    fn new(dest: usize, capacity: isize, cost: isize, rev: usize) -> MinCostFlowEdge {
        MinCostFlowEdge { dest, capacity, cost, rev }
    }
}

struct MinCostFlow {
    paths: Vec<Vec<MinCostFlowEdge>>,
    potential: Vec<isize>,
    min_cost: Vec<isize>,
    prevv: Vec<usize>,
    preve: Vec<usize>,
}

impl MinCostFlow {
    fn new(n: usize) -> MinCostFlow {
        MinCostFlow {
            paths: vec![vec![]; n],
            potential: vec![0isize; n],
            min_cost: vec![0isize; n],
            prevv: vec![0usize; n],
            preve: vec![0usize; n],
        }
    }

    fn pusha2b(&mut self, a: usize, b: usize, capacity: isize, cost: isize) {
        let connecta = self.paths[a].len();
        let connectb = self.paths[b].len();
        self.paths[a].push(MinCostFlowEdge::new(b, capacity, cost, connectb));
        self.paths[b].push(MinCostFlowEdge::new(a, 0, -cost, connecta));
    }

    fn mincost_flow(&mut self, s: usize, t: usize, _f: isize) -> isize {
        let N = self.paths.len();
        let mut ret = 0isize;
        let mut que = BinaryHeap::new();
        self.potential = vec![0isize; N];
        let mut f = _f;
        while f > 0 {
            self.min_cost = vec![INF; N];
            que.push(Reverse((0, s)));
            self.min_cost[s] = 0;
            while let Some(Reverse((p, v))) = que.pop() {
                if self.min_cost[v] < p { continue; }
                for i in 0..self.paths[v].len() {
                    let edge = self.paths[v][i];
                    let ncost = self.min_cost[v] + edge.cost + self.potential[v] - self.potential[edge.dest];
                    if edge.capacity > 0 && self.min_cost[edge.dest] > ncost {
                        self.min_cost[edge.dest] = ncost;
                        self.prevv[edge.dest] = v;
                        self.preve[edge.dest] = i;
                        que.push(Reverse((self.min_cost[edge.dest], edge.dest)));
                    }
                }
            }
            if self.min_cost[t] == INF {
                return -1;
            }
            for i in 0..N { self.potential[i] += self.min_cost[i]; }

            let mut d = f;
            let mut temp = t;
            while temp != s {
                d = d.min(self.paths[self.prevv[temp]][self.preve[temp]].capacity);
                temp = self.prevv[temp];
            }
            f -= d;
            ret += d * self.potential[t];
            temp = t;
            while temp != s {
                let edge = self.paths[self.prevv[temp]][self.preve[temp]];
                self.paths[self.prevv[temp]][self.preve[temp]].capacity -= d;
                self.paths[temp][edge.rev].capacity += d;
                temp = self.prevv[temp];
            }
        }
        ret
    }
}

fn main() {
    let mut nm = String::new();
    std::io::stdin().read_line(&mut nm).ok();
    let nm: Vec<usize> = nm.trim().split_whitespace().map(|s| s.parse().unwrap()).collect();
    let n = nm[0];
    let m = nm[1];
    let mut flow = MinCostFlow::new(n);
    for _ in 0..m {
        let mut temp = String::new();
        std::io::stdin().read_line(&mut temp).ok();
        let temp: Vec<isize> = temp.trim().split_whitespace().map(|s| s.parse().unwrap()).collect();
        let u = temp[0] as usize - 1;
        let v = temp[1] as usize - 1;
        let c = temp[2];
        let d = temp[3];
        flow.pusha2b(u, v, 1, c);
        flow.pusha2b(u, v, 1, d);
        flow.pusha2b(v, u, 1, c);
        flow.pusha2b(v, u, 1, d);
    }

    println!("{}", flow.mincost_flow(0, n-1, 2));
}
0