結果
| 問題 |
No.788 トラックの移動
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2022-12-05 23:46:56 |
| 言語 | Rust (1.83.0 + proconio) |
| 結果 |
AC
|
| 実行時間 | 377 ms / 2,000 ms |
| コード長 | 2,015 bytes |
| コンパイル時間 | 12,390 ms |
| コンパイル使用メモリ | 403,192 KB |
| 実行使用メモリ | 33,408 KB |
| 最終ジャッジ日時 | 2024-10-12 19:55:38 |
| 合計ジャッジ時間 | 15,352 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 14 |
ソースコード
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);
}