AtCoder Beginner Contest 340 D

https://atcoder.jp/contests/abc340/tasks/abc340_d

単なるダイクストラ法です。
隣が2つと決まっているので、グラフをVecにしやすいです。

// Super Takahashi Bros.
#![allow(non_snake_case)]


//////////////////// library ////////////////////

fn read<T: std::str::FromStr>() -> T {
    let mut line = String::new();
    std::io::stdin().read_line(&mut line).ok();
    line.trim().parse().ok().unwrap()
}

fn read_vec<T: std::str::FromStr>() -> Vec<T> {
    read::<String>().split_whitespace()
            .map(|e| e.parse().ok().unwrap()).collect()
}


//////////////////// Graph ////////////////////

struct Graph {
    vs: Vec<usize>
}

impl Graph {
    fn len(&self) -> usize {
        self.vs.len() / 4
    }
    
    fn is_end(&self, v: usize) -> bool {
        self.vs[v*4-4] == 0
    }
    
    fn neighbors(&self, v: usize) -> Vec<(usize, usize)> {
        let mut neis: Vec<(usize, usize)> = vec![];
        if !self.is_end(v) {
            neis.push((self.vs[v*4-4], self.vs[v*4-3]));
            neis.push((self.vs[v*4-2], self.vs[v*4-1]))
        }
        neis
    }
    
    fn create(edges: Vec<(usize, usize, usize)>) -> Graph {
        let N = edges.len() + 1;
        let mut vs: Vec<usize> = vec![0; N*4];
        for i in 0..N-1 {
            vs[i*4] = i+2;
            let (A, B, X) = edges[i];
            vs[i*4+1] = A;
            vs[i*4+2] = X;
            vs[i*4+3] = B
        }
        Graph { vs }
    }
}


//////////////////// Dijkstra ////////////////////

use std::collections::BinaryHeap;

#[derive(Debug, Eq, PartialEq)]
struct Item {
    dist: usize,
    node: usize,
}

impl Ord for Item {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        other.dist.cmp(&self.dist)
    }
}

impl PartialOrd for Item {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        Some(self.cmp(other))
    }
}

fn Dijkstra(graph: Graph, v0: usize, v1: usize) -> usize {
    let mut heap = BinaryHeap::new();
    heap.push(Item { dist: 0, node: v0 });
    let N = graph.len();
    let mut visited: Vec<bool> = vec![false; N];
    while let Some(v) = heap.pop() {
        if visited[v.node-1] {
            continue
        }
        else {
            visited[v.node-1] = true
        }
        
        if v.node == v1 {
            return v.dist
        }
        
        let neis = graph.neighbors(v.node);
        for (v2, d2) in neis.into_iter() {
            if !visited[v2-1] {
                heap.push(Item { dist: v.dist+d2, node: v2 })
            }
        }
    }
    0
}


//////////////////// process ////////////////////

fn read_input() -> Vec<(usize, usize, usize)> {
    let N: usize = read();
    let edges: Vec<(usize, usize, usize)> = (0..N-1).map(|_| read_vec()).
                                            map(|v| (v[0], v[1], v[2])).
                                            collect();
    edges
}

fn F(edges: Vec<(usize, usize, usize)>) -> usize {
    let graph = Graph::create(edges);
    let N = graph.len();
    Dijkstra(graph, 1, N)
}

fn main() {
    let edges = read_input();
    println!("{}", F(edges))
}