結果

問題 No.1283 Extra Fee
ユーザー StrorkisStrorkis
提出日時 2020-11-06 22:53:13
言語 Rust
(1.77.0)
結果
MLE  
実行時間 -
コード長 3,049 bytes
コンパイル時間 3,042 ms
コンパイル使用メモリ 153,232 KB
実行使用メモリ 812,796 KB
最終ジャッジ日時 2023-09-29 19:22:50
合計ジャッジ時間 6,956 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 MLE -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

pub mod procon_input {
    use std::{str::FromStr, iter::FromIterator};

    pub fn read_line() -> String {
        let mut input = String::new();
        std::io::stdin().read_line(&mut input).ok();
        input
    }

    pub fn parse<T: FromStr>(s: &str) -> T {
        s.parse().ok().unwrap()
    }
    
    pub fn read<T: FromStr>() -> T {
        parse(read_line().trim_end())
    }
    
    pub fn read_collection<T: FromStr, C: FromIterator<T>>() -> C {
        read_line().split_whitespace().map(parse).collect()
    }

    #[macro_export]
    macro_rules! read_tuple {
        ( $( $t:ty ),* ) => {{
            let input = read_line();
            let mut iter = input.split_whitespace();
            ( $( parse::<$t>(iter.next().unwrap()) ),* )
        }};
    }
}

use procon_input::*;

fn dijkstra(
    graph: &Vec<Vec<usize>>, tolls: &Vec<i64>
) -> Vec<Vec<i64>> {
    use std::collections::BinaryHeap;
    use std::cmp::Reverse;

    let n = graph.len();
    let mut heap = BinaryHeap::new();
    let mut dist = vec![vec![1 << 60; n]; n];
    for s in 0..n {
        heap.push((Reverse(tolls[s]), s));
        dist[s][s] = tolls[s];
        while let Some((Reverse(cost), from)) = heap.pop() {
            for &to in &graph[from] {
                let next_cost = cost + 1 + tolls[to];
                if next_cost < dist[s][to] {
                    heap.push((Reverse(next_cost), to));
                    dist[s][to] = next_cost;
                }
            }
        }
    }
    dist
}

fn solve(writer: &mut std::io::BufWriter<std::io::StdoutLock>) {
    use std::io::Write;

    let (n, m) = read_tuple!(usize, usize);

    let mut graph = vec![vec![]; n * n];
    for h in 0..n {
        for w in 0..n {
            let u = h * n + w;
            if w + 1 < n {
                let v = h * n + (w + 1);
                graph[u].push(v);
                graph[v].push(u);
            }
            if h + 1 < n {
                let v = (h + 1) * n + w;
                graph[u].push(v);
                graph[v].push(u);
            }
        }
    }

    let mut tolls = vec![0; n * n];
    for _ in 0..m {
        let (h, w, c) = read_tuple!(usize, usize, i64);
        tolls[(h - 1) * n + (w - 1)] = c;
    }

    let dist = dijkstra(&graph, &tolls);
    let mut ans = dist[0][n * n - 1];
    for x in 0..(n * n) {
        if tolls[x] == 0 { continue; }

        use std::cmp::min;
        let mut prev = std::i64::MAX;
        if x % n > 0 {
            prev = min(prev, dist[0][x - 1]);
        }
        if x / n > 0 {
            prev = min(prev, dist[0][x - n])
        }
        let mut next = std::i64::MAX;
        if x % n + 1 < n {
            next = min(next, dist[x + 1][n * n - 1]);
        }
        if x / n + 1 < n {
            next = min(next, dist[x + n][n * n - 1]);
        }
        ans = min(ans, prev + next + 2);
    }
    writeln!(writer, "{}", ans).ok();
}

fn main() {
    let stdout = std::io::stdout();
    let mut writer = std::io::BufWriter::new(stdout.lock());
    solve(&mut writer);
}
0