結果

問題 No.860 買い物
ユーザー fukafukatanifukafukatani
提出日時 2019-08-09 23:45:41
言語 Rust
(1.77.0)
結果
AC  
実行時間 54 ms / 1,000 ms
コード長 3,225 bytes
コンパイル時間 1,875 ms
コンパイル使用メモリ 156,008 KB
実行使用メモリ 16,056 KB
最終ジャッジ日時 2023-09-26 22:11:18
合計ジャッジ時間 3,896 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 3 ms
4,376 KB
testcase_07 AC 54 ms
14,868 KB
testcase_08 AC 53 ms
14,912 KB
testcase_09 AC 53 ms
14,896 KB
testcase_10 AC 53 ms
15,652 KB
testcase_11 AC 42 ms
15,612 KB
testcase_12 AC 42 ms
16,008 KB
testcase_13 AC 50 ms
14,904 KB
testcase_14 AC 54 ms
14,896 KB
testcase_15 AC 38 ms
16,056 KB
testcase_16 AC 52 ms
14,888 KB
testcase_17 AC 33 ms
13,680 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: unused variable: `i`
  --> 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

warning: 1 warning emitted

ソースコード

diff #

#![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
}
0