結果

問題 No.1678 Coin Trade (Multiple)
ユーザー koba-e964koba-e964
提出日時 2021-09-10 22:51:04
言語 Rust
(1.72.1)
結果
AC  
実行時間 1,760 ms / 5,000 ms
コード長 5,355 bytes
コンパイル時間 2,099 ms
コンパイル使用メモリ 162,584 KB
実行使用メモリ 26,128 KB
最終ジャッジ日時 2023-09-02 20:11:17
合計ジャッジ時間 27,586 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,368 KB
testcase_01 AC 1 ms
4,368 KB
testcase_02 AC 1 ms
4,368 KB
testcase_03 AC 110 ms
12,536 KB
testcase_04 AC 749 ms
20,248 KB
testcase_05 AC 289 ms
24,828 KB
testcase_06 AC 215 ms
22,736 KB
testcase_07 AC 558 ms
14,236 KB
testcase_08 AC 373 ms
15,712 KB
testcase_09 AC 319 ms
24,976 KB
testcase_10 AC 83 ms
6,468 KB
testcase_11 AC 500 ms
19,000 KB
testcase_12 AC 73 ms
6,036 KB
testcase_13 AC 1,231 ms
23,504 KB
testcase_14 AC 300 ms
12,048 KB
testcase_15 AC 395 ms
18,700 KB
testcase_16 AC 96 ms
18,444 KB
testcase_17 AC 347 ms
25,472 KB
testcase_18 AC 1 ms
4,372 KB
testcase_19 AC 1 ms
4,368 KB
testcase_20 AC 1 ms
4,368 KB
testcase_21 AC 2 ms
4,368 KB
testcase_22 AC 1 ms
4,372 KB
testcase_23 AC 1 ms
4,368 KB
testcase_24 AC 1 ms
4,368 KB
testcase_25 AC 1 ms
4,368 KB
testcase_26 AC 1 ms
4,368 KB
testcase_27 AC 1 ms
4,372 KB
testcase_28 AC 1 ms
4,368 KB
testcase_29 AC 1 ms
4,368 KB
testcase_30 AC 1 ms
4,368 KB
testcase_31 AC 1 ms
4,372 KB
testcase_32 AC 1 ms
4,368 KB
testcase_33 AC 2 ms
4,376 KB
testcase_34 AC 1 ms
4,372 KB
testcase_35 AC 1 ms
4,372 KB
testcase_36 AC 1 ms
4,368 KB
testcase_37 AC 1 ms
4,368 KB
testcase_38 AC 1 ms
4,372 KB
testcase_39 AC 1 ms
4,368 KB
testcase_40 AC 1 ms
4,372 KB
testcase_41 AC 1 ms
4,368 KB
testcase_42 AC 1 ms
4,368 KB
testcase_43 AC 1 ms
4,368 KB
testcase_44 AC 1 ms
4,372 KB
testcase_45 AC 1 ms
4,368 KB
testcase_46 AC 1 ms
4,368 KB
testcase_47 AC 1 ms
4,372 KB
testcase_48 AC 1,579 ms
26,084 KB
testcase_49 AC 1,760 ms
26,040 KB
testcase_50 AC 1,617 ms
26,028 KB
testcase_51 AC 1,659 ms
26,028 KB
testcase_52 AC 1,594 ms
26,108 KB
testcase_53 AC 1,573 ms
26,084 KB
testcase_54 AC 1,599 ms
26,124 KB
testcase_55 AC 1,651 ms
26,128 KB
testcase_56 AC 1,575 ms
26,044 KB
testcase_57 AC 1,513 ms
26,072 KB
testcase_58 AC 909 ms
24,196 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
macro_rules! input {
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes.by_ref().map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr,) => {};
    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => { ($(read_value!($next, $t)),*) };
    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };
    ($next:expr, usize1) => (read_value!($next, usize) - 1);
    ($next:expr, [ $t:tt ]) => {{
        let len = read_value!($next, usize);
        read_value!($next, [$t; len])
    }};
    ($next:expr, $t:ty) => ($next().parse::<$t>().expect("Parse error"));
}

// Minimum cost flow.
// Verified by: yukicoder No.1301 Strange Graph Shortest Path
//              (https://yukicoder.me/submissions/590401)
//              AtCoder Library Practice Contest - E
//              (https://atcoder.jp/contests/practice2/submissions/22478556)
//              ACL Contest 1 - C
//              (https://atcoder.jp/contests/acl1/submissions/23898415)
type Cap = isize;
type Cost = i64;
#[derive(Debug, Clone, Copy)]
struct Edge {
    to: usize,
    cap: Cap,
    cost: Cost,
    rev: usize, // rev is the position of reverse edge in graph[to]
}

#[derive(Debug, Clone)]
struct MinCostFlow {
    n: usize,
    graph: Vec<Vec<Edge>>,
    h: Vec<Cost>, // potential,
    dist: Vec<Cost>, // minimum distance
    prev: Vec<(usize, usize)>, // previous vertex and edge
}

impl MinCostFlow {
    // Initializes this solver. n is the number of vertices.
    fn new(n: usize) -> Self {
        MinCostFlow {
            n: n,
            graph: vec![vec![]; n],
            h: vec![0; n],
            dist: vec![0; n],
            prev: vec![(0, 0); n],
        }
    }
    fn add_edge(&mut self, from: usize, to: usize, cap: Cap, cost: Cost) {
        let fst = Edge {
            to: to,
            cap: cap,
            cost: cost,
            rev: self.graph[to].len(),
        };
        self.graph[from].push(fst);
        let snd = Edge {
            to: from,
            cap: 0,
            cost: -cost,
            rev: self.graph[from].len() - 1,
        };
        self.graph[to].push(snd);
    }
    // Calcucates the minimum cost flow
    // whose source is s, sink is t, and flow is f.
    fn min_cost_flow(&mut self, s: usize, t: usize, mut f: Cap) -> Cost {
        let n = self.n;
        let inf: Cost = std::i64::MAX / 10; // ?????
        let mut res = 0;
        let h = &mut self.h;
        let dist = &mut self.dist;
        while f > 0 {
            let mut que = std::collections::BinaryHeap::<(Cost, usize)>::new();
            for i in 0..n {
                dist[i] = inf;
            }
            dist[s] = 0;
            que.push((0, s));
            while let Some((d, v)) = que.pop() {
                let d = -d;
                if dist[v] < d {
                    continue;
                }
                for (i, &e) in self.graph[v].iter().enumerate() {
                    if e.cap > 0 && dist[e.to] > dist[v] + e.cost + h[v] - h[e.to] {
                        dist[e.to] = dist[v] + e.cost + h[v] - h[e.to];
                        self.prev[e.to] = (v, i);
                        que.push((-dist[e.to], e.to));
                    }
                }
            }
            if dist[t] == inf {
	        return -1; // Cannot add flow anymore
            }
            for i in 0..n {
                h[i] += dist[i];
            }
            // Add flow fully
            let mut d = f;
            let mut i = t;
            while i != s {
                let (pv, pe) = self.prev[i];
	        d = std::cmp::min(d, self.graph[pv][pe].cap);
                i = pv;
            }
            f -= d;
            res += d as Cost * h[t];
            i = t;
            while i != s {
                let (pv, pe) = self.prev[i];
                self.graph[pv][pe].cap -= d;
                let erev = self.graph[pv][pe].rev;
	        self.graph[i][erev].cap += d;
                i = pv;
            }
        }
        return res;
    }
}

fn main() {
    // In order to avoid potential stack overflow, spawn a new thread.
    let stack_size = 104_857_600; // 100 MB
    let thd = std::thread::Builder::new().stack_size(stack_size);
    thd.spawn(|| solve()).unwrap().join().unwrap();
}

fn solve() {
    input! {
        n: usize, k: isize,
        ab: [(i64, [usize1]); n],
    }
    let mut mcf = MinCostFlow::new(2 * n + 1);
    let big = 1i64 << 40;
    for i in 0..n {
        let (a, ref b) = ab[i];
        mcf.add_edge(n + i, i, k, a);
        mcf.add_edge(i, n + i + 1, k, big - a);
        for &b in b {
            mcf.add_edge(b, i, 1, big * (i - b) as i64);
        }
    }
    let ans = mcf.min_cost_flow(n, 2 * n, k);
    println!("{}", big * k as i64 * n as i64 - ans);
}
0