結果

問題 No.872 All Tree Path
ユーザー keikei
提出日時 2019-08-30 22:46:12
言語 Rust
(1.77.0 + proconio)
結果
AC  
実行時間 160 ms / 3,000 ms
コード長 5,239 bytes
コンパイル時間 18,009 ms
コンパイル使用メモリ 389,916 KB
実行使用メモリ 45,028 KB
最終ジャッジ日時 2024-11-22 01:42:16
合計ジャッジ時間 21,521 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 156 ms
33,664 KB
testcase_01 AC 157 ms
33,656 KB
testcase_02 AC 160 ms
33,732 KB
testcase_03 AC 84 ms
45,028 KB
testcase_04 AC 1 ms
6,816 KB
testcase_05 AC 160 ms
33,636 KB
testcase_06 AC 159 ms
33,676 KB
testcase_07 AC 153 ms
33,660 KB
testcase_08 AC 12 ms
6,820 KB
testcase_09 AC 11 ms
6,820 KB
testcase_10 AC 11 ms
6,816 KB
testcase_11 AC 11 ms
6,816 KB
testcase_12 AC 11 ms
6,820 KB
testcase_13 AC 1 ms
6,820 KB
testcase_14 AC 1 ms
6,816 KB
testcase_15 AC 1 ms
6,816 KB
testcase_16 AC 1 ms
6,820 KB
testcase_17 AC 1 ms
6,820 KB
testcase_18 AC 1 ms
6,816 KB
testcase_19 AC 1 ms
6,816 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#![allow(unused_mut, non_snake_case, unused_imports, dead_code,unused_macros)]

use std::iter;
use std::cmp::{max, min, Ordering};
use std::mem::swap;
use std::collections::{HashMap, BTreeMap, HashSet, BTreeSet, BinaryHeap,VecDeque};
use std::iter::FromIterator;

// 高速 EOF要
//macro_rules! input {(source = $s:expr, $($r:tt)*) => {let mut iter = $s.split_whitespace();input_inner!{iter, $($r)*}};($($r:tt)*) => {let mut s = {use std::io::Read;let mut s = String::new();std::io::stdin().read_to_string(&mut s).unwrap();s};let mut iter = s.split_whitespace();input_inner!{iter, $($r)*}};}
//macro_rules! input_inner {($iter:expr) => {};($iter:expr, ) => {};($iter:expr, $var:ident : $t:tt $($r:tt)*) => {let $var = read_value!($iter, $t);input_inner!{$iter $($r)*}};}
//macro_rules! read_value {($iter:expr, ( $($t:tt),* )) => {( $(read_value!($iter, $t)),* )};($iter:expr, [ $t:tt ; $len:expr ]) => {(0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()};($iter:expr, chars) => {read_value!($iter, String).chars().collect::<Vec<char>>()};($iter:expr, usize1) => {read_value!($iter, usize) - 1};($iter:expr, $t:ty) => {$iter.next().unwrap().parse::<$t>().expect("Parse error")};}
// 低速 EOF不要
macro_rules! input {(source = $s:expr, $($r:tt)*) => {let mut iter = $s.split_whitespace();let mut next = || { iter.next().unwrap() };input_inner!{next, $($r)*}};($($r:tt)*) => {let stdin = std::io::stdin();let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));let mut next = move || -> String{bytes.by_ref().map(|r|r.unwrap() as char).skip_while(|c|c.is_whitespace()).take_while(|c|!c.is_whitespace()).collect()};input_inner!{next, $($r)*}};}
macro_rules! mut_input {(source = $s:expr, $($r:tt)*) => {let mut iter = $s.split_whitespace();let mut next = || { iter.next().unwrap() }; mut_input_inner!{next, $($r)*}};($($r:tt)*) => {let stdin = std::io::stdin();let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));let mut next = move || -> String{bytes.by_ref().map(|r|r.unwrap() as char).skip_while(|c|c.is_whitespace()).take_while(|c|!c.is_whitespace()).collect()}; mut_input_inner!{next, $($r)*}};}
macro_rules! input_inner {($next:expr) => {};($next:expr, ) => {};($next:expr, $var:ident : $t:tt $($r:tt)*) => {let $var = read_value!($next, $t);input_inner!{$next $($r)*}};}
macro_rules! mut_input_inner {($next:expr) => {};($next:expr, ) => {};($next:expr, $var:ident : $t:tt $($r:tt)*) => {let mut $var = read_value!($next, $t);mut_input_inner!{$next $($r)*}};}
macro_rules! read_value {($next:expr, ( $($t:tt),* )) => {( $(read_value!($next, $t)),* )};($next:expr, [ $t:tt ; $len:expr ]) => {(0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()};($next:expr, chars) => {read_value!($next, String).chars().collect::<Vec<char>>()};($next:expr, usize1) => {read_value!($next, usize) - 1};($next:expr, $t:ty) => {$next().parse::<$t>().expect("Parse error")};}

// 非常時
fn read_line() -> String {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).unwrap();
    s.trim().to_string()
}
fn read_vec<T: std::str::FromStr>() -> Vec<T> {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    s.trim().split_whitespace()
        .map(|e| e.parse().ok().unwrap()).collect()
}

#[derive(Copy, Clone)]
pub struct Edge{
    from:usize,
    to:usize,
    cost:i64
}

#[derive(Clone)]
pub struct Graph{
    N:usize,
    Edges:Vec<Vec<Edge>>
}
impl Graph{
    pub fn new(n:usize) -> Graph{
        Graph{N:n,Edges:vec![vec![];n]}
    }
    pub fn add_edge(&mut self,u:usize,v:usize,cost:i64,undirected:bool){
        self.Edges[u].push(Edge{from:u,to:v,cost});
        if undirected {
            self.Edges[v].push(Edge{from:v,to:u,cost})
        }
    }

    pub fn Dijkstra(&self,root:usize) -> Vec<i64>{
        let mut dist = vec![i64::max_value() as i64;self.N];
        dist[root] = 0;

        let mut pq = BinaryHeap::new();
        pq.push((0_i64,root));
        while !pq.is_empty(){
            let s = pq.pop().unwrap();
            let (d,n) = (-s.0,s.1);
            if dist[n] < d {
                continue;
            }

            for e in &self.Edges[n]{
                if dist[e.to] > dist[e.from] + e.cost{
                    dist[e.to] = dist[e.from] + e.cost;
                    pq.push((-dist[e.to],e.to));
                }
            }
        }

        return dist;
    }
}


pub fn rec(n:usize,pre:usize,G:&Vec<Vec<Edge>>,ans:&mut i64) -> usize{
    let mut ret = 1;
    let mut W = 0;
    for e in &G[n]{
        if e.to == pre{
            W = e.cost;
            continue;
        }
        ret += rec(e.to,n,G,ans);
    }

    *ans += 2*W*(ret as i64)*(G.len() as i64 - ret as i64);

    return ret;
}

fn solve(){
    input!(N:usize,info:[(usize1,usize1,i64);N-1]);
    let N:usize = N;
    let info:Vec<(usize,usize,i64)> = info;

    let mut G = Graph::new(N);
    for e in info{
        G.add_edge(e.0,e.1,e.2,true);
    }

    let mut ans = 0;
    rec(0,1e9 as usize,&G.Edges,&mut ans);
    println!("{}",ans);
}
fn main() {
    let stack_size = 104_857_600; // 100 MB
    let thd = std::thread::Builder::new().stack_size(stack_size);
    thd.spawn(|| solve()).unwrap().join().unwrap();
}
0