結果

問題 No.1190 Points
ユーザー fukafukatanifukafukatani
提出日時 2020-08-23 15:57:36
言語 Rust
(1.77.0)
結果
AC  
実行時間 279 ms / 2,000 ms
コード長 3,299 bytes
コンパイル時間 1,112 ms
コンパイル使用メモリ 167,424 KB
実行使用メモリ 25,984 KB
最終ジャッジ日時 2024-04-23 18:03:43
合計ジャッジ時間 6,094 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 0 ms
5,376 KB
testcase_03 AC 116 ms
19,328 KB
testcase_04 AC 79 ms
17,536 KB
testcase_05 AC 120 ms
16,000 KB
testcase_06 AC 109 ms
23,936 KB
testcase_07 AC 182 ms
25,728 KB
testcase_08 AC 237 ms
25,984 KB
testcase_09 AC 214 ms
23,792 KB
testcase_10 AC 246 ms
25,856 KB
testcase_11 AC 152 ms
17,344 KB
testcase_12 AC 230 ms
25,088 KB
testcase_13 AC 149 ms
14,732 KB
testcase_14 AC 22 ms
12,928 KB
testcase_15 AC 279 ms
24,360 KB
testcase_16 AC 13 ms
5,888 KB
testcase_17 AC 222 ms
23,240 KB
testcase_18 AC 83 ms
11,688 KB
testcase_19 AC 17 ms
13,440 KB
testcase_20 AC 119 ms
13,828 KB
testcase_21 AC 30 ms
11,520 KB
testcase_22 AC 38 ms
18,304 KB
testcase_23 AC 277 ms
24,964 KB
testcase_24 AC 269 ms
25,040 KB
testcase_25 AC 122 ms
24,320 KB
testcase_26 AC 79 ms
23,808 KB
testcase_27 AC 125 ms
24,576 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: unused variable: `i`
  --> main.rs:31:9
   |
31 |     for i in 0..m {
   |         ^ help: if this is intentional, prefix it with an underscore: `_i`
   |
   = note: `#[warn(unused_variables)]` on by default

warning: 1 warning emitted

ソースコード

diff #

#![allow(unused_imports)]
#![allow(non_snake_case)]
use std::cmp::*;
use std::collections::*;
use std::io::Write;

#[allow(unused_macros)]
macro_rules! debug {
    ($($e:expr),*) => {
        #[cfg(debug_assertions)]
        $({
            let (e, mut err) = (stringify!($e), std::io::stderr());
            writeln!(err, "{} = {:?}", e, $e).unwrap()
        })*
    };
}

fn to_index(idx: usize, res: usize) -> usize {
    res + idx * 2
}

fn main() {
    let v = read_vec::<i64>();
    let (n, m, p) = (v[0] as usize, v[1] as usize, v[2]);
    //let p = min(p, 1000000 + p % 2) as usize;

    let v = read_vec::<usize>();
    let (s, g) = (v[0] - 1, v[1] - 1);

    let mut edges = vec![vec![]; 2 * n];
    for i in 0..m {
        let v = read_vec::<usize>();
        let (u, v) = (v[0] - 1, v[1] - 1);
        edges[to_index(u, 0)].push(Edge {
            to: to_index(v, 1),
            cost: 1,
        });
        edges[to_index(v, 0)].push(Edge {
            to: to_index(u, 1),
            cost: 1,
        });

        edges[to_index(u, 1)].push(Edge {
            to: to_index(v, 0),
            cost: 1,
        });
        edges[to_index(v, 1)].push(Edge {
            to: to_index(u, 0),
            cost: 1,
        });
    }

    let from_s = solve(&edges, to_index(s, 0));
    let from_g = solve(&edges, to_index(g, 0));

    if from_s[to_index(g, p as usize % 2)] > p {
        println!("-1");
        return;
    }

    let mut answers = vec![];
    for i in 0..n {
        if from_s[to_index(i, 0)] + from_g[to_index(i, p as usize % 2)] <= p {
            answers.push(i);
        } else if from_s[to_index(i, 1)] + from_g[to_index(i, 1 - (p as usize % 2))] <= p {
            answers.push(i);
        }
    }

    println!("{}", answers.len());
    for ans in answers {
        println!("{}", ans + 1);
    }
}

fn read<T: std::str::FromStr>() -> T {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    s.trim().parse().ok().unwrap()
}

fn read_vec<T: std::str::FromStr>() -> Vec<T> {
    read::<String>()
        .split_whitespace()
        .map(|e| e.parse().ok().unwrap())
        .collect()
}

use std::cmp::Ordering;
use std::collections::BinaryHeap;
const INF: i64 = 100000_00000_00000;

#[derive(PartialEq, Debug)]
struct MinInt {
    value: i64,
}

impl Eq for MinInt {}

impl PartialOrd for MinInt {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        other.value.partial_cmp(&self.value)
    }
}

impl Ord for MinInt {
    fn cmp(&self, other: &MinInt) -> Ordering {
        other.value.cmp(&self.value)
    }
}

fn make_pair(x: i64, y: usize) -> (MinInt, usize) {
    (MinInt { value: x }, y)
}

#[derive(Debug, Clone)]
struct Edge {
    to: usize,
    cost: i64,
}

fn solve(edges: &Vec<Vec<Edge>>, start_idx: usize) -> Vec<i64> {
    let num_apexes = edges.len();
    let mut d = vec![INF; num_apexes];
    d[start_idx] = 0;
    let mut que = BinaryHeap::new();
    que.push(make_pair(0, start_idx));

    while let Some((u, v)) = que.pop() {
        if d[v] < u.value {
            continue;
        }
        for e in &edges[v] {
            if d[v] != INF && d[e.to] > d[v] + e.cost {
                d[e.to] = d[v] + e.cost;
                que.push(make_pair(d[e.to], e.to));
            }
        }
    }
    d
}
0