Submission #2376507


Source Code Expand

use std::collections::HashMap;
use std::collections::VecDeque;

fn main() {
    let mut sc = Scanner::new();
    let n: usize = sc.read();
    let m: usize = sc.read();

    let mut edges = Vec::new();
    for _ in 0..m {
        let p = sc.read::<usize>() - 1;
        let q = sc.read::<usize>() - 1;
        let c: u32 = sc.read();
        edges.push((p, q, c));
    }

    let mut edge_map = HashMap::new();
    for &(p, q, company) in &edges {
        if !edge_map.contains_key(&(p, company)) {
            let id = edge_map.len();
            edge_map.insert((p, company), id);
        }
        if !edge_map.contains_key(&(q, company)) {
            let id = edge_map.len();
            edge_map.insert((q, company), id);
        }
    }
    let mut graph: Vec<Vec<(usize, u32, usize)>> = vec![vec![]; n];
    for &(p, q, company) in &edges {
        let q_id = edge_map.get(&(q, company)).unwrap();
        graph[p].push((q, company, *q_id));
        let p_id = edge_map.get(&(p, company)).unwrap();
        graph[q].push((p, company, *p_id));
    }

    let mut dist = vec![std::usize::MAX; edge_map.len()];
    let mut visit = vec![false; edge_map.len()];
    let mut q = VecDeque::new();
    for &(to, company, to_id) in &graph[0] {
        q.push_back((to, 1, company, to_id));
        dist[to_id] = 1;
        let inverse_id = edge_map.get(&(0, company)).unwrap();
        dist[*inverse_id] = 0;
    }

    while let Some((v, v_cost, company, v_id)) = q.pop_front() {
        if v == n - 1 {
            println!("{}", v_cost);
            return;
        }
        if visit[v_id] {
            continue;
        }
        visit[v_id] = true;

        for &(to, next_company, to_id) in &graph[v] {
            let next_cost = if company == next_company {
                v_cost
            } else {
                v_cost + 1
            };

            if dist[to_id] <= next_cost {
                continue;
            }
            dist[to_id] = next_cost;

            if next_cost > v_cost {
                q.push_back((to, next_cost, next_company, to_id));
            } else {
                q.push_front((to, next_cost, next_company, to_id));
            }
        }
    }

    println!("-1");
}

struct Scanner {
    ptr: usize,
    length: usize,
    buf: Vec<u8>,
    small_cache: Vec<u8>,
}

impl Scanner {
    fn new() -> Scanner {
        Scanner {
            ptr: 0,
            length: 0,
            buf: vec![0; 1024],
            small_cache: vec![0; 1024],
        }
    }

    fn load(&mut self) {
        use std::io::Read;
        let mut s = std::io::stdin();
        self.length = s.read(&mut self.buf).unwrap();
    }

    fn byte(&mut self) -> u8 {
        if self.ptr >= self.length {
            self.ptr = 0;
            self.load();
            if self.length == 0 {
                self.buf[0] = b'\n';
                self.length = 1;
            }
        }

        self.ptr += 1;
        return self.buf[self.ptr - 1];
    }

    fn is_space(b: u8) -> bool {
        b == b'\n' || b == b'\r' || b == b'\t' || b == b' '
    }

    fn read<T>(&mut self) -> T
    where
        T: std::str::FromStr,
        T::Err: std::fmt::Debug,
    {
        let mut b = self.byte();
        while Scanner::is_space(b) {
            b = self.byte();
        }

        for pos in 0..self.small_cache.len() {
            self.small_cache[pos] = b;
            b = self.byte();
            if Scanner::is_space(b) {
                return String::from_utf8_lossy(&self.small_cache[0..(pos + 1)])
                    .parse()
                    .unwrap();
            }
        }

        let mut v = self.small_cache.clone();
        while !Scanner::is_space(b) {
            v.push(b);
            b = self.byte();
        }
        return String::from_utf8_lossy(&v).parse().unwrap();
    }
}

Submission Info

Submission Time
Task E - Snuke's Subway Trip
User kenkoooo
Language Rust (1.15.1)
Score 0
Code Size 3978 Byte
Status TLE
Exec Time 3156 ms
Memory 60548 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 600
Status AC
AC × 57
TLE × 2
Set Name Test Cases
Sample
All 01.txt, 02.txt, 03.txt, 04.txt, 05.txt, 06.txt, 07.txt, 08.txt, 09.txt, 10.txt, 11.txt, 12.txt, 13.txt, 14.txt, 15.txt, 16.txt, 17.txt, 18.txt, 19.txt, 20.txt, 21.txt, 22.txt, 23.txt, 24.txt, 25.txt, 26.txt, 27.txt, 28.txt, 29.txt, 30.txt, 31.txt, 32.txt, 33.txt, 34.txt, 35.txt, 36.txt, 37.txt, 38.txt, sample_01.txt, sample_02.txt, sample_03.txt, w1.txt, w10.txt, w11.txt, w12.txt, w13.txt, w14.txt, w15.txt, w16.txt, w17.txt, w18.txt, w2.txt, w3.txt, w4.txt, w5.txt, w6.txt, w7.txt, w8.txt, w9.txt
Case Name Status Exec Time Memory
01.txt AC 2 ms 4352 KB
02.txt AC 95 ms 32928 KB
03.txt AC 184 ms 57504 KB
04.txt AC 158 ms 49312 KB
05.txt AC 152 ms 57504 KB
06.txt AC 70 ms 30884 KB
07.txt AC 76 ms 32932 KB
08.txt AC 165 ms 57504 KB
09.txt AC 82 ms 32932 KB
10.txt AC 88 ms 32932 KB
11.txt AC 107 ms 51232 KB
12.txt AC 63 ms 30836 KB
13.txt AC 92 ms 36972 KB
14.txt AC 107 ms 45216 KB
15.txt AC 91 ms 42860 KB
16.txt AC 103 ms 49056 KB
17.txt AC 106 ms 53408 KB
18.txt AC 72 ms 30964 KB
19.txt AC 98 ms 52180 KB
20.txt AC 106 ms 51108 KB
21.txt AC 146 ms 53152 KB
22.txt AC 135 ms 54208 KB
23.txt AC 144 ms 60548 KB
24.txt AC 105 ms 43040 KB
25.txt AC 58 ms 30836 KB
26.txt AC 79 ms 32876 KB
27.txt AC 99 ms 39072 KB
28.txt AC 101 ms 47008 KB
29.txt AC 104 ms 47008 KB
30.txt AC 95 ms 45216 KB
31.txt AC 85 ms 40812 KB
32.txt AC 107 ms 52192 KB
33.txt AC 104 ms 42912 KB
34.txt AC 108 ms 40940 KB
35.txt AC 60 ms 28916 KB
36.txt AC 88 ms 30836 KB
37.txt AC 105 ms 39068 KB
38.txt AC 100 ms 42860 KB
sample_01.txt AC 2 ms 4352 KB
sample_02.txt AC 2 ms 4352 KB
sample_03.txt AC 2 ms 4352 KB
w1.txt AC 110 ms 45676 KB
w10.txt AC 165 ms 51360 KB
w11.txt AC 61 ms 31220 KB
w12.txt AC 76 ms 28268 KB
w13.txt AC 149 ms 32928 KB
w14.txt AC 162 ms 51360 KB
w15.txt AC 62 ms 31304 KB
w16.txt AC 66 ms 26536 KB
w17.txt AC 74 ms 24820 KB
w18.txt AC 105 ms 34972 KB
w2.txt AC 112 ms 46308 KB
w3.txt AC 133 ms 47264 KB
w4.txt AC 105 ms 39148 KB
w5.txt TLE 3156 ms 59552 KB
w6.txt AC 140 ms 59680 KB
w7.txt TLE 3156 ms 55328 KB
w8.txt AC 1874 ms 47136 KB
w9.txt AC 244 ms 45216 KB