結果

問題 No.1607 Kth Maximum Card
ユーザー phsplsphspls
提出日時 2022-11-23 19:39:33
言語 Rust
(1.77.0)
結果
TLE  
実行時間 -
コード長 3,119 bytes
コンパイル時間 1,181 ms
コンパイル使用メモリ 184,692 KB
実行使用メモリ 53,588 KB
最終ジャッジ日時 2023-10-25 04:47:40
合計ジャッジ時間 7,596 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 0 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 1 ms
4,348 KB
testcase_04 AC 1 ms
4,348 KB
testcase_05 AC 1 ms
4,348 KB
testcase_06 AC 1 ms
4,348 KB
testcase_07 AC 1 ms
4,348 KB
testcase_08 TLE -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: unused import: `std::collections::VecDeque`
 --> Main.rs:1:5
  |
1 | use std::collections::VecDeque;
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: `#[warn(unused_imports)]` on by default

warning: field `n` is never read
 --> Main.rs:8:5
  |
7 | struct Dijkstra {
  |        -------- field in this struct
8 |     n: usize,
  |     ^
  |
  = note: `Dijkstra` has derived impls for the traits `Clone` and `Debug`, but these are intentionally ignored during dead code analysis
  = note: `#[warn(dead_code)]` on by default

warning: methods `get_cost` and `reconstruct` are never used
  --> Main.rs:29:8
   |
14 | impl Dijkstra {
   | ------------- methods in this implementation
...
29 |     fn get_cost(&self, idx: usize) -> usize {
   |        ^^^^^^^^
...
50 |     fn reconstruct(&self, startpoint: usize, endpoint: usize) -> Vec<(usize, usize)> {
   |        ^^^^^^^^^^^

warning: 3 warnings emitted

ソースコード

diff #

use std::collections::VecDeque;
use std::{cmp::Reverse, collections::BinaryHeap};

const INF: usize = 1usize << 60;

#[derive(Debug, Clone)]
struct Dijkstra {
    n: usize,
    pathcosts: Vec<Vec<(usize, usize)>>,
    pathcostsrev: Vec<Vec<(usize, usize)>>,
    costs: Vec<usize>
}

impl Dijkstra {
    fn new(n: usize) -> Self {
        Self {
              n: n
            , pathcosts: vec![vec![]; n]
            , pathcostsrev: vec![vec![]; n]
            , costs: vec![INF; n]
        }
    }

    fn pusha2b(&mut self, a: usize, b: usize, cost: usize) {
        self.pathcosts[a].push((cost, b));
        self.pathcostsrev[b].push((cost, a));
    }

    fn get_cost(&self, idx: usize) -> usize {
        self.costs[idx]
    }

    fn solve(&mut self, startpoint: usize) {
        let mut que = BinaryHeap::new();
        que.push(Reverse((0, startpoint)));
        self.costs[startpoint] = 0;
        while let Some(Reverse(p)) = que.pop() {
            let cost = p.0;
            let dest = p.1;
            if cost > self.costs[dest] { continue; }
            for &p2 in self.pathcosts[dest].iter() {
                if self.costs[dest] + p2.0 < self.costs[p2.1] {
                    self.costs[p2.1] = self.costs[dest] + p2.0;
                    que.push(Reverse((self.costs[p2.1], p2.1)));
                }
            }
        }
    }

    fn reconstruct(&self, startpoint: usize, endpoint: usize) -> Vec<(usize, usize)> {
        let mut ret = vec![];
        if self.costs[endpoint] == INF { return ret; }
        let mut current = endpoint;
        while current != startpoint {
            for &(cost, u) in self.pathcostsrev[current].iter() {
                if self.costs[current] == self.costs[u] + cost {
                    let prev = current;
                    current = u;
                    ret.push((current, prev));
                    break;
                }
            }
        }
        ret.reverse();
        ret
    }
}

fn main() {
    let mut nmk = String::new();
    std::io::stdin().read_line(&mut nmk).ok();
    let nmk: Vec<usize> = nmk.trim().split_whitespace().map(|s| s.parse().unwrap()).collect();
    let n = nmk[0];
    let m = nmk[1];
    let k = nmk[2];
    let mut paths = Vec::with_capacity(m);
    for _ in 0..m {
        let mut temp = String::new();
        std::io::stdin().read_line(&mut temp).ok();
        let temp: Vec<usize> = temp.trim().split_whitespace().map(|s| s.parse().unwrap()).collect();
        let u = temp[0]-1;
        let v = temp[1]-1;
        let c = temp[2];
        paths.push((u, v, c));
    }

    let mut lower = 0usize;
    let mut upper = INF;
    while upper > lower {
        let middle = (upper + lower) / 2;
        let mut dijkstra = Dijkstra::new(n);
        for &(u, v, c) in paths.iter() {
            dijkstra.pusha2b(u, v, if c > middle { 1 } else { 0 });
            dijkstra.pusha2b(v, u, if c > middle { 1 } else { 0 });
        }
        dijkstra.solve(0);
        if dijkstra.costs[n-1] < k {
            upper = middle;
        } else {
            lower = middle + 1;
        }
    }
    println!("{}", upper);
}
0