結果

問題 No.2321 Continuous Flip
ユーザー akakimidoriakakimidori
提出日時 2023-05-26 21:47:44
言語 Rust
(1.77.0)
結果
AC  
実行時間 217 ms / 2,000 ms
コード長 2,908 bytes
コンパイル時間 1,049 ms
コンパイル使用メモリ 159,980 KB
実行使用メモリ 42,036 KB
最終ジャッジ日時 2023-08-26 11:08:04
合計ジャッジ時間 10,131 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 201 ms
40,368 KB
testcase_05 AC 202 ms
40,364 KB
testcase_06 AC 208 ms
40,384 KB
testcase_07 AC 208 ms
40,352 KB
testcase_08 AC 206 ms
40,404 KB
testcase_09 AC 203 ms
40,276 KB
testcase_10 AC 206 ms
40,360 KB
testcase_11 AC 207 ms
40,308 KB
testcase_12 AC 204 ms
40,344 KB
testcase_13 AC 202 ms
40,400 KB
testcase_14 AC 201 ms
40,372 KB
testcase_15 AC 217 ms
40,636 KB
testcase_16 AC 199 ms
40,284 KB
testcase_17 AC 204 ms
40,360 KB
testcase_18 AC 203 ms
40,376 KB
testcase_19 AC 206 ms
40,364 KB
testcase_20 AC 206 ms
40,356 KB
testcase_21 AC 200 ms
40,292 KB
testcase_22 AC 211 ms
40,664 KB
testcase_23 AC 206 ms
40,384 KB
testcase_24 AC 60 ms
34,984 KB
testcase_25 AC 1 ms
4,376 KB
testcase_26 AC 59 ms
35,632 KB
testcase_27 AC 71 ms
42,036 KB
testcase_28 AC 143 ms
38,964 KB
testcase_29 AC 144 ms
39,756 KB
testcase_30 AC 152 ms
39,032 KB
testcase_31 AC 147 ms
38,760 KB
testcase_32 AC 146 ms
38,668 KB
testcase_33 AC 152 ms
39,492 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: unused import: `std::io::Write`
 --> Main.rs:1:5
  |
1 | use std::io::Write;
  |     ^^^^^^^^^^^^^^
  |
  = note: `#[warn(unused_imports)]` on by default

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

warning: type alias `Set` is never used
 --> Main.rs:5:6
  |
5 | type Set<T> = BTreeSet<T>;
  |      ^^^

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

warning: 4 warnings emitted

ソースコード

diff #

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,
        c: i64,
        a: [i64; n],
        p: [(usize1, usize); m],
    }
    let mut g = vec![vec![]; n + 1];
    for (l, r) in p {
        g[l].push((r, c));
        g[r].push((l, c));
    }
    for i in 1..=n {
        g[i].push((i - 1, a[i - 1]));
        g[i - 1].push((i, a[i - 1]));
    }
    let mut h = std::collections::BinaryHeap::new();
    let mut dp = vec![std::i64::MAX / 2; n + 1];
    dp[0] = 0;
    h.push((0, 0));
    while let Some((d, v)) = h.pop() {
        let d = -d;
        if d > dp[v] {
            continue;
        }
        for &(u, w) in g[v].iter() {
            if dp[u].chmin(d + w) {
                h.push((-dp[u], u));
            }
        }
    }
    let ans = a.iter().sum::<i64>() - dp[n];
    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 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