結果

問題 No.357 品物の並び替え (Middle)
ユーザー koba-e964koba-e964
提出日時 2016-04-07 22:09:22
言語 Rust
(1.77.0)
結果
AC  
実行時間 23 ms / 5,000 ms
コード長 1,719 bytes
コンパイル時間 514 ms
コンパイル使用メモリ 163,076 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-14 22:25:44
合計ジャッジ時間 1,277 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 1 ms
6,944 KB
testcase_02 AC 1 ms
6,940 KB
testcase_03 AC 1 ms
6,944 KB
testcase_04 AC 1 ms
6,944 KB
testcase_05 AC 1 ms
6,940 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 1 ms
6,944 KB
testcase_08 AC 1 ms
6,940 KB
testcase_09 AC 5 ms
6,940 KB
testcase_10 AC 3 ms
6,940 KB
testcase_11 AC 1 ms
6,944 KB
testcase_12 AC 23 ms
6,944 KB
testcase_13 AC 12 ms
6,944 KB
testcase_14 AC 8 ms
6,944 KB
testcase_15 AC 1 ms
6,940 KB
testcase_16 AC 4 ms
6,944 KB
testcase_17 AC 3 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#[allow(unused_imports)]
use std::cmp::*;
use std::io::*;
#[allow(dead_code)]
fn getline() -> String {
    let mut ret = String::new();
    std::io::stdin().read_line(&mut ret).ok();
    return ret;
}
#[allow(dead_code)]
fn getword() -> String {
    let mut stdin = std::io::stdin();
    let mut u8b: [u8; 1] = [0];
    loop {
        let mut buf: Vec<u8> = Vec::with_capacity(16);
        loop {
            let res = stdin.read(&mut u8b);
            if res.is_err() ||u8b[0] <= ' ' as u8 {
                break;
            } else {
                buf.push(u8b[0]);
            }
        }
        if buf.len() >= 1 {
            let ret = std::string::String::from_utf8(buf).unwrap();
            return ret;
        }
    }
}
#[allow(dead_code)]
fn parse<T : std::str::FromStr>(s : &str) -> T {
     return s.parse::<T>().ok().unwrap();
}

#[allow(dead_code)]
fn get<T: std::str::FromStr>() -> T {
    parse(&getword())
}

fn main() {
    let n: usize = get();
    let m: usize = get();
    let mut items: Vec<(usize, usize, i32)> = Vec::new();
    for _ in 0 .. m {
        let a: usize = get::<usize>();
        let b: usize = get::<usize>();
        let c: i32 = get::<i32>();
        items.push((a, b, c))
    }
    let mut dp = vec![0; 1 << n];
    for bits in 0 .. (1 << n) {
        for i in 0 .. n {
            if (bits & (1 << i)) == 0 {
                continue;
            }
            let mut before = dp[bits ^ (1 << i)];
            for (f, t, s) in items.iter().cloned() {
                if t == i && (bits & (1 << f)) != 0 {
                    before += s;
                }
            }
            dp[bits] = max(dp[bits], before);
        }
    }
    println!("{}", dp[(1 << n) - 1]);
}
0