結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 0 ms
6,816 KB
testcase_01 AC 1 ms
6,944 KB
testcase_02 AC 1 ms
6,940 KB
testcase_03 AC 0 ms
6,940 KB
testcase_04 AC 1 ms
6,944 KB
testcase_05 AC 1 ms
6,940 KB
testcase_06 AC 1 ms
6,944 KB
testcase_07 AC 1 ms
6,944 KB
testcase_08 AC 1 ms
6,944 KB
testcase_09 AC 1 ms
6,940 KB
testcase_10 AC 1 ms
6,940 KB
testcase_11 AC 1 ms
6,944 KB
testcase_12 AC 1 ms
6,944 KB
testcase_13 AC 14 ms
8,320 KB
testcase_14 AC 8 ms
6,940 KB
testcase_15 AC 20 ms
9,344 KB
testcase_16 AC 13 ms
7,168 KB
testcase_17 AC 12 ms
6,940 KB
testcase_18 AC 4 ms
6,940 KB
testcase_19 AC 14 ms
6,944 KB
testcase_20 AC 17 ms
7,552 KB
testcase_21 AC 14 ms
8,192 KB
testcase_22 AC 8 ms
6,944 KB
testcase_23 AC 29 ms
13,952 KB
testcase_24 AC 26 ms
9,600 KB
testcase_25 AC 39 ms
12,928 KB
testcase_26 AC 47 ms
14,208 KB
testcase_27 AC 27 ms
9,728 KB
testcase_28 AC 39 ms
16,876 KB
testcase_29 AC 37 ms
17,260 KB
testcase_30 AC 36 ms
16,748 KB
testcase_31 AC 36 ms
16,748 KB
testcase_32 AC 37 ms
17,260 KB
testcase_33 AC 35 ms
16,748 KB
testcase_34 AC 37 ms
17,256 KB
testcase_35 AC 36 ms
16,616 KB
testcase_36 AC 35 ms
16,876 KB
testcase_37 AC 35 ms
16,748 KB
testcase_38 AC 35 ms
16,748 KB
testcase_39 AC 34 ms
16,620 KB
testcase_40 AC 37 ms
16,744 KB
testcase_41 AC 34 ms
16,748 KB
testcase_42 AC 36 ms
17,260 KB
testcase_43 AC 34 ms
16,880 KB
testcase_44 AC 35 ms
16,744 KB
testcase_45 AC 36 ms
17,260 KB
testcase_46 AC 35 ms
16,624 KB
testcase_47 AC 68 ms
17,348 KB
testcase_48 AC 64 ms
17,428 KB
testcase_49 AC 66 ms
17,452 KB
testcase_50 AC 43 ms
17,168 KB
testcase_51 AC 64 ms
17,340 KB
testcase_52 AC 47 ms
16,924 KB
testcase_53 AC 51 ms
16,924 KB
testcase_54 AC 50 ms
16,928 KB
testcase_55 AC 47 ms
16,920 KB
testcase_56 AC 47 ms
16,920 KB
testcase_57 AC 41 ms
18,308 KB
testcase_58 AC 43 ms
15,828 KB
evil_aftercontest.txt AC 90 ms
28,948 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#[allow(unused_imports)]
use std::cmp::*;
#[allow(unused_imports)]
use std::collections::*;
use std::io::Read;

#[allow(dead_code)]
fn getline() -> String {
    let mut ret = String::new();
    std::io::stdin().read_line(&mut ret).ok().unwrap();
    ret
}

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