結果

問題 No.1480 Many Complete Graphs
ユーザー koba-e964koba-e964
提出日時 2021-10-15 09:28:00
言語 Rust
(1.77.0)
結果
AC  
実行時間 96 ms / 2,000 ms
コード長 2,751 bytes
コンパイル時間 881 ms
コンパイル使用メモリ 176,956 KB
実行使用メモリ 18,160 KB
最終ジャッジ日時 2023-10-17 19:34:35
合計ジャッジ時間 4,952 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 0 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 0 ms
4,348 KB
testcase_08 AC 1 ms
4,348 KB
testcase_09 AC 1 ms
4,348 KB
testcase_10 AC 1 ms
4,348 KB
testcase_11 AC 1 ms
4,348 KB
testcase_12 AC 1 ms
4,348 KB
testcase_13 AC 17 ms
8,460 KB
testcase_14 AC 11 ms
6,084 KB
testcase_15 AC 27 ms
9,460 KB
testcase_16 AC 17 ms
7,416 KB
testcase_17 AC 16 ms
6,916 KB
testcase_18 AC 5 ms
4,764 KB
testcase_19 AC 17 ms
6,876 KB
testcase_20 AC 22 ms
7,668 KB
testcase_21 AC 18 ms
8,196 KB
testcase_22 AC 9 ms
5,292 KB
testcase_23 AC 36 ms
14,004 KB
testcase_24 AC 33 ms
9,580 KB
testcase_25 AC 53 ms
12,860 KB
testcase_26 AC 59 ms
14,292 KB
testcase_27 AC 32 ms
9,804 KB
testcase_28 AC 42 ms
16,892 KB
testcase_29 AC 44 ms
17,152 KB
testcase_30 AC 42 ms
16,844 KB
testcase_31 AC 44 ms
16,844 KB
testcase_32 AC 45 ms
17,156 KB
testcase_33 AC 43 ms
16,844 KB
testcase_34 AC 44 ms
17,156 KB
testcase_35 AC 42 ms
16,580 KB
testcase_36 AC 43 ms
16,580 KB
testcase_37 AC 43 ms
16,584 KB
testcase_38 AC 43 ms
16,580 KB
testcase_39 AC 43 ms
16,580 KB
testcase_40 AC 43 ms
16,576 KB
testcase_41 AC 43 ms
16,580 KB
testcase_42 AC 43 ms
17,152 KB
testcase_43 AC 44 ms
16,892 KB
testcase_44 AC 43 ms
16,844 KB
testcase_45 AC 44 ms
17,152 KB
testcase_46 AC 43 ms
16,844 KB
testcase_47 AC 91 ms
17,540 KB
testcase_48 AC 96 ms
17,524 KB
testcase_49 AC 91 ms
17,528 KB
testcase_50 AC 56 ms
17,104 KB
testcase_51 AC 94 ms
17,536 KB
testcase_52 AC 65 ms
16,888 KB
testcase_53 AC 64 ms
16,888 KB
testcase_54 AC 63 ms
16,888 KB
testcase_55 AC 62 ms
16,884 KB
testcase_56 AC 62 ms
16,888 KB
testcase_57 AC 49 ms
18,160 KB
testcase_58 AC 52 ms
15,656 KB
evil_aftercontest.txt AC 105 ms
29,028 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