結果

問題 No.1340 おーじ君をさがせ
ユーザー fukafukatani
提出日時 2021-01-15 22:41:50
言語 Rust
(1.83.0 + proconio)
結果
AC  
実行時間 273 ms / 2,000 ms
コード長 1,767 bytes
コンパイル時間 11,322 ms
コンパイル使用メモリ 379,664 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-11-27 23:16:57
合計ジャッジ時間 16,439 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 59
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: type `mat` should have an upper camel case name
  --> src/main.rs:18:6
   |
18 | type mat = Vec<Vec<i64>>;
   |      ^^^ help: convert the identifier to upper camel case: `Mat`
   |
   = note: `#[warn(non_camel_case_types)]` on by default

warning: unused variable: `i`
  --> src/main.rs:58:9
   |
58 |     for i in 0..m {
   |         ^ help: if this is intentional, prefix it with an underscore: `_i`
   |
   = note: `#[warn(unused_variables)]` on by default

ソースコード

diff #

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

#[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()
        })*
    };
}

type mat = Vec<Vec<i64>>;

fn mat_zeros(n: usize, m: usize) -> mat {
    vec![vec![0i64; m]; n]
}

fn matmul(a: &mat, b: &mat) -> mat {
    let mut c = mat_zeros(a.len(), b[0].len());
    for i in 0..a.len() {
        for k in 0..b.len() {
            for j in 0..b[0].len() {
                c[i][j] += a[i][k] * b[k][j];
                c[i][j] = min(1, c[i][j]);
            }
        }
    }
    c
}

fn matpow(a: &mat, n: u64) -> mat {
    let mut b = mat_zeros(a.len(), a.len());
    for i in 0..a.len() {
        b[i][i] = 1;
    }
    let mut n = n;
    let mut a: mat = a.clone();
    while n > 0 {
        if n & 1 == 1 {
            b = matmul(&a, &b);
        }
        a = matmul(&a, &a);
        n >>= 1;
    }
    b
}

fn main() {
    let v = read_vec::<u64>();
    let (n, m, t) = (v[0] as usize, v[1] as usize, v[2]);
    let mut c = mat_zeros(n, n);
    for i in 0..m {
        let v = read_vec::<usize>();
        let (a, b) = (v[0], v[1]);
        c[a][b] = 1;
    }
    let c = matpow(&c, t);
    let ans = c[0].iter().sum::<i64>();
    println!("{}", ans);
}

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()
}
0