提出 #58171789


ソースコード 拡げる

#![allow(unused)]

fn main() {
    let inp = readv::<usize>();
    let (n, m) = (inp[0], inp[1]);
    let inf = 1 << 60;
    let mut adj = vec![vec![inf; n]; n];
    let mut es = vec![];
    for u in 0..n {
        adj[u][u] = 0;
    }
    for _ in 0..m {
        let edge = readv::<i64>();
        let u = edge[0] as usize - 1;
        let v = edge[1] as usize - 1;
        let w = edge[2];
        es.push((u, v, w));
        adj[u][v] = adj[u][v].min(w);
        adj[v][u] = adj[v][u].min(w);
    }

    let dis = floyd_warshall(&adj, inf);

    let q = read::<usize>();
    let mut ans = vec![i64::MAX; q];
    for qid in 0..q {
        let k = read::<usize>();
        let b = readv::<usize>();
        let b = mapv(&b, |&x| x - 1);

        for perm in perm_iter(k) {
            for mask in 0..(1 << k) {
                let mut last = 0;
                let mut cost = 0;
                for i in 0..k {
                    let (mut u, mut v, w) = es[b[perm[i]]];
                    if (mask >> i) & 1 == 1 {
                        (u, v) = (v, u);
                    }
                    cost += dis[last][u]; // last -> u
                    cost += w; // u -> v
                    last = v;
                }
                cost += dis[last][n - 1];
                ans[qid] = ans[qid].min(cost);
            }
        }
    }

    println!("{}", join(&ans, "\n"));
}

fn floyd_warshall(adj: &Vec<Vec<i64>>, inf: i64) -> Vec<Vec<i64>> {
    // dp[k][u][v] = minimum distance from u to v using vertices 0..=k as intermediate
    // dp[0][u][v] = adj[u][v]
    // dp[k][u][v] = min(dp[k - 1][u][v], dp[k - 1][u][k] + dp[k - 1][k][v]);
    let n = adj.len();
    let mut dp = vec![vec![i64::MAX; n]; n];
    dp = adj.clone();
    for k in 0..n {
        for u in 0..n {
            for v in 0..n {
                if dp[u][k] != inf && dp[k][v] != inf {
                    dp[u][v] = dp[u][v].min(dp[u][k] + dp[k][v]);
                }
            }
        }
    }
    dp
}

fn next_perm<T: Ord>(arr: &mut [T]) -> Option<()> {
    let k = arr.windows(2).rposition(|w| w[0] < w[1])?;
    let j = arr.iter().rposition(|a| a > &arr[k]).unwrap();
    arr.swap(k, j);
    arr[(k + 1)..].reverse();
    Some(())
}

fn perm_iter(n: usize) -> impl std::iter::Iterator<Item = Vec<usize>> {
    let mut perm: Vec<usize> = (0..n).collect();
    let iter1 = std::iter::once(perm.clone());
    let iter2 = std::iter::from_fn(move || next_perm(&mut perm).and_then(|_| Some(perm.clone())));
    iter1.chain(iter2)
}

fn read<T: std::str::FromStr>() -> T {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    s.trim().parse().ok().unwrap()
}

fn readv<T: std::str::FromStr>() -> Vec<T> {
    read::<String>()
        .split_ascii_whitespace()
        .map(|t| t.parse().ok().unwrap())
        .collect()
}

fn reads() -> Vec<char> {
    read::<String>().chars().collect()
}

fn mapv<T, S, F: Fn(&T) -> S>(arr: &Vec<T>, f: F) -> Vec<S> {
    arr.iter().map(f).collect()
}

fn join<T: ToString>(arr: &[T], sep: &str) -> String {
    arr.iter()
        .map(|x| x.to_string())
        .collect::<Vec<String>>()
        .join(sep)
}

提出情報

提出日時
問題 E - Sightseeing Tour
ユーザ amoshuangyc
言語 Rust (rustc 1.70.0)
得点 450
コード長 3271 Byte
結果 AC
実行時間 290 ms
メモリ 10352 KiB

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 450 / 450
結果
AC × 3
AC × 39
セット名 テストケース
Sample example_00.txt, example_01.txt, example_02.txt
All example_00.txt, example_01.txt, example_02.txt, hand_00.txt, hand_01.txt, hand_02.txt, hand_03.txt, hand_04.txt, hand_05.txt, random_00.txt, random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, random_19.txt, random_20.txt, random_21.txt, random_22.txt, random_23.txt, random_24.txt, random_25.txt, random_26.txt, random_27.txt, random_28.txt, random_29.txt
ケース名 結果 実行時間 メモリ
example_00.txt AC 1 ms 1880 KiB
example_01.txt AC 1 ms 2080 KiB
example_02.txt AC 1 ms 2076 KiB
hand_00.txt AC 206 ms 5412 KiB
hand_01.txt AC 206 ms 5612 KiB
hand_02.txt AC 57 ms 6796 KiB
hand_03.txt AC 276 ms 7276 KiB
hand_04.txt AC 290 ms 10352 KiB
hand_05.txt AC 216 ms 5464 KiB
random_00.txt AC 102 ms 5592 KiB
random_01.txt AC 223 ms 5568 KiB
random_02.txt AC 222 ms 5456 KiB
random_03.txt AC 126 ms 5512 KiB
random_04.txt AC 230 ms 5188 KiB
random_05.txt AC 231 ms 5520 KiB
random_06.txt AC 137 ms 5516 KiB
random_07.txt AC 255 ms 5544 KiB
random_08.txt AC 258 ms 5520 KiB
random_09.txt AC 133 ms 5524 KiB
random_10.txt AC 255 ms 5676 KiB
random_11.txt AC 253 ms 5560 KiB
random_12.txt AC 143 ms 5548 KiB
random_13.txt AC 255 ms 5572 KiB
random_14.txt AC 253 ms 5536 KiB
random_15.txt AC 144 ms 5572 KiB
random_16.txt AC 266 ms 6040 KiB
random_17.txt AC 255 ms 5604 KiB
random_18.txt AC 147 ms 5716 KiB
random_19.txt AC 262 ms 5848 KiB
random_20.txt AC 264 ms 6116 KiB
random_21.txt AC 149 ms 6344 KiB
random_22.txt AC 265 ms 6384 KiB
random_23.txt AC 263 ms 6148 KiB
random_24.txt AC 152 ms 7188 KiB
random_25.txt AC 265 ms 6444 KiB
random_26.txt AC 268 ms 7168 KiB
random_27.txt AC 154 ms 7428 KiB
random_28.txt AC 276 ms 9224 KiB
random_29.txt AC 273 ms 7992 KiB