Submission #2376354
Source Code Expand
use std::collections::BTreeMap; use std::collections::BTreeSet; 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 graph = vec![vec![]; n]; let mut dist = HashMap::new(); for _ in 0..m { let p = sc.read::<usize>() - 1; let q = sc.read::<usize>() - 1; let c: u32 = sc.read(); graph[p].push((q, c)); graph[q].push((p, c)); dist.insert((p, c), std::usize::MAX); dist.insert((q, c), std::usize::MAX); } let mut q = VecDeque::new(); for &(to, company) in &graph[0] { q.push_back((to, 1, company)); dist.insert((to, company), 1); dist.insert((0, company), 0); } let mut visit = BTreeSet::new(); while !q.is_empty() { let (v, v_cost, company) = q.pop_front().unwrap(); if v == n - 1 { println!("{}", v_cost); return; } if visit.contains(&(v, company)) { continue; } visit.insert((v, company)); for &(to, next_company) in &graph[v] { let next_cost = if company == next_company { v_cost } else { v_cost + 1 }; let cost = *dist.get(&(to, next_company)).unwrap(); if cost > next_cost { assert_eq!(next_cost, v_cost); dist.insert((to, next_company), next_cost); q.push_front((to, next_cost, next_company)); } } } 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 | 3366 Byte |
Status | RE |
Exec Time | 130 ms |
Memory | 41176 KB |
Compile Error
warning: unused import: `std::collections::BTreeMap`, #[warn(unused_imports)] on by default --> ./Main.rs:1:5 | 1 | use std::collections::BTreeMap; | ^^^^^^^^^^^^^^^^^^^^^^^^^^
Judge Result
Set Name | Sample | All | ||||
---|---|---|---|---|---|---|
Score / Max Score | 0 / 0 | 0 / 600 | ||||
Status | AC |
|
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 | RE | 2 ms | 4352 KB |
02.txt | RE | 72 ms | 16576 KB |
03.txt | RE | 121 ms | 39160 KB |
04.txt | AC | 130 ms | 39160 KB |
05.txt | RE | 108 ms | 41136 KB |
06.txt | RE | 53 ms | 24804 KB |
07.txt | RE | 55 ms | 24820 KB |
08.txt | RE | 127 ms | 39140 KB |
09.txt | RE | 65 ms | 22756 KB |
10.txt | RE | 62 ms | 22756 KB |
11.txt | RE | 77 ms | 28920 KB |
12.txt | AC | 48 ms | 16636 KB |
13.txt | RE | 66 ms | 20736 KB |
14.txt | RE | 84 ms | 26876 KB |
15.txt | AC | 119 ms | 26848 KB |
16.txt | RE | 77 ms | 30984 KB |
17.txt | RE | 78 ms | 30520 KB |
18.txt | RE | 55 ms | 16636 KB |
19.txt | RE | 84 ms | 30684 KB |
20.txt | RE | 81 ms | 28856 KB |
21.txt | RE | 126 ms | 40972 KB |
22.txt | RE | 86 ms | 30588 KB |
23.txt | RE | 95 ms | 41176 KB |
24.txt | RE | 73 ms | 26880 KB |
25.txt | RE | 46 ms | 18684 KB |
26.txt | AC | 66 ms | 20704 KB |
27.txt | RE | 75 ms | 26876 KB |
28.txt | RE | 73 ms | 28920 KB |
29.txt | RE | 73 ms | 26808 KB |
30.txt | RE | 86 ms | 30436 KB |
31.txt | RE | 65 ms | 20700 KB |
32.txt | RE | 81 ms | 30896 KB |
33.txt | RE | 81 ms | 26808 KB |
34.txt | RE | 68 ms | 22780 KB |
35.txt | AC | 56 ms | 16636 KB |
36.txt | RE | 61 ms | 16636 KB |
37.txt | RE | 67 ms | 20732 KB |
38.txt | RE | 74 ms | 24768 KB |
sample_01.txt | AC | 2 ms | 4352 KB |
sample_02.txt | RE | 2 ms | 4352 KB |
sample_03.txt | AC | 2 ms | 4352 KB |
w1.txt | RE | 81 ms | 33004 KB |
w10.txt | RE | 118 ms | 35032 KB |
w11.txt | RE | 44 ms | 18752 KB |
w12.txt | RE | 53 ms | 16636 KB |
w13.txt | RE | 76 ms | 20724 KB |
w14.txt | RE | 110 ms | 35040 KB |
w15.txt | RE | 43 ms | 18200 KB |
w16.txt | RE | 50 ms | 14588 KB |
w17.txt | RE | 57 ms | 12540 KB |
w18.txt | RE | 84 ms | 20732 KB |
w2.txt | RE | 78 ms | 31892 KB |
w3.txt | RE | 91 ms | 26844 KB |
w4.txt | AC | 90 ms | 24824 KB |
w5.txt | RE | 127 ms | 39156 KB |
w6.txt | RE | 107 ms | 41164 KB |
w7.txt | RE | 89 ms | 36648 KB |
w8.txt | RE | 101 ms | 38404 KB |
w9.txt | RE | 102 ms | 33012 KB |