結果
問題 | No.2387 Yokan Factory |
ユーザー |
|
提出日時 | 2023-07-21 23:14:38 |
言語 | Rust (1.83.0 + proconio) |
結果 |
AC
|
実行時間 | 417 ms / 5,000 ms |
コード長 | 4,748 bytes |
コンパイル時間 | 19,035 ms |
コンパイル使用メモリ | 383,012 KB |
実行使用メモリ | 20,464 KB |
最終ジャッジ日時 | 2024-09-22 00:43:26 |
合計ジャッジ時間 | 23,414 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 35 |
ソースコード
pub mod binary_search {type T = usize;pub fn binary_search(mut ok: T, mut ng: T, f: impl Fn(T) -> bool) -> T {let cont = |ok: T, ng: T| {let l = ok.min(ng);let r = ok.max(ng);l + 1 < r};while cont(ok, ng) {let x = (ok + ng) / 2;if f(x) {ok = x;} else {ng = x;}}ok}}pub mod graph {#[derive(Clone, Copy, Debug)]pub struct Edge {pub from: usize,pub to: usize,pub index: usize,pub weight: isize,pub width: isize,}pub struct Graph {pub n: usize,pub m: usize,pub edges: Vec<Vec<Edge>>,}impl Graph {pub fn new(n: usize, m: usize) -> Graph {let edges = vec![vec![]; n];Graph { n, m, edges }}pub fn add_edge(&mut self,from: usize,to: usize,index: usize,weight: isize,width: isize,) {let edge = Edge {from,to,index,weight,width,};self.edges[from].push(edge);}}}pub mod scanner {pub struct Scanner {buf: Vec<String>,}impl Scanner {pub fn new() -> Self {Self { buf: vec![] }}pub fn new_from(source: &str) -> Self {let source = String::from(source);let buf = Self::split(source);Self { buf }}pub fn next<T: std::str::FromStr>(&mut self) -> T {loop {if let Some(x) = self.buf.pop() {return x.parse().ok().expect("");}let mut source = String::new();std::io::stdin().read_line(&mut source).expect("");self.buf = Self::split(source);}}fn split(source: String) -> Vec<String> {source.split_whitespace().rev().map(String::from).collect::<Vec<_>>()}}}use crate::{binary_search::binary_search,graph::{Edge, Graph},scanner::Scanner,};use std::io::Write;fn main() {let mut scanner = Scanner::new();let out = std::io::stdout();let mut out = std::io::BufWriter::new(out.lock());let t: usize = 1;for _ in 0..t {solve(&mut scanner, &mut out);}}fn shortest_path(graph: &Graph,from: usize,w: isize,) -> (Vec<Option<isize>>, Vec<Option<(usize, Edge)>>) {#[inline]fn add(x: isize, y: isize) -> isize {x + y}#[inline]fn e() -> isize {0}use std::cmp::Reverse;let mut que = std::collections::BinaryHeap::<Reverse<(isize, usize)>>::new();let mut dist = vec![None; graph.n];let mut prev = vec![None; graph.n];que.push(Reverse((e(), from)));dist[from] = Some(e());while let Some(Reverse((d, u))) = que.pop() {if let Some(now_d) = dist[u] {if now_d < d {continue;}}for edge in graph.edges[u].iter() {let &Edge {to: v,weight,width,..} = edge;if width < w {continue;}let next_d = add(d, weight);if let Some(now_d) = dist[v] {if now_d <= next_d {continue;}}que.push(Reverse((next_d, v)));dist[v] = Some(next_d);prev[v] = Some((u, *edge));}}(dist, prev)}fn solve(scanner: &mut Scanner, out: &mut std::io::BufWriter<std::io::StdoutLock>) {let n: usize = scanner.next();let m: usize = scanner.next();let x: usize = scanner.next();let mut graph = Graph::new(n, m);for index in 0..m {let from: usize = scanner.next::<usize>() - 1;let to: usize = scanner.next::<usize>() - 1;let weight: isize = scanner.next();let width: isize = scanner.next();graph.add_edge(from, to, index, weight, width);graph.add_edge(to, from, index, weight, width);}let ans = binary_search(0, 1e9 as usize + 10, |w| {let dist = shortest_path(&graph, 0, w as isize);dist.0[n - 1].is_some() && dist.0[n - 1].unwrap() <= x as isize});let ans = if ans == 0 { -1 as isize } else { ans as isize };writeln!(out, "{}", ans).unwrap();}