結果
| 問題 |
No.1995 CHIKA Road
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2022-09-20 00:20:59 |
| 言語 | Rust (1.83.0 + proconio) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 3,261 bytes |
| コンパイル時間 | 16,425 ms |
| コンパイル使用メモリ | 380,192 KB |
| 実行使用メモリ | 53,184 KB |
| 最終ジャッジ日時 | 2024-12-22 02:58:04 |
| 合計ジャッジ時間 | 19,025 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 31 WA * 6 |
コンパイルメッセージ
warning: field `n` is never read
--> src/main.rs:7:5
|
6 | struct Dijkstra {
| -------- field in this struct
7 | n: usize,
| ^
|
= note: `Dijkstra` has derived impls for the traits `Clone` and `Debug`, but these are intentionally ignored during dead code analysis
= note: `#[warn(dead_code)]` on by default
warning: method `reconstruct` is never used
--> src/main.rs:49:8
|
13 | impl Dijkstra {
| ------------- method in this implementation
...
49 | fn reconstruct(&self, startpoint: usize, endpoint: usize) -> Vec<(usize, usize)> {
| ^^^^^^^^^^^
ソースコード
use std::{cmp::Reverse, collections::{BinaryHeap, BTreeSet, HashMap}};
const INF: usize = 1usize << 60;
#[derive(Debug, Clone)]
struct Dijkstra {
n: usize,
pathcosts: Vec<Vec<(usize, usize)>>,
pathcostsrev: Vec<Vec<(usize, usize)>>,
costs: Vec<usize>
}
impl Dijkstra {
fn new(n: usize) -> Self {
Self {
n: n
, pathcosts: vec![vec![]; n]
, pathcostsrev: vec![vec![]; n]
, costs: vec![INF; n]
}
}
fn pusha2b(&mut self, a: usize, b: usize, cost: usize) {
self.pathcosts[a].push((cost, b));
self.pathcostsrev[b].push((cost, a));
}
fn get_cost(&self, idx: usize) -> usize {
self.costs[idx]
}
fn solve(&mut self, startpoint: usize) {
let mut que = BinaryHeap::new();
que.push(Reverse((0, startpoint)));
self.costs[startpoint] = 0;
while let Some(Reverse(p)) = que.pop() {
let cost = p.0;
let dest = p.1;
if cost > self.costs[dest] { continue; }
for &p2 in self.pathcosts[dest].iter() {
if self.costs[dest] + p2.0 < self.costs[p2.1] {
self.costs[p2.1] = self.costs[dest] + p2.0;
que.push(Reverse((self.costs[p2.1], p2.1)));
}
}
}
}
fn reconstruct(&self, startpoint: usize, endpoint: usize) -> Vec<(usize, usize)> {
let mut ret = vec![];
if self.costs[endpoint] == INF { return ret; }
let mut current = endpoint;
while current != startpoint {
for &(cost, u) in self.pathcostsrev[current].iter() {
if self.costs[current] == self.costs[u] + cost {
let prev = current;
current = u;
ret.push((current, prev));
break;
}
}
}
ret.reverse();
ret
}
}
fn main() {
let mut nm = String::new();
std::io::stdin().read_line(&mut nm).ok();
let nm: Vec<usize> = nm.trim().split_whitespace().map(|s| s.parse().unwrap()).collect();
let n = nm[0];
let m = nm[1];
let mut lines = Vec::with_capacity(m);
let mut used = BTreeSet::new();
used.insert(n-1);
for _ in 0..m {
let mut ab = String::new();
std::io::stdin().read_line(&mut ab).ok();
let ab: Vec<usize> = ab.trim().split_whitespace().map(|s| s.parse().unwrap()).collect();
let a = ab[0];
let b = ab[1];
let dist = 2*b - 2*a - 1;
let a = a-1;
let b = b-1;
lines.push((a, b, dist));
used.insert(a);
used.insert(b);
}
let mut mapping = HashMap::new();
for (i, &v) in used.iter().enumerate() {
mapping.insert(v, i);
}
let mut paths = Dijkstra::new(used.len());
for &(u, v, w) in lines.iter() {
paths.pusha2b(*mapping.get(&u).unwrap(), *mapping.get(&v).unwrap(), w)
}
let used = used.into_iter().collect::<Vec<usize>>();
for i in 0..used.len()-1 {
let u = used[i];
let v = used[i+1];
let dist = (v - u) * 2;
paths.pusha2b(i, i+1, dist);
}
paths.solve(0);
println!("{}", paths.get_cost(used.len()-1));
}