結果

問題 No.1678 Coin Trade (Multiple)
ユーザー akakimidoriakakimidori
提出日時 2021-08-14 04:05:30
言語 Rust
(1.77.0 + proconio)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 5,812 bytes
コンパイル時間 14,250 ms
コンパイル使用メモリ 379,724 KB
実行使用メモリ 18,864 KB
最終ジャッジ日時 2024-06-11 21:46:47
合計ジャッジ時間 47,853 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,248 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 390 ms
7,552 KB
testcase_04 AC 1,048 ms
11,304 KB
testcase_05 AC 1,190 ms
13,456 KB
testcase_06 AC 1,142 ms
12,292 KB
testcase_07 AC 695 ms
8,448 KB
testcase_08 AC 618 ms
9,216 KB
testcase_09 AC 1,201 ms
13,548 KB
testcase_10 AC 51 ms
5,376 KB
testcase_11 AC 1,032 ms
10,856 KB
testcase_12 AC 49 ms
5,376 KB
testcase_13 AC 1,292 ms
12,852 KB
testcase_14 AC 399 ms
7,296 KB
testcase_15 AC 1,083 ms
10,748 KB
testcase_16 AC 550 ms
10,492 KB
testcase_17 AC 1,120 ms
13,724 KB
testcase_18 AC 1 ms
5,376 KB
testcase_19 AC 1 ms
5,376 KB
testcase_20 AC 0 ms
5,376 KB
testcase_21 AC 1 ms
5,376 KB
testcase_22 AC 1 ms
5,376 KB
testcase_23 AC 1 ms
5,376 KB
testcase_24 AC 1 ms
5,376 KB
testcase_25 AC 1 ms
5,376 KB
testcase_26 AC 1 ms
5,376 KB
testcase_27 AC 1 ms
5,376 KB
testcase_28 AC 1 ms
5,376 KB
testcase_29 AC 1 ms
5,376 KB
testcase_30 AC 1 ms
5,376 KB
testcase_31 AC 1 ms
5,376 KB
testcase_32 AC 1 ms
5,376 KB
testcase_33 AC 1 ms
5,376 KB
testcase_34 AC 1 ms
5,376 KB
testcase_35 AC 1 ms
5,376 KB
testcase_36 AC 1 ms
5,376 KB
testcase_37 AC 1 ms
5,376 KB
testcase_38 AC 1 ms
5,376 KB
testcase_39 AC 1 ms
5,376 KB
testcase_40 AC 1 ms
5,376 KB
testcase_41 AC 1 ms
5,376 KB
testcase_42 AC 1 ms
5,376 KB
testcase_43 AC 1 ms
5,376 KB
testcase_44 AC 1 ms
5,376 KB
testcase_45 AC 1 ms
5,376 KB
testcase_46 AC 1 ms
5,376 KB
testcase_47 AC 1 ms
5,376 KB
testcase_48 AC 1,809 ms
14,108 KB
testcase_49 AC 1,400 ms
14,084 KB
testcase_50 AC 1,770 ms
14,100 KB
testcase_51 AC 1,926 ms
14,112 KB
testcase_52 AC 1,624 ms
14,072 KB
testcase_53 AC 1,460 ms
14,116 KB
testcase_54 AC 1,742 ms
14,108 KB
testcase_55 AC 1,389 ms
14,096 KB
testcase_56 AC 1,886 ms
14,084 KB
testcase_57 AC 1,972 ms
14,104 KB
testcase_58 TLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

const INF: i64 = 1_000_000_000_000_000i64 + 1;

struct Graph {
    size: usize,
    edge: Vec<(usize, usize, i64, i64)>,
}

impl Graph {
    fn new(size: usize) -> Self {
        Graph {
            size: size,
            edge: vec![],
        }
    }
    fn add_edge(&mut self, src: usize, dst: usize, capa: i64, cost: i64) {
        assert!(src < self.size && dst < self.size && src != dst);
        self.edge.push((src, dst, capa, cost));
    }
    fn flow(&self, src: usize, dst: usize, flow: i64) -> Vec<(i64, i64)> {
        assert!(src != dst);
        let size = self.size;
        let edge = &self.edge;
        let mut deg = vec![0; size];
        for &(a, b, _, _) in edge.iter() {
            deg[a] += 1;
            deg[b] += 1;
        }
        let mut graph: Vec<_> = deg.into_iter().map(|d| Vec::with_capacity(d)).collect();
        for &(a, b, capa, cost) in edge.iter() {
            let x = graph[a].len();
            let y = graph[b].len();
            graph[a].push((b, capa, cost, y));
            graph[b].push((a, 0, -cost, x));
        }
        let mut sum = 0;
        let mut res = vec![];
        let mut used = 0;
        let mut dp = Vec::with_capacity(size);
        let mut pot = vec![0; size];
        let mut h = std::collections::BinaryHeap::new();
        while used < flow {
            dp.clear();
            dp.resize(size, (INF, src, 0)); // コスト、親、親からの番号
            dp[src] = (0, src, 0);
            if used > 0 {
                h.push((0, src));
                while let Some((d, v)) = h.pop() {
                    let d = -d;
                    if d > dp[v].0 {
                        continue;
                    }
                    for (i, &(u, capa, cost, _)) in graph[v].iter().enumerate() {
                        if capa == 0 {
                            continue;
                        }
                        let c = d + cost - pot[u] + pot[v];
                        if c < dp[u].0 {
                            dp[u] = (c, v, i);
                            h.push((-c, u));
                        }
                    }
                }
            } else {
                let mut elem = Vec::with_capacity(size);
                let mut que = std::collections::VecDeque::new();
                elem.clear();
                elem.resize(size, false);
                elem[src] = true;
                que.push_back(src);
                while let Some(v) = que.pop_front() {
                    elem[v] = false;
                    let (c, _, _) = dp[v];
                    for (i, &(u, capa, cost, _)) in graph[v].iter().enumerate() {
                        if capa == 0 {
                            continue;
                        }
                        let c = c + cost;
                        if c < dp[u].0 {
                            dp[u] = (c, v, i);
                            if !elem[u] {
                                elem[u] = true;
                                que.push_back(u);
                            }
                        }
                    }
                }
            }
            if dp[dst].0 == INF {
                break;
            }
            for i in 0..size {
                pot[i] -= dp[dst].0 - dp[i].0;
            }
            let mut sub = 1;
            let mut pos = dst;
            while pos != src {
                let (_, parent, k) = dp[pos];
                sub = std::cmp::min(sub, graph[parent][k].1);
                pos = parent;
            }
            let mut pos = dst;
            while pos != src {
                let (_, parent, k) = dp[pos];
                let inv = graph[parent][k].3;
                graph[parent][k].1 -= sub;
                graph[pos][inv].1 += sub;
                pos = parent;
            }
            used += sub;
            sum += -pot[src] * sub;
            res.push((used, sum));
        }
        res
    }
}
// ---------- begin scannner ----------
#[allow(dead_code)]
mod scanner {
    use std::str::FromStr;
    pub struct Scanner<'a> {
        it: std::str::SplitWhitespace<'a>,
    }
    impl<'a> Scanner<'a> {
        pub fn new(s: &'a String) -> Scanner<'a> {
            Scanner {
                it: s.split_whitespace(),
            }
        }
        pub fn next<T: FromStr>(&mut self) -> T {
            self.it.next().unwrap().parse::<T>().ok().unwrap()
        }
        pub fn next_bytes(&mut self) -> Vec<u8> {
            self.it.next().unwrap().bytes().collect()
        }
        pub fn next_chars(&mut self) -> Vec<char> {
            self.it.next().unwrap().chars().collect()
        }
        pub fn next_vec<T: FromStr>(&mut self, len: usize) -> Vec<T> {
            (0..len).map(|_| self.next()).collect()
        }
    }
}
// ---------- end scannner ----------

use std::io::Write;

fn main() {
    use std::io::Read;
    let mut s = String::new();
    std::io::stdin().read_to_string(&mut s).unwrap();
    let mut sc = scanner::Scanner::new(&s);
    let out = std::io::stdout();
    let mut out = std::io::BufWriter::new(out.lock());
    run(&mut sc, &mut out);
}

fn run<W: Write>(sc: &mut scanner::Scanner, out: &mut std::io::BufWriter<W>) {
    let n: usize = sc.next();
    let k: i64 = sc.next();
    let mut g = Graph::new(n + 1);
    let src = 0;
    let dst = n;
    for i in 1..=n {
        g.add_edge(i - 1, i, k, 0);
    }
    let mut a = vec![0i64; n];
    for i in 0..n {
        a[i] = sc.next();
        let m: usize = sc.next();
        for _ in 0..m {
            let b = sc.next::<usize>() - 1;
            if a[b] < a[i] {
                g.add_edge(b, i, 1, -(a[i] - a[b]));
            }
        }
    }
    let res = g.flow(src, dst, k);
    let ans = -res.iter().map(|p| p.1).min().unwrap_or(0);
    writeln!(out, "{}", ans).ok();
}
0