CSES - Datatähti 2023 alku - Results
Submission details
Task:Sadonkorjuu
Sender:drvilepis
Submission time:2022-11-01 16:28:28 +0200
Language:Rust
Status:COMPILE ERROR

Compiler report

error[E0308]: mismatched types
  --> input/code.rs:71:29
   |
71 |                 if length > min_dist {
   |                             ^^^^^^^^ expected `usize`, found enum `Option`
   |
   = note: expected type `usize`
              found enum `Option<usize>`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0308`.

Code

use std::collections::HashMap;

fn sort(i: usize, j: usize) -> (usize, usize) {
    if i < j {
        (i, j)
    } else {
        (j, i)
    }
}

fn main() {
    let stdin = std::io::stdin();

    let mut input = String::new();

    stdin.read_line(&mut input).unwrap();
    let n_cities = input.trim().parse::<usize>().unwrap();
    input.clear();

    stdin.read_line(&mut input).unwrap();

    // true on satama, false on pelto
    let types = input.split_whitespace().map(|c| c.chars().next().unwrap() == '0').collect::<Vec<_>>();
    input.clear();

    let mut lenmap = HashMap::new();
    let mut bridgemap: HashMap<usize, Vec<usize>> = HashMap::new();

    loop {
        let res = stdin.read_line(&mut input).unwrap();

        if res == 0 {
            break;
        };

        let mut nums = input.split_whitespace().map(|c| c.parse::<usize>().unwrap());
        let (i, j) = (nums.next().unwrap() - 1, nums.next().unwrap() - 1);

        let (i, j) = sort(i, j);

        lenmap.insert((i, j), nums.next().unwrap());

        if let Some(vec) = bridgemap.get_mut(&i) {
            vec.push(j)
        } else {
            bridgemap.insert(i, vec![j]);
        }

        if let Some(vec) = bridgemap.get_mut(&j) {
            vec.push(i)
        } else {
            bridgemap.insert(j, vec![i]);
        }

        input.clear();
    }

    let sum = types.iter().enumerate().filter_map(|(i, &t)| {
        if t {
            return None;
        }

        let mut min_dist: Option<usize> = None;

        let mut nums: Vec<(usize, Option<usize>, usize)> = vec![];

        nums.push((i, None, 0));

        loop {
            nums = nums.iter().map(|&(cur_node, last_node_opt, length)| {
                if length > min_dist {
                    return vec![];
                }
                let other_nodes = bridgemap.get(&cur_node).unwrap().clone();

                let mut out = vec![];

                for other_node in other_nodes {
                    if let Some(last_node) = last_node_opt {
                        if other_node == last_node {
                            continue;
                        }
                    };

                    let new_length = length + *lenmap.get(&sort(other_node, cur_node)).unwrap();

                    if let Some(dist) = min_dist {
                        if new_length >= dist {
                            continue
                        };
                    };

                    if !types[other_node] {
                        out.push((other_node, Some(cur_node), new_length));
                    } else {
                        if let Some(dist) = min_dist {
                            if new_length < dist {
                                min_dist = Some(new_length);
                            }
                        } else {
                            min_dist = Some(new_length);
                        }
                    }
                }

                out
            }).flatten().collect();

            if nums.is_empty() {
                break;
            }
        }

        min_dist
    }).sum::<usize>();

    println!("{}", sum);
}