結果

問題 No.2387 Yokan Factory
ユーザー 👑 MizarMizar
提出日時 2023-07-01 17:28:50
言語 Rust
(1.83.0 + proconio)
結果
WA  
実行時間 -
コード長 2,374 bytes
コンパイル時間 10,895 ms
コンパイル使用メモリ 398,368 KB
実行使用メモリ 9,076 KB
最終ジャッジ日時 2024-07-08 03:30:50
合計ジャッジ時間 13,503 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2 WA * 1
other AC * 14 WA * 21
権限があれば一括ダウンロードができます

ソースコード

diff #

// -*- coding:utf-8-unix -*-
use std::{cmp::Reverse, collections::BinaryHeap, io::prelude::*};

fn main() {
    let stdin = std::io::stdin();
    let mut lines = stdin.lock().lines();

    let (n, m, x) = {
        let l = lines.next().unwrap().unwrap();
        let mut t = l.split_ascii_whitespace();
        let n = t.next().unwrap().parse::<usize>().unwrap();
        let m = t.next().unwrap().parse::<usize>().unwrap();
        let x = t.next().unwrap().parse::<u64>().unwrap();
        (n, m, x)
    };
    let n1 = n - 1;
    let mut g = (0..n)
        .map(|_| Vec::<(u32, u32, u32)>::new())
        .collect::<Vec<_>>();
    let mut yokan = (0u32, 1000000000u32);
    let mut queue = BinaryHeap::with_capacity(n + m);

    for _ in 0..m {
        let l = lines.next().unwrap().unwrap();
        let mut t = l.split_ascii_whitespace();
        let u = t.next().unwrap().parse::<u32>().unwrap() - 1;
        let v = t.next().unwrap().parse::<u32>().unwrap() - 1;
        let a = t.next().unwrap().parse::<u32>().unwrap();
        let b = t.next().unwrap().parse::<u32>().unwrap();

        g[u as usize].push((v, a, b));
        g[v as usize].push((u, a, b));
    }
    for p in g.iter_mut() {
        p.sort_by_key(|e| e.2);
    }

    while yokan.0 < yokan.1 {
        let pivot = (yokan.1 - yokan.0 + 1) / 2 + yokan.0;
        let mut dists = vec![1u64 << 60; n];
        dists[0] = 0;
        queue.clear();
        queue.push(Reverse((0u64, 0usize)));
        while let Some(Reverse((pd, i))) = queue.pop() {
            if dists[i] < pd {
                continue;
            }
            for &(j, a, b) in g[i].iter() {
                let ju = j as usize;
                let nd = pd + (a as u64);
                if b < pivot {
                    break;
                }
                if nd > x || dists[ju] <= nd {
                    continue;
                }
                dists[ju] = nd;
                if ju == n1 {
                    queue.clear();
                    break;
                }
                queue.push(Reverse((nd, ju)));
            }
        }
        if dists[n1] <= x {
            yokan.0 = pivot;
        } else {
            yokan.1 = pivot - 1;
        }
    }

    println!(
        "{}",
        if yokan.0 == 0 {
            "-1".to_string()
        } else {
            yokan.0.to_string()
        }
    );
}
0