結果

問題 No.1812 Uribo Road
ユーザー koba-e964koba-e964
提出日時 2022-02-17 22:59:24
言語 Rust
(1.77.0 + proconio)
結果
AC  
実行時間 76 ms / 5,000 ms
コード長 3,592 bytes
コンパイル時間 13,370 ms
コンパイル使用メモリ 377,972 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-06-29 07:52:52
合計ジャッジ時間 14,314 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 1 ms
6,944 KB
testcase_02 AC 1 ms
6,940 KB
testcase_03 AC 7 ms
6,944 KB
testcase_04 AC 1 ms
6,940 KB
testcase_05 AC 1 ms
6,940 KB
testcase_06 AC 1 ms
6,944 KB
testcase_07 AC 1 ms
6,940 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 3 ms
6,944 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 2 ms
6,940 KB
testcase_12 AC 40 ms
6,940 KB
testcase_13 AC 20 ms
6,940 KB
testcase_14 AC 20 ms
6,944 KB
testcase_15 AC 24 ms
6,940 KB
testcase_16 AC 25 ms
6,940 KB
testcase_17 AC 61 ms
6,944 KB
testcase_18 AC 16 ms
6,944 KB
testcase_19 AC 75 ms
6,940 KB
testcase_20 AC 76 ms
6,944 KB
testcase_21 AC 62 ms
6,940 KB
testcase_22 AC 63 ms
6,944 KB
testcase_23 AC 3 ms
6,940 KB
testcase_24 AC 1 ms
6,944 KB
testcase_25 AC 10 ms
6,944 KB
testcase_26 AC 1 ms
6,940 KB
testcase_27 AC 11 ms
6,944 KB
testcase_28 AC 19 ms
6,944 KB
testcase_29 AC 1 ms
6,944 KB
testcase_30 AC 3 ms
6,940 KB
testcase_31 AC 38 ms
6,944 KB
testcase_32 AC 1 ms
6,940 KB
testcase_33 AC 30 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::cmp::*;
// 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, usize1) => (read_value!($next, usize) - 1);
    ($next:expr, $t:ty) => ($next().parse::<$t>().expect("Parse error"));
}

/*
 * Dijkstra's algorithm.
 * Verified by: AtCoder ABC164 (https://atcoder.jp/contests/abc164/submissions/12423853)
 */

struct Dijkstra {
    edges: Vec<Vec<(usize, i64)>>, // adjacent list representation
}

impl Dijkstra {
    fn new(n: usize) -> Self {
        Dijkstra { edges: vec![Vec::new(); n] }
    }
    fn add_edge(&mut self, from: usize, to: usize, cost: i64) {
        self.edges[from].push((to, cost));
    }
    /*
     * This function returns a Vec consisting of the distances from vertex source.
     */
    fn solve(&self, source: usize, inf: i64) -> Vec<i64> {
        let n = self.edges.len();
        let mut d = vec![inf; n];
        // que holds (-distance, vertex), so that que.pop() returns the nearest element.
        let mut que = std::collections::BinaryHeap::new();
        que.push((0, source));
        while let Some((cost, pos)) = que.pop() {
            let cost = -cost;
            if d[pos] <= cost {
                continue;
            }
            d[pos] = cost;
            for &(w, c) in &self.edges[pos] {
                let newcost = cost + c;
                if d[w] > newcost {
                    d[w] = newcost + 1;
                    que.push((-newcost, w));
                }
            }
        }
        return d;
    }
}

fn main() {
    input! {
        n: usize, m: usize, k: usize,
        r: [usize1; k],
        abc: [(usize1, usize1, i64); m],
    }
    let mut dijk = Dijkstra::new(n);
    for &(a, b, c) in &abc {
        dijk.add_edge(a, b, c);
        dijk.add_edge(b, a, c);
    }
    const INF: i64 = 1 << 50;
    let s0 = dijk.solve(0, INF);
    let s1 = dijk.solve(n - 1, INF);
    let mut s = vec![vec![]; 2 * k];
    let mut v = vec![0; 2 * k];
    for i in 0..k {
        let (a, b, _) = abc[r[i]];
        s[2 * i] = dijk.solve(a, INF);
        s[2 * i + 1] = dijk.solve(b, INF);
        v[2 * i] = a;
        v[2 * i + 1] = b;
    }
    let mut dp = vec![vec![INF; 1 << k]; 2 * k];
    for i in 0..2 * k {
        dp[i ^ 1][1 << (i / 2)] = s0[v[i]] + abc[r[i / 2]].2;
    }
    for bits in 1..1 << k {
        for j in 0..2 * k {
            if (bits & 1 << (j / 2)) == 0 { continue; }
            let pre = bits ^ 1 << (j / 2);
            for l in 0..2 * k {
                dp[j][bits] = min(dp[j][bits], dp[l][pre] + s[l][v[j ^ 1]] + abc[r[j / 2]].2);
            }
        }
    }
    let mut ans = INF;
    for j in 0..2 * k {
        ans = min(ans, dp[j][(1 << k) - 1] + s1[v[j]]);
    }
    println!("{}", ans);
}
0