Submission #4014722


Source Code Expand

#[allow(unused_imports)]
use std::cmp::*;
#[allow(unused_imports)]
use std::collections::*;
use std::io::{Write, BufWriter};
// 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, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };

    ($next:expr, usize1) => {
        read_value!($next, usize) - 1
    };

    ($next:expr, [ $t:tt ]) => {{
        let len = read_value!($next, usize);
        (0..len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    }};

    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}

fn solve() {
    let out = std::io::stdout();
    let mut out = BufWriter::new(out.lock());
    macro_rules! puts {
        ($($format:tt)*) => (write!(out,$($format)*).unwrap());
    }
    input! {
        n: usize,
        d: i64,
        a: [i64; n],
    }
    let mut g = vec![Vec::new(); 2 * n];
    for i in 0 .. n {
        g[i].push((n + i, a[i]));
        g[n + i].push((i, a[i]));
    }
    for i in 0 .. n - 1 {
        g[n + i].push((n + i + 1, d));
        g[n + i + 1].push((n + i, d));
    }
    let mut que = BinaryHeap::new();
    const INF: i64 = 1 << 50;
    let mut dp = vec![(INF, 2 * n); 2 * n];
    for i in 0 .. n {
        que.push((0, i, i));
    }
    let mut tot = 0;
    while let Some((dist, v, w)) = que.pop() {
        let dist = -dist;
        if dp[w].0 == INF {
            dp[w] = (dist, v);
            for &(u, cost) in g[w].iter() { 
                que.push((-dist - cost, v, u));
            }
        } else {
            // dp[w].1 and v meets
            if v != dp[w].1 {
                tot += dp[w].0 + dist;
            }
        }
    }
    puts!("{}\n", tot / 2);
}

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();
}

Submission Info

Submission Time
Task E - Connecting Cities
User kobae964
Language Rust (1.15.1)
Score 600
Code Size 3012 Byte
Status AC
Exec Time 428 ms
Memory 71160 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 600 / 600
Status
AC × 4
AC × 34
Set Name Test Cases
Sample s1.txt, s2.txt, s3.txt, s4.txt
All 01.txt, 02.txt, 03.txt, 04.txt, 05.txt, 06.txt, 07.txt, 08.txt, 09.txt, 10.txt, 11.txt, 12.txt, 13.txt, 14.txt, 15.txt, 16.txt, 17.txt, 18.txt, 19.txt, 20.txt, 21.txt, 22.txt, 23.txt, 24.txt, 25.txt, 26.txt, 27.txt, 28.txt, 29.txt, 30.txt, s1.txt, s2.txt, s3.txt, s4.txt
Case Name Status Exec Time Memory
01.txt AC 362 ms 71160 KiB
02.txt AC 355 ms 71160 KiB
03.txt AC 361 ms 71160 KiB
04.txt AC 339 ms 71160 KiB
05.txt AC 340 ms 71160 KiB
06.txt AC 318 ms 71160 KiB
07.txt AC 333 ms 71160 KiB
08.txt AC 340 ms 71160 KiB
09.txt AC 338 ms 71160 KiB
10.txt AC 319 ms 71160 KiB
11.txt AC 399 ms 71160 KiB
12.txt AC 385 ms 71160 KiB
13.txt AC 428 ms 71160 KiB
14.txt AC 415 ms 71160 KiB
15.txt AC 415 ms 71160 KiB
16.txt AC 317 ms 71160 KiB
17.txt AC 318 ms 71160 KiB
18.txt AC 324 ms 71160 KiB
19.txt AC 315 ms 71160 KiB
20.txt AC 322 ms 71160 KiB
21.txt AC 271 ms 65016 KiB
22.txt AC 259 ms 65016 KiB
23.txt AC 249 ms 65016 KiB
24.txt AC 270 ms 65016 KiB
25.txt AC 246 ms 65016 KiB
26.txt AC 253 ms 65016 KiB
27.txt AC 254 ms 65016 KiB
28.txt AC 250 ms 65016 KiB
29.txt AC 268 ms 65016 KiB
30.txt AC 254 ms 65016 KiB
s1.txt AC 3 ms 8572 KiB
s2.txt AC 3 ms 8572 KiB
s3.txt AC 3 ms 8572 KiB
s4.txt AC 3 ms 8572 KiB