|
| 1 | +#![allow(dead_code)] |
| 2 | + |
| 3 | +/* |
| 4 | + * @lc app=leetcode id=2642 lang=rust |
| 5 | + * |
| 6 | + * [2642] Design Graph With Shortest Path Calculator |
| 7 | + */ |
| 8 | + |
| 9 | +// @lc code=start |
| 10 | + |
| 11 | +use std::{collections::BinaryHeap, fmt::Display}; |
| 12 | + |
| 13 | +struct GraphNode { |
| 14 | + id: usize, |
| 15 | + cost: usize, |
| 16 | +} |
| 17 | + |
| 18 | +impl From<&Vec<i32>> for GraphNode { |
| 19 | + fn from(edge: &Vec<i32>) -> Self { |
| 20 | + Self { |
| 21 | + id: check_positive(edge[1]).unwrap(), |
| 22 | + cost: check_positive(edge[2]).unwrap() |
| 23 | + } |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +#[derive(Copy, Clone, Eq, PartialEq)] |
| 28 | +struct State { |
| 29 | + cost: usize, |
| 30 | + position: usize, |
| 31 | +} |
| 32 | + |
| 33 | +impl Ord for State { |
| 34 | + fn cmp(&self, other: &Self) -> std::cmp::Ordering { |
| 35 | + other |
| 36 | + .cost |
| 37 | + .cmp(&self.cost) |
| 38 | + .then_with(|| self.position.cmp(&other.position)) |
| 39 | + } |
| 40 | +} |
| 41 | +impl PartialOrd for State { |
| 42 | + fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> { |
| 43 | + Some(self.cmp(other)) |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +struct Graph { |
| 48 | + graph: Vec<Vec<GraphNode>>, |
| 49 | +} |
| 50 | +#[derive(Debug, Clone)] |
| 51 | +struct NegativeError; |
| 52 | + |
| 53 | +impl Display for NegativeError { |
| 54 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 55 | + write!(f, "input value was negative") |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +fn check_positive(a: i32) -> Result<usize, NegativeError> { |
| 60 | + if a < 0 { |
| 61 | + return Err(NegativeError) |
| 62 | + } else { |
| 63 | + Ok(a as usize) |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +impl Graph { |
| 68 | + fn new(n: i32, edges: Vec<Vec<i32>>) -> Self { |
| 69 | + let n = check_positive(n).unwrap(); |
| 70 | + // Vec with n items, graph[node_int] = [Edge neighbors {node: other_int, cost: #}] |
| 71 | + let mut graph: Vec<Vec<GraphNode>> = Vec::with_capacity(n as usize); |
| 72 | + for _ in 0..n { |
| 73 | + graph.push(Vec::new()); |
| 74 | + } |
| 75 | + for edge in edges.iter() { |
| 76 | + match edge.len() { |
| 77 | + 3 => { |
| 78 | + graph[edge[0] as usize].push(GraphNode::from(edge)); |
| 79 | + } |
| 80 | + _ => panic!( |
| 81 | + "Invalid input format, edge must have format [to, from, cost] {:?}", |
| 82 | + edge |
| 83 | + ), |
| 84 | + } |
| 85 | + } |
| 86 | + Self { graph } |
| 87 | + } |
| 88 | + |
| 89 | + fn add_edge(&mut self, edge: Vec<i32>) { |
| 90 | + // Create new Edge, addd to graph[edge[0]] |
| 91 | + match edge.len() { |
| 92 | + 3 => { |
| 93 | + let node = GraphNode::from(&edge); |
| 94 | + self.graph[edge[0] as usize].push(node); |
| 95 | + } |
| 96 | + _ => panic!( |
| 97 | + "Invalid input format, edge must have format [to, from, cost] {:?}", |
| 98 | + edge |
| 99 | + ), |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + fn shortest_path(&mut self, node1: i32, node2: i32) -> i32 { |
| 104 | + let start = check_positive(node1).unwrap(); |
| 105 | + let end = check_positive(node2).unwrap(); |
| 106 | + let mut dist: Vec<usize> = vec![usize::MAX; self.graph.len()]; |
| 107 | + let mut heap = BinaryHeap::new(); |
| 108 | + |
| 109 | + dist[start] = 0; |
| 110 | + heap.push(State { |
| 111 | + cost: 0, |
| 112 | + position: start |
| 113 | + }); |
| 114 | + while let Some(State { cost, position }) = heap.pop() { |
| 115 | + if position == end { |
| 116 | + return cost as i32; |
| 117 | + } |
| 118 | + if cost > dist[position] { |
| 119 | + continue; |
| 120 | + } |
| 121 | + for edge in &self.graph[position] { |
| 122 | + let next = State {cost: cost + edge.cost, position: edge.id as usize}; |
| 123 | + if next.cost < dist[next.position] { |
| 124 | + heap.push(next); |
| 125 | + dist[next.position] = next.cost; |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + -1 |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +// @lc code=end |
| 134 | +fn main() { |
| 135 | + unimplemented!(); |
| 136 | +} |
0 commit comments