CSES - Datatähti 2020 alku - Results
Submission details
Task:Mastot
Sender:aleksih
Submission time:2019-10-10 15:58:39 +0300
Language:Rust
Status:COMPILE ERROR

Compiler report

error: attempt to shift left with overflow
  --> input/code.rs:23:39
   |
23 |     dijkstra_hinnat.resize(valit_lkm, (1 << 63) as usize);
   |                                       ^^^^^^^^^
   |
   = note: #[deny(exceeding_bitshifts)] on by default

error: attempt to shift left with overflow
  --> input/code.rs:35:36
   |
35 |         let mut lahin_solu_hinta = (1 << 63) as usize;
   |                                    ^^^^^^^^^

error: aborting due to 2 previous errors

Code

fn main() {
    let mut valit_lkm_str = String::new();

    std::io::stdin().read_line(&mut valit_lkm_str).unwrap();
    let valit_lkm = valit_lkm_str.trim().parse::<usize>().unwrap();

    let mut kantamat = Vec::with_capacity(valit_lkm);
    let mut kantamat_str = String::new();
    std::io::stdin().read_line(&mut kantamat_str).unwrap();
    for luku in kantamat_str.split_ascii_whitespace() {
        kantamat.push(luku.trim().parse::<usize>().unwrap());
    }

    let mut hinnat = Vec::with_capacity(valit_lkm);
    let mut hinnat_str = String::new();
    std::io::stdin().read_line(&mut hinnat_str).unwrap();
    hinnat.push(0);
    for luku in hinnat_str.split_ascii_whitespace() {
        hinnat.push(luku.trim().parse::<usize>().unwrap());
    }

    let mut dijkstra_hinnat = Vec::new();
    dijkstra_hinnat.resize(valit_lkm, (1 << 63) as usize);
    dijkstra_hinnat[0] = 0;
    let mut edellinen = Vec::new();
    edellinen.resize(valit_lkm, None as Option<usize>);

    let mut solut = Vec::with_capacity(valit_lkm);
    for i in 0..valit_lkm {
        solut.push(i);
    }

    while !solut.is_empty() {
        let mut lahin_solu = 0;
        let mut lahin_solu_hinta = (1 << 63) as usize;
        for solu in solut.iter() {
            if dijkstra_hinnat[*solu] < lahin_solu_hinta {
                lahin_solu_hinta = dijkstra_hinnat[*solu];
                lahin_solu = *solu;
            }
        }

        if lahin_solu + kantamat[lahin_solu] >= valit_lkm {
            break;
        }
        solut.remove(solut.iter().position(|&el| el == lahin_solu).unwrap());

        for naapuri in (lahin_solu + 1)..std::cmp::min(hinnat.len(), lahin_solu + kantamat[lahin_solu] + 1) {
            let alt = dijkstra_hinnat[lahin_solu] + hinnat[lahin_solu];
            if alt < dijkstra_hinnat[naapuri] {
                dijkstra_hinnat[naapuri] = alt;
                edellinen[naapuri] = Some(lahin_solu);
            }
        }
    }

    let viimeinen_iter = edellinen
        .iter().rev();
    let mut viimeinen_pos = edellinen.len();
    let mut viimeinen = None;
    for viim in viimeinen_iter {
        viimeinen_pos -= 1;
        if viim.is_some() {
            viimeinen = viim.as_ref();
            break;
        }
    }

    let mut summa = hinnat[viimeinen_pos];
    loop {
        match viimeinen {
            Some(viim) => {
                summa += hinnat[*viim];
                viimeinen = edellinen[*viim].as_ref();
            },
            None => break
        }
    }

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