結果

問題 No.1320 Two Type Min Cost Cycle
ユーザー fukafukatanifukafukatani
提出日時 2020-12-20 20:08:22
言語 Rust
(1.77.0)
結果
AC  
実行時間 125 ms / 2,000 ms
コード長 3,191 bytes
コンパイル時間 1,166 ms
コンパイル使用メモリ 184,516 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-21 11:00:21
合計ジャッジ時間 4,699 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 1 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 0 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 125 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 89 ms
4,348 KB
testcase_12 AC 8 ms
4,348 KB
testcase_13 AC 39 ms
4,348 KB
testcase_14 AC 1 ms
4,348 KB
testcase_15 AC 2 ms
4,348 KB
testcase_16 AC 7 ms
4,348 KB
testcase_17 AC 2 ms
4,348 KB
testcase_18 AC 1 ms
4,348 KB
testcase_19 AC 52 ms
4,348 KB
testcase_20 AC 1 ms
4,348 KB
testcase_21 AC 123 ms
4,348 KB
testcase_22 AC 0 ms
4,348 KB
testcase_23 AC 1 ms
4,348 KB
testcase_24 AC 1 ms
4,348 KB
testcase_25 AC 1 ms
4,348 KB
testcase_26 AC 1 ms
4,348 KB
testcase_27 AC 1 ms
4,348 KB
testcase_28 AC 1 ms
4,348 KB
testcase_29 AC 73 ms
4,348 KB
testcase_30 AC 1 ms
4,348 KB
testcase_31 AC 13 ms
4,348 KB
testcase_32 AC 1 ms
4,348 KB
testcase_33 AC 82 ms
4,348 KB
testcase_34 AC 38 ms
4,348 KB
testcase_35 AC 6 ms
4,348 KB
testcase_36 AC 3 ms
4,348 KB
testcase_37 AC 2 ms
4,348 KB
testcase_38 AC 9 ms
4,348 KB
testcase_39 AC 2 ms
4,348 KB
testcase_40 AC 1 ms
4,348 KB
testcase_41 AC 1 ms
4,348 KB
testcase_42 AC 1 ms
4,348 KB
testcase_43 AC 3 ms
4,348 KB
testcase_44 AC 2 ms
4,348 KB
testcase_45 AC 115 ms
4,348 KB
testcase_46 AC 9 ms
4,348 KB
testcase_47 AC 32 ms
4,348 KB
testcase_48 AC 63 ms
4,348 KB
testcase_49 AC 8 ms
4,348 KB
testcase_50 AC 1 ms
4,348 KB
testcase_51 AC 0 ms
4,348 KB
testcase_52 AC 11 ms
4,348 KB
testcase_53 AC 6 ms
4,348 KB
testcase_54 AC 75 ms
4,348 KB
testcase_55 AC 75 ms
4,348 KB
testcase_56 AC 74 ms
4,348 KB
testcase_57 AC 105 ms
4,348 KB
testcase_58 AC 105 ms
4,348 KB
testcase_59 AC 105 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

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

fn main() {
    let t = read::<usize>();
    let v = read_vec::<usize>();
    let (n, m) = (v[0], v[1]);
    let mut edges = vec![vec![]; n];

    let mut edge_list = vec![];
    for _ in 0..m {
        let v = read_vec::<i64>();
        let (a, b, c) = (v[0] as usize - 1, v[1] as usize - 1, v[2]);
        edges[a].push(Edge { to: b, cost: c });
        if t == 0 {
            edges[b].push(Edge { to: a, cost: c });
        }
        edge_list.push((a, b));
    }

    let mut ans = INF;
    for (a, b) in edge_list {
        let mut idx = 0;
        let mut cost = 0;
        for i in 0..edges[a].len() {
            if edges[a][i].to == b {
                idx = i;
                cost = edges[a][i].cost;
            }
        }
        edges[a].remove(idx);
        if t == 0 {
            let mut idx2 = 0;
            for i in 0..edges[b].len() {
                if edges[b][i].to == a {
                    idx2 = i;
                }
            }
            edges[b].remove(idx2);
        }
        let d = solve(&edges, b);
        edges[a].push(Edge { to: b, cost: cost });
        if t == 0 {
            edges[b].push(Edge { to: a, cost: cost });
        }
        ans = min(ans, d[a] + cost);
    }
    if ans == INF {
        println!("-1");
        return;
    }
    println!("{}", ans);
}

use std::cmp::Ordering;
use std::collections::BinaryHeap;
type Cost = i64;

const INF: Cost = 100000_00000_00000;

#[derive(PartialEq, Debug)]
struct MinInt {
    value: Cost,
}

impl Eq for MinInt {}

impl PartialOrd for MinInt {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        other.value.partial_cmp(&self.value)
    }
}

impl Ord for MinInt {
    fn cmp(&self, other: &MinInt) -> Ordering {
        other.value.partial_cmp(&self.value).unwrap()
    }
}

fn make_pair(x: Cost, y: usize) -> (MinInt, usize) {
    (MinInt { value: x }, y)
}

#[derive(Debug, Clone)]
struct Edge {
    to: usize,
    cost: Cost,
}

fn solve(edges: &Vec<Vec<Edge>>, start_idx: usize) -> Vec<Cost> {
    let num_apexes = edges.len();
    let mut d = vec![INF; num_apexes];
    d[start_idx] = 0;
    let mut que = BinaryHeap::new();
    que.push(make_pair(0, start_idx));

    while let Some((u, v)) = que.pop() {
        if d[v] < u.value {
            continue;
        }
        for e in &edges[v] {
            if d[v] != INF && d[e.to] > d[v] + e.cost {
                d[e.to] = d[v] + e.cost;
                que.push(make_pair(d[e.to], e.to));
            }
        }
    }
    d
}

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