結果

問題 No.2764 Warp Drive Spacecraft
ユーザー akakimidoriakakimidori
提出日時 2024-05-17 21:25:04
言語 Rust
(1.77.0)
結果
AC  
実行時間 350 ms / 3,000 ms
コード長 6,094 bytes
コンパイル時間 12,569 ms
コンパイル使用メモリ 401,460 KB
実行使用メモリ 37,268 KB
最終ジャッジ日時 2024-05-18 00:10:13
合計ジャッジ時間 18,805 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 1 ms
6,944 KB
testcase_02 AC 1 ms
6,944 KB
testcase_03 AC 1 ms
6,944 KB
testcase_04 AC 1 ms
6,940 KB
testcase_05 AC 1 ms
6,944 KB
testcase_06 AC 1 ms
6,944 KB
testcase_07 AC 1 ms
6,944 KB
testcase_08 AC 1 ms
6,940 KB
testcase_09 AC 0 ms
6,944 KB
testcase_10 AC 1 ms
6,940 KB
testcase_11 AC 1 ms
6,944 KB
testcase_12 AC 2 ms
6,940 KB
testcase_13 AC 2 ms
6,944 KB
testcase_14 AC 1 ms
6,940 KB
testcase_15 AC 1 ms
6,940 KB
testcase_16 AC 88 ms
33,480 KB
testcase_17 AC 89 ms
33,472 KB
testcase_18 AC 88 ms
33,288 KB
testcase_19 AC 192 ms
26,016 KB
testcase_20 AC 179 ms
27,928 KB
testcase_21 AC 188 ms
27,516 KB
testcase_22 AC 192 ms
27,300 KB
testcase_23 AC 195 ms
27,516 KB
testcase_24 AC 189 ms
28,044 KB
testcase_25 AC 186 ms
26,292 KB
testcase_26 AC 310 ms
34,084 KB
testcase_27 AC 320 ms
34,396 KB
testcase_28 AC 329 ms
35,236 KB
testcase_29 AC 335 ms
35,236 KB
testcase_30 AC 318 ms
34,792 KB
testcase_31 AC 350 ms
37,268 KB
testcase_32 AC 337 ms
36,904 KB
testcase_33 AC 69 ms
22,040 KB
testcase_34 AC 65 ms
22,088 KB
testcase_35 AC 66 ms
22,060 KB
testcase_36 AC 112 ms
32,648 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: unused import: `std::io::Write`
 --> src/main.rs:1:5
  |
1 | use std::io::Write;
  |     ^^^^^^^^^^^^^^
  |
  = note: `#[warn(unused_imports)]` on by default

warning: type alias `Map` is never used
 --> src/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
 --> src/main.rs:5:6
  |
5 | type Set<T> = BTreeSet<T>;
  |      ^^^

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

ソースコード

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,
        w: [i64; n],
        e: [(usize1, usize1, i64); m],
    }
    let mut g = vec![vec![]; n];
    for (a, b, c) in e {
        g[a].push((b, c));
        g[b].push((a, c));
    }
    let inf = std::i64::MAX / 2;
    let calc = |s: usize| -> Vec<i64> {
        let mut dp = vec![inf; n];
        dp[s] = 0;
        let mut h = BinaryHeap::new();
        h.push((0, s));
        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));
                }
            }
        }
        dp
    };
    let a = calc(0);
    let b = calc(n - 1);
    let mut ans = a[n - 1];
    let mut cht = IncrementalCHT::new();
    for (w, b) in w.iter().zip(b.iter()) {
        cht.add_line(*w, *b);
    }
    for (w, a) in w.iter().zip(a.iter()) {
        ans.chmin(cht.find(*w) + *a);
    }
    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 ----------
// 以下のクエリを処理する
//   add_line(a, b): 直線 ax + b を追加
//   find(x): min (ax + b) を返す。空のとき呼ぶとREになる
// 計算量
// 直線追加クエリがN回飛んでくるとする
// 直線追加 償却O(log N)
// 点質問: O((log N)^2)
// ---------- begin incremental convex hull trick (min) ----------
// reference: https://yukicoder.me/wiki/decomposable_searching_problem
// verify: https://old.yosupo.jp/submission/35150
#[derive(Clone)]
struct ConvexHullTrick {
    line: Vec<(i64, i64)>,
}

impl ConvexHullTrick {
    fn new(mut line: Vec<(i64, i64)>) -> Self {
        assert!(line.len() > 0);
        line.sort();
        line.dedup_by(|a, b| a.0 == b.0);
        let mut stack: Vec<(i64, i64)> = vec![];
        for (a, b) in line {
            while stack.len() >= 2 {
                let len = stack.len();
                let (c, d) = stack[len - 1];
                let (e, f) = stack[len - 2];
                let x = (d - b).div_euclid(a - c);
                let y = (f - d).div_euclid(c - e);
                if x >= y {
                    stack.pop();
                } else {
                    break;
                }
            }
            stack.push((a, b));
        }
        ConvexHullTrick { line: stack }
    }
    fn find(&self, x: i64) -> i64 {
        let mut l = 0;
        let mut r = self.line.len() - 1;
        let line = &self.line;
        let func = |k: usize| -> i64 {
            let (a, b) = line[k];
            a * x + b
        };
        while r - l >= 3 {
            let ll = (2 * l + r) / 3;
            let rr = (l + 2 * r) / 3;
            if func(ll) <= func(rr) {
                r = rr;
            } else {
                l = ll;
            }
        }
        line[l..=r].iter().map(|p| p.0 * x + p.1).min().unwrap()
    }
}

#[derive(Clone, Default)]
pub struct IncrementalCHT {
    size: usize,
    cht: Vec<(ConvexHullTrick, usize)>,
}

impl IncrementalCHT {
    pub fn new() -> Self {
        IncrementalCHT { 
            size: 0,
            cht: vec![]
        }
    }
    pub fn add_line(&mut self, a: i64, b: i64) {
        self.size += 1;
        let mut line = vec![(a, b)];
        let mut p = 0;
        while self.cht.last().map_or(false, |q| q.1 == p) {
            p += 1;
            line.append(&mut self.cht.pop().unwrap().0.line);
        }
        let cht = ConvexHullTrick::new(line);
        self.cht.push((cht, p));
    }
    pub fn find(&self, x: i64) -> i64 {
        self.cht.iter().map(|p| p.0.find(x)).min().unwrap()
    }
    pub fn append(&mut self, other: &mut Self) {
        if self.size < other.size {
            std::mem::swap(self, other);
        }
        for (mut cht, _) in other.cht.drain(..) {
            for (a, b) in cht.line.drain(..) {
                self.add_line(a, b);
            }
        }
        other.size = 0;
    }
}
// ---------- end incremental convex hull trick (min) ----------

// ---------- 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