Created
April 16, 2024 13:26
-
-
Save BlueZeeKing/e6a010e11f28537c4bc7c6906264cd9b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| use std::io::stdin; | |
| fn main() { | |
| let mut lines = stdin().lines().filter_map(|val| val.ok()); | |
| let count: usize = lines.next().unwrap().parse().unwrap(); | |
| for _ in 0..count { | |
| let first = lines.next().unwrap(); | |
| let mut parts = first.split(' '); | |
| let num_rooms: u8 = parts.next().unwrap().parse().unwrap(); | |
| let num_minutes: u8 = parts.next().unwrap().parse().unwrap(); | |
| let rooms = (0..num_rooms) | |
| .map(|_| { | |
| let line = lines.next().unwrap(); | |
| let mut parts = line.split(' '); | |
| ( | |
| parts.next().unwrap().parse::<f64>().unwrap(), | |
| parts | |
| .map(|val| val.parse::<usize>().unwrap()) | |
| .collect::<Vec<_>>(), | |
| ) | |
| }) | |
| .collect::<Vec<_>>(); | |
| println!("{:.3}", calculate_ratio(&rooms, num_minutes as u32, 1)); | |
| } | |
| } | |
| fn calculate_ratio(rooms: &[(f64, Vec<usize>)], num_mins: u32, current_room: usize) -> f64 { | |
| let (chances, connections) = &rooms[current_room - 1]; | |
| if num_mins == 1 { | |
| *chances | |
| } else { | |
| let (total, count) = connections | |
| .iter() | |
| .map(|room| calculate_ratio(rooms, num_mins - 1, *room)) | |
| .fold((0.0, 0), |(total, count), value| (total + value, count + 1)); | |
| (total / count as f64) * chances | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment