結果

問題 No.788 トラックの移動
ユーザー phsplsphspls
提出日時 2022-12-05 23:46:56
言語 Rust
(1.77.0)
結果
AC  
実行時間 365 ms / 2,000 ms
コード長 2,015 bytes
コンパイル時間 1,084 ms
コンパイル使用メモリ 174,904 KB
実行使用メモリ 33,408 KB
最終ジャッジ日時 2024-04-20 21:40:56
合計ジャッジ時間 3,903 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 365 ms
33,408 KB
testcase_01 AC 1 ms
6,816 KB
testcase_02 AC 1 ms
6,944 KB
testcase_03 AC 0 ms
6,944 KB
testcase_04 AC 80 ms
9,856 KB
testcase_05 AC 348 ms
33,408 KB
testcase_06 AC 355 ms
33,408 KB
testcase_07 AC 1 ms
6,940 KB
testcase_08 AC 1 ms
6,940 KB
testcase_09 AC 0 ms
6,944 KB
testcase_10 AC 1 ms
6,940 KB
testcase_11 AC 1 ms
6,940 KB
testcase_12 AC 1 ms
6,944 KB
testcase_13 AC 1 ms
6,944 KB
testcase_14 AC 1 ms
6,944 KB
testcase_15 AC 125 ms
33,408 KB
testcase_16 AC 316 ms
33,408 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::{cmp::Reverse, collections::BinaryHeap};

const INF: usize = 1usize << 60;

fn dijkstra(start: usize, paths: &Vec<Vec<(usize, usize)>>) -> Vec<usize> {
    let mut costs = vec![INF; paths.len()];
    let mut que = BinaryHeap::new();
    que.push(Reverse((0, start)));
    costs[start] = 0;
    while let Some(Reverse((cost, dest))) = que.pop() {
        if cost > costs[dest] { continue; }
        for &(v, ncost) in paths[dest].iter() {
            if costs[dest] + ncost < costs[v] {
                costs[v] = costs[dest] + ncost;
                que.push(Reverse((costs[v], v)));
            }
        }
    }
    costs
}

fn main() {
    let mut nml = String::new();
    std::io::stdin().read_line(&mut nml).ok();
    let nml: Vec<usize> = nml.trim().split_whitespace().map(|s| s.parse().unwrap()).collect();
    let n = nml[0];
    let m = nml[1];
    let l = nml[2] - 1;
    let mut t = String::new();
    std::io::stdin().read_line(&mut t).ok();
    let t: Vec<usize> = t.trim().split_whitespace().map(|s| s.parse().unwrap()).collect();
    let mut paths = vec![vec![]; n];
    for _ in 0..m {
        let mut temp = String::new();
        std::io::stdin().read_line(&mut temp).ok();
        let temp: Vec<usize> = temp.trim().split_whitespace().map(|s| s.parse().unwrap()).collect();
        let a= temp[0]-1;
        let b= temp[1]-1;
        let c= temp[2];
        paths[a].push((b, c));
        paths[b].push((a, c));
    }

    if t.iter().filter(|&&v| v > 0).count() == 1 {
        println!("0");
        return;
    }
    let mut result = INF;
    let mut mapping = Vec::with_capacity(n);
    for i in 0..n {
        mapping.push(dijkstra(i, &paths));
    }
    for i in 0..n {
        let moving = (0..n).filter(|&j| t[j] > 0).map(|j| mapping[i][l] + 2*mapping[i][j] - mapping[j][l] - mapping[i][j]).max().unwrap();
        let total = (0..n).map(|j| t[j] * mapping[i][j]).sum::<usize>() * 2 + mapping[i][l] - moving;
        result = result.min(total);
    }
    println!("{}", result);
}
0