結果

問題 No.1480 Many Complete Graphs
ユーザー koba-e964koba-e964
提出日時 2021-10-15 09:28:00
言語 Rust
(1.77.0 + proconio)
結果
AC  
実行時間 88 ms / 2,000 ms
コード長 2,751 bytes
コンパイル時間 12,400 ms
コンパイル使用メモリ 401,904 KB
実行使用メモリ 18,256 KB
最終ジャッジ日時 2024-09-17 16:46:26
合計ジャッジ時間 16,538 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 1 ms
6,812 KB
testcase_02 AC 1 ms
6,944 KB
testcase_03 AC 1 ms
6,944 KB
testcase_04 AC 1 ms
6,940 KB
testcase_05 AC 1 ms
6,940 KB
testcase_06 AC 1 ms
6,940 KB
testcase_07 AC 1 ms
6,940 KB
testcase_08 AC 1 ms
6,940 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 1 ms
6,944 KB
testcase_12 AC 2 ms
6,940 KB
testcase_13 AC 17 ms
8,320 KB
testcase_14 AC 11 ms
6,944 KB
testcase_15 AC 27 ms
9,344 KB
testcase_16 AC 17 ms
7,296 KB
testcase_17 AC 15 ms
6,944 KB
testcase_18 AC 5 ms
6,944 KB
testcase_19 AC 17 ms
6,940 KB
testcase_20 AC 23 ms
7,680 KB
testcase_21 AC 17 ms
8,192 KB
testcase_22 AC 9 ms
6,944 KB
testcase_23 AC 37 ms
14,080 KB
testcase_24 AC 32 ms
9,472 KB
testcase_25 AC 51 ms
12,928 KB
testcase_26 AC 61 ms
14,208 KB
testcase_27 AC 31 ms
9,728 KB
testcase_28 AC 44 ms
17,132 KB
testcase_29 AC 43 ms
17,132 KB
testcase_30 AC 43 ms
16,620 KB
testcase_31 AC 42 ms
16,748 KB
testcase_32 AC 44 ms
17,256 KB
testcase_33 AC 43 ms
16,876 KB
testcase_34 AC 42 ms
17,260 KB
testcase_35 AC 41 ms
16,748 KB
testcase_36 AC 42 ms
16,876 KB
testcase_37 AC 42 ms
16,748 KB
testcase_38 AC 42 ms
16,620 KB
testcase_39 AC 43 ms
16,748 KB
testcase_40 AC 43 ms
16,748 KB
testcase_41 AC 42 ms
16,748 KB
testcase_42 AC 42 ms
17,260 KB
testcase_43 AC 42 ms
16,748 KB
testcase_44 AC 42 ms
16,620 KB
testcase_45 AC 43 ms
17,252 KB
testcase_46 AC 42 ms
16,752 KB
testcase_47 AC 88 ms
17,536 KB
testcase_48 AC 87 ms
17,280 KB
testcase_49 AC 87 ms
17,460 KB
testcase_50 AC 50 ms
17,188 KB
testcase_51 AC 86 ms
17,524 KB
testcase_52 AC 62 ms
16,808 KB
testcase_53 AC 60 ms
17,048 KB
testcase_54 AC 60 ms
16,924 KB
testcase_55 AC 64 ms
16,920 KB
testcase_56 AC 60 ms
17,048 KB
testcase_57 AC 48 ms
18,256 KB
testcase_58 AC 52 ms
15,696 KB
evil_aftercontest.txt AC 109 ms
28,952 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::io::Read;

fn get_word() -> String {
    let stdin = std::io::stdin();
    let mut stdin=stdin.lock();
    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.unwrap_or(0) == 0 || u8b[0] <= b' ' {
                break;
            } else {
                buf.push(u8b[0]);
            }
        }
        if buf.len() >= 1 {
            let ret = String::from_utf8(buf).unwrap();
            return ret;
        }
    }
}

#[allow(dead_code)]
fn get<T: std::str::FromStr>() -> T { get_word().parse().ok().unwrap() }

/*
 * Dijkstra's algorithm.
 * Verified by: AtCoder ABC164 (https://atcoder.jp/contests/abc164/submissions/12423853)
 */

struct Dijkstra {
    edges: Vec<Vec<(usize, i64)>>, // adjacent list representation
}

impl Dijkstra {
    fn new(n: usize) -> Self {
        Dijkstra { edges: vec![Vec::new(); n] }
    }
    fn add_edge(&mut self, from: usize, to: usize, cost: i64) {
        self.edges[from].push((to, cost));
    }
    /*
     * This function returns a Vec consisting of the distances from vertex source.
     */
    fn solve(&self, source: usize, inf: i64) -> Vec<i64> {
        let n = self.edges.len();
        let mut d = vec![inf; n];
        // que holds (-distance, vertex), so that que.pop() returns the nearest element.
        let mut que = std::collections::BinaryHeap::new();
        que.push((0, source));
        while let Some((cost, pos)) = que.pop() {
            let cost = -cost;
            if d[pos] <= cost {
                continue;
            }
            d[pos] = cost;
            for &(w, c) in &self.edges[pos] {
                let newcost = cost + c;
                if d[w] > newcost {
                    d[w] = newcost + 1;
                    que.push((-newcost, w));
                }
            }
        }
        return d;
    }
}

fn main() {
    let n: usize = get();
    let m: usize = get();
    let mut dijk = Dijkstra::new(n + 2 * m);
    for i in 0..m {
        let k: usize = get();
        let c: i64 = get();
        let s: Vec<usize> = (0..k).map(|_| get()).collect();
        for &s in &s {
            if s % 2 == 0 {
                dijk.add_edge(s - 1, n + 2 * i, s as i64 / 2 + c);
                dijk.add_edge(n + 2 * i, s - 1, s as i64 / 2);
            } else {
                dijk.add_edge(s - 1, n + 2 * i + 1, s as i64 / 2 + c);
                dijk.add_edge(n + 2 * i, s - 1, s as i64 / 2 + 1);
            }
            dijk.add_edge(n + 2 * i + 1, s - 1, s as i64 / 2 + 1);
        }
    }
    const INF: i64 = 1 << 50;
    let sol = dijk.solve(0, INF);
    println!("{}", if sol[n - 1] >= INF { -1 } else { sol[n - 1] });
}
0