結果
| 問題 |
No.1320 Two Type Min Cost Cycle
|
| コンテスト | |
| ユーザー |
fukafukatani
|
| 提出日時 | 2020-12-20 20:08:22 |
| 言語 | Rust (1.83.0 + proconio) |
| 結果 |
AC
|
| 実行時間 | 129 ms / 2,000 ms |
| コード長 | 3,191 bytes |
| コンパイル時間 | 13,184 ms |
| コンパイル使用メモリ | 378,368 KB |
| 実行使用メモリ | 5,376 KB |
| 最終ジャッジ日時 | 2024-09-21 12:09:44 |
| 合計ジャッジ時間 | 16,526 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 57 |
ソースコード
#![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()
}
fukafukatani