結果
問題 | No.860 買い物 |
ユーザー | fukafukatani |
提出日時 | 2019-08-09 23:45:41 |
言語 | Rust (1.77.0 + proconio) |
結果 |
AC
|
実行時間 | 58 ms / 1,000 ms |
コード長 | 3,225 bytes |
コンパイル時間 | 14,128 ms |
コンパイル使用メモリ | 399,156 KB |
実行使用メモリ | 16,152 KB |
最終ジャッジ日時 | 2024-07-19 15:58:12 |
合計ジャッジ時間 | 16,059 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
6,812 KB |
testcase_01 | AC | 1 ms
6,944 KB |
testcase_02 | AC | 1 ms
6,944 KB |
testcase_03 | AC | 1 ms
6,944 KB |
testcase_04 | AC | 1 ms
6,940 KB |
testcase_05 | AC | 1 ms
6,944 KB |
testcase_06 | AC | 3 ms
6,944 KB |
testcase_07 | AC | 58 ms
14,928 KB |
testcase_08 | AC | 58 ms
14,880 KB |
testcase_09 | AC | 56 ms
16,152 KB |
testcase_10 | AC | 57 ms
15,784 KB |
testcase_11 | AC | 44 ms
15,752 KB |
testcase_12 | AC | 44 ms
14,756 KB |
testcase_13 | AC | 54 ms
15,264 KB |
testcase_14 | AC | 58 ms
14,836 KB |
testcase_15 | AC | 44 ms
14,920 KB |
testcase_16 | AC | 54 ms
15,368 KB |
testcase_17 | AC | 34 ms
13,552 KB |
コンパイルメッセージ
warning: unused variable: `i` --> src/main.rs:21:9 | 21 | for i in 0..n { | ^ help: if this is intentional, prefix it with an underscore: `_i` | = note: `#[warn(unused_variables)]` on by default
ソースコード
#![allow(unused_imports)] #![allow(non_snake_case)] use std::cmp::*; use std::collections::*; use std::io::Write; #[allow(unused_macros)] macro_rules! debug { ($($e:expr),*) => { #[cfg(debug_assertions)] $({ let (e, mut err) = (stringify!($e), std::io::stderr()); writeln!(err, "{} = {:?}", e, $e).unwrap() })* }; } fn main() { let n = read::<usize>(); let mut products = vec![]; for i in 0..n { let v = read_vec::<i64>(); products.push((v[0], v[1])); } let mut edges: Vec<Edge> = Vec::new(); for (i, &p) in products.iter().enumerate() { if i != 0 { edges.push(Edge { from: i, to: i + 1, cost: p.1, }); } edges.push(Edge { from: 0, to: i + 1, cost: p.0, }); } println!( "{}", products.iter().map(|x| x.0).sum::<i64>() + kruskal(&edges, n + 1) ); } 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 read_vec<T: std::str::FromStr>() -> Vec<T> { read::<String>() .split_whitespace() .map(|e| e.parse().ok().unwrap()) .collect() } use std::cmp::Ordering; #[derive(Debug, Clone)] struct Edge { from: usize, to: usize, cost: i64, } impl PartialEq for Edge { fn eq(&self, other: &Edge) -> bool { self.cost == other.cost } } impl Eq for Edge {} impl Ord for Edge { fn cmp(&self, other: &Self) -> Ordering { self.cost.cmp(&other.cost) } } impl PartialOrd for Edge { fn partial_cmp(&self, other: &Edge) -> Option<Ordering> { Some(self.cost.cmp(&other.cost)) } } struct UnionFindTree { parent_or_size: Vec<isize>, } impl UnionFindTree { fn new(size: usize) -> UnionFindTree { UnionFindTree { parent_or_size: vec![-1; size], } } fn find(&self, index: usize) -> usize { let mut index = index; while self.parent_or_size[index] >= 0 { index = self.parent_or_size[index] as usize; } index } fn same(&self, x: usize, y: usize) -> bool { self.find(x) == self.find(y) } fn unite(&mut self, index0: usize, index1: usize) -> bool { let a = self.find(index0); let b = self.find(index1); if a == b { false } else { if self.parent_or_size[a] < self.parent_or_size[b] { self.parent_or_size[a] += self.parent_or_size[b]; self.parent_or_size[b] = a as isize; } else { self.parent_or_size[b] += self.parent_or_size[a]; self.parent_or_size[a] = b as isize; } true } } } fn kruskal(edges: &Vec<Edge>, num_apexes: usize) -> i64 { let mut edges = edges.clone(); let mut res = 0; edges.sort(); let mut unf = UnionFindTree::new(num_apexes); for e in edges { if !unf.same(e.to, e.from) { unf.unite(e.to, e.from); res += e.cost; } } res }