結果

問題 No.1812 Uribo Road
ユーザー koba-e964koba-e964
提出日時 2022-02-17 22:38:01
言語 Rust
(1.77.0 + proconio)
結果
WA  
実行時間 -
コード長 3,586 bytes
コンパイル時間 12,595 ms
コンパイル使用メモリ 407,620 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-06-29 07:52:16
合計ジャッジ時間 14,392 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

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