結果

問題 No.3097 Azuki Kurai
ユーザー akakimidori
提出日時 2025-04-06 17:41:55
言語 Rust
(1.83.0 + proconio)
結果
WA  
実行時間 -
コード長 9,237 bytes
コンパイル時間 12,941 ms
コンパイル使用メモリ 399,440 KB
実行使用メモリ 7,848 KB
最終ジャッジ日時 2025-04-06 17:43:00
合計ジャッジ時間 64,400 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 2 WA * 30
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: unused import: `std::io::Write`
  --> src/main.rs:21:5
   |
21 | use std::io::Write;
   |     ^^^^^^^^^^^^^^
   |
   = note: `#[warn(unused_imports)]` on by default

warning: type alias `Map` is never used
  --> src/main.rs:24:6
   |
24 | type Map<K, V> = BTreeMap<K, V>;
   |      ^^^
   |
   = note: `#[warn(dead_code)]` on by default

warning: type alias `Set` is never used
  --> src/main.rs:25:6
   |
25 | type Set<T> = BTreeSet<T>;
   |      ^^^

warning: type alias `Deque` is never used
  --> src/main.rs:26:6
   |
26 | type Deque<T> = VecDeque<T>;
   |      ^^^^^

warning: associated functions `zero` and `inf` are never used
   --> src/main.rs:138:12
    |
135 |     pub trait MaxFlowCapacity:
    |               --------------- associated functions in this trait
...
138 |         fn zero() -> Self;
    |            ^^^^
139 |         fn inf() -> Self;
    |            ^^^

warning: fields `to_` and `inv_` are never read
   --> src/main.rs:162:9
    |
161 |     struct Edge<Cap> {
    |            ---- fields in this struct
162 |         to_: u32,
    |         ^^^
163 |         inv_: u32,
    |         ^^^^
    |
    = note: `Edge` has a derived impl for the trait `Clone`, but this is intentionally ignored during dead code analysis

warning: associated items `new`, `to`, and `inv` are never used
   --> src/main.rs:168:12
    |
167 |     impl<Cap> Edge<Cap> {
    |     ------------------- associated items in this implementation
168 |         fn new(to: usize, inv: usize, cap: Cap) -> Self {
    |            ^^^
...
175 |         fn to(&self) -> usize {
    |            ^^
...
178 |         fn inv(&self) -> usize {
    |            ^^^

warning: methods `add` and `sub` are never used
   --> src/main.rs:184:12
    |
183 |     impl<Cap: MaxFlowCapacity> Edge<Cap> {
    |     ------------------------------------ methods in this implementation
184 |         fn add(&mut self, cap: Cap) {
    |            ^^^
...
187 |         fn sub(&mut self, cap: Cap) {
    | 

ソースコード

diff #

// M<=5 とかでもようわからん
// i=1の場合は max(0, A_i - K) だけ諦めることになる
// 小豆をいくらか捨ててしまう
// 取られたら負けという問題は解けるか?
// グラフ作ればフローになって、最大流流せるかという問題になる
// 終わりじゃない?
// フローは早いで終わるのかな
//
// WA
// あれ?
//
// グラフ作り直してみる
// TLE
// ???
// サンプルはない?
//
// カットでDPというのを忘れとる
// Bの扱いがうまくできてないな
// いや、フローでWAになっとる方が問題

use std::io::Write;
use std::collections::*;

type Map<K, V> = BTreeMap<K, V>;
type Set<T> = BTreeSet<T>;
type Deque<T> = VecDeque<T>;

fn main() {
    input! {
        n: usize,
        m: usize,
        k: i64,
        a: [i64; n],
        b: [usize1; m],
    }
    let mut dp = vec![0; 1 << n];
    for i in 0..(1 << n) {
        for j in 0..n {
            if i >> j & 1 == 0 {
                dp[i] += a[j];
            }
        }
    }
    let mut or = [0; 9];
    for i in 0..n {
        for j in [(i - 1) % n, (i + 1) % n].iter() {
            or[i] |= 1 << *j;
        }
    }
    let inf = a.iter().sum::<i64>();
    for b in b.iter() {
        let mut next = vec![inf; 1 << n];
        for (i, dp) in dp.iter().enumerate() {
            let mut x = i;
            while x > 0 {
                let mut bit = i;
                for j in 0..n {
                    if x >> j & 1 == 1 {
                        bit |= or[j];
                    }
                }
                let c = (i ^ x).count_ones() as i64;
                next[bit].chmin(*dp + k * c);
                x = (x - 1) & i;
            }
            next[i].chmin(*dp + k * i.count_ones() as i64);
        }
        dp = next;
        for i in 0..(1 << n) {
            if i >> b & 1 == 1 {
                let v = dp[i];
                dp[i ^ (1 << b)].chmin(v);
                dp[i] = inf;
            }
        }
        let ans = dp[0];
        println!("{}", ans);
    }
}

// ---------- begin input macro ----------
// reference: https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
#[macro_export]
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
    ($($r:tt)*) => {
        let s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        let mut iter = s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
}

#[macro_export]
macro_rules! input_inner {
    ($iter:expr) => {};
    ($iter:expr, ) => {};
    ($iter:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };
}

#[macro_export]
macro_rules! read_value {
    ($iter:expr, ( $($t:tt),* )) => {
        ( $(read_value!($iter, $t)),* )
    };
    ($iter:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
    };
    ($iter:expr, chars) => {
        read_value!($iter, String).chars().collect::<Vec<char>>()
    };
    ($iter:expr, bytes) => {
        read_value!($iter, String).bytes().collect::<Vec<u8>>()
    };
    ($iter:expr, usize1) => {
        read_value!($iter, usize) - 1
    };
    ($iter:expr, $t:ty) => {
        $iter.next().unwrap().parse::<$t>().expect("Parse error")
    };
}
// ---------- end input macro ----------
// ---------- begin max flow (Dinic) ----------
mod maxflow {
    pub trait MaxFlowCapacity:
        Copy + Ord + std::ops::Add<Output = Self> + std::ops::Sub<Output = Self>
    {
        fn zero() -> Self;
        fn inf() -> Self;
    }

    macro_rules! impl_primitive_integer_capacity {
        ($x:ty, $y:expr) => {
            impl MaxFlowCapacity for $x {
                fn zero() -> Self {
                    0
                }
                fn inf() -> Self {
                    $y
                }
            }
        };
    }

    impl_primitive_integer_capacity!(u32, std::u32::MAX);
    impl_primitive_integer_capacity!(u64, std::u64::MAX);
    impl_primitive_integer_capacity!(i32, std::i32::MAX);
    impl_primitive_integer_capacity!(i64, std::i64::MAX);

    #[derive(Clone)]
    struct Edge<Cap> {
        to_: u32,
        inv_: u32,
        cap_: Cap,
    }

    impl<Cap> Edge<Cap> {
        fn new(to: usize, inv: usize, cap: Cap) -> Self {
            Edge {
                to_: to as u32,
                inv_: inv as u32,
                cap_: cap,
            }
        }
        fn to(&self) -> usize {
            self.to_ as usize
        }
        fn inv(&self) -> usize {
            self.inv_ as usize
        }
    }

    impl<Cap: MaxFlowCapacity> Edge<Cap> {
        fn add(&mut self, cap: Cap) {
            self.cap_ = self.cap_ + cap;
        }
        fn sub(&mut self, cap: Cap) {
            self.cap_ = self.cap_ - cap;
        }
        fn cap(&self) -> Cap {
            self.cap_
        }
    }

    #[derive(Clone)]
    pub struct Graph<Cap> {
        graph: Vec<Vec<Edge<Cap>>>,
    }

    #[allow(dead_code)]
    pub struct EdgeIndex {
        src: usize,
        dst: usize,
        x: usize,
        y: usize,
    }

    impl<Cap: MaxFlowCapacity> Graph<Cap> {
        pub fn new(size: usize) -> Self {
            Self {
                graph: vec![vec![]; size],
            }
        }
        pub fn add_edge(&mut self, src: usize, dst: usize, cap: Cap) -> EdgeIndex {
            assert!(src.max(dst) < self.graph.len());
            assert!(cap >= Cap::zero());
            assert!(src != dst);
            let x = self.graph[src].len();
            let y = self.graph[dst].len();
            self.graph[src].push(Edge::new(dst, y, cap));
            self.graph[dst].push(Edge::new(src, x, Cap::zero()));
            EdgeIndex { src, dst, x, y }
        }
        // src, dst, used, intial_capacity
        #[allow(dead_code)]
        pub fn get_edge(&self, e: &EdgeIndex) -> (usize, usize, Cap, Cap) {
            let max = self.graph[e.src][e.x].cap() + self.graph[e.dst][e.y].cap();
            let used = self.graph[e.dst][e.y].cap();
            (e.src, e.dst, used, max)
        }
        pub fn flow(&mut self, src: usize, dst: usize) -> Cap {
            let size = self.graph.len();
            assert!(src.max(dst) < size);
            assert!(src != dst);
            let mut queue = std::collections::VecDeque::new();
            let mut level = vec![0; size];
            let mut it = vec![0; size];
            let mut ans = Cap::zero();
            loop {
                (|| {
                    level.clear();
                    level.resize(size, 0);
                    level[src] = 1;
                    queue.clear();
                    queue.push_back(src);
                    while let Some(v) = queue.pop_front() {
                        let d = level[v] + 1;
                        for e in self.graph[v].iter() {
                            let u = e.to();
                            if e.cap() > Cap::zero() && level[u] == 0 {
                                level[u] = d;
                                if u == dst {
                                    return;
                                }
                                queue.push_back(u);
                            }
                        }
                    }
                })();
                if level[dst] == 0 {
                    break;
                }
                it.clear();
                it.resize(size, 0);
                loop {
                    let f = self.dfs(dst, src, Cap::inf(), &mut it, &level);
                    if f == Cap::zero() {
                        break;
                    }
                    ans = ans + f;
                }
            }
            ans
        }
        fn dfs(&mut self, v: usize, src: usize, cap: Cap, it: &mut [usize], level: &[u32]) -> Cap {
            if v == src {
                return cap;
            }
            while let Some((u, inv)) = self.graph[v].get(it[v]).map(|p| (p.to(), p.inv())) {
                if level[u] + 1 == level[v] && self.graph[u][inv].cap() > Cap::zero() {
                    let cap = cap.min(self.graph[u][inv].cap());
                    let c = self.dfs(u, src, cap, it, level);
                    if c > Cap::zero() {
                        self.graph[v][it[v]].add(c);
                        self.graph[u][inv].sub(c);
                        return c;
                    }
                }
                it[v] += 1;
            }
            Cap::zero()
        }
    }
}
// ---------- end max flow (Dinic) ----------

// ---------- begin chmin, chmax ----------
pub trait ChangeMinMax {
    fn chmin(&mut self, x: Self) -> bool;
    fn chmax(&mut self, x: Self) -> bool;
}

impl<T: PartialOrd> ChangeMinMax for T {
    fn chmin(&mut self, x: Self) -> bool {
        *self > x && {
            *self = x;
            true
        }
    }
    fn chmax(&mut self, x: Self) -> bool {
        *self < x && {
            *self = x;
            true
        }
    }
}
// ---------- end chmin, chmax ----------
0