結果
| 問題 |
No.1320 Two Type Min Cost Cycle
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2020-12-24 13:39:50 |
| 言語 | Rust (1.83.0 + proconio) |
| 結果 |
AC
|
| 実行時間 | 1,079 ms / 2,000 ms |
| コード長 | 5,772 bytes |
| コンパイル時間 | 14,028 ms |
| コンパイル使用メモリ | 391,068 KB |
| 実行使用メモリ | 6,948 KB |
| 最終ジャッジ日時 | 2024-09-21 16:54:36 |
| 合計ジャッジ時間 | 22,952 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 57 |
ソースコード
#![allow(unused_imports, unused_macros, dead_code)]
use std::cmp::*;
use std::collections::*;
macro_rules! min {
(.. $x:expr) => {{
let mut it = $x.iter();
it.next().map(|z| it.fold(z, |x, y| min!(x, y)))
}};
($x:expr) => ($x);
($x:expr, $($ys:expr),*) => {{
let t = min!($($ys),*);
if $x < t { $x } else { t }
}}
}
macro_rules! max {
(.. $x:expr) => {{
let mut it = $x.iter();
it.next().map(|z| it.fold(z, |x, y| max!(x, y)))
}};
($x:expr) => ($x);
($x:expr, $($ys:expr),*) => {{
let t = max!($($ys),*);
if $x > t { $x } else { t }
}}
}
macro_rules! trace {
($x:expr) => {
#[cfg(debug_assertions)]
eprintln!(">>> {} = {:?}", stringify!($x), $x)
};
($($xs:expr),*) => { trace!(($($xs),*)) }
}
macro_rules! put {
(.. $x:expr) => {{
let mut it = $x.iter();
if let Some(x) = it.next() { print!("{}", x); }
for x in it { print!(" {}", x); }
println!("");
}};
($x:expr) => { println!("{}", $x) };
($x:expr, $($xs:expr),*) => { print!("{} ", $x); put!($($xs),*) }
}
fn main() {
let mut sc = Scanner::new();
let ty: usize = sc.cin();
let n: usize = sc.cin();
let m: usize = sc.cin();
let mut g = vec![vec![]; n];
let mut e = vec![];
if ty == 0 {
for _ in 0..m {
let u = sc.cin::<usize>() - 1;
let v = sc.cin::<usize>() - 1;
let w: i128 = sc.cin();
g[u].push((v, w));
g[v].push((u, w));
e.push((u, v, w));
}
} else {
for _ in 0..m {
let u = sc.cin::<usize>() - 1;
let v = sc.cin::<usize>() - 1;
let w: i128 = sc.cin();
g[u].push((v, w));
e.push((u, v, w));
}
}
match solve(n, &g, &e) {
Some(w) => put!(w),
_ => put!(-1),
}
}
fn solve(n: usize, g: &Vec<Vec<(usize, i128)>>, e: &Vec<(usize, usize, i128)>) -> Option<i128> {
use Hyper::*;
let mut ans = Inf;
for &(u0, v0, w0) in e.iter() {
// u0 -> v0 -> ... -> u0
let mut memo = vec![Inf; n];
memo[v0] = Real(0);
let mut q = BinaryHeap::new();
q.push((Reverse(0), v0));
while let Some((Reverse(cost), u)) = q.pop() {
for &(v, w) in g[u].iter() {
if (u, v) == (u0, v0) || (u, v) == (v0, u0) {
continue;
}
let c = cost + w;
if memo[v] > Real(c) {
memo[v] = Real(c);
q.push((Reverse(c), v));
}
}
}
ans = min!(ans, memo[u0] + w0);
}
ans.ok()
}
// @algebra/hyper
// @algebra/group
pub trait Group:
std::ops::Add<Output = Self>
+ std::ops::Sub<Output = Self>
+ std::ops::Neg<Output = Self>
+ std::iter::Sum
{
fn zero() -> Self;
}
macro_rules! define_group {
($t:ty, $x:expr) => {
impl Group for $t {
fn zero() -> Self {
$x
}
}
};
}
define_group!(i32, 0);
define_group!(i64, 0);
define_group!(i128, 0);
define_group!(f32, 0.0);
define_group!(f64, 0.0);
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum Hyper<X> {
NegInf,
Real(X),
Inf,
}
use Hyper::{Inf, NegInf, Real};
impl<X> Hyper<X> {
pub fn unwrap(self) -> X {
if let Hyper::Real(x) = self {
x
} else {
panic!()
}
}
pub fn ok(self) -> Option<X> {
if let Hyper::Real(x) = self {
Some(x)
} else {
None
}
}
}
impl<X: Group> std::ops::Add for Hyper<X> {
type Output = Self;
fn add(self, rhs: Hyper<X>) -> Hyper<X> {
match (self, rhs) {
(Real(x), Real(y)) => Real(x + y),
(Inf, _) => Inf,
(_, Inf) => Inf,
_ => NegInf,
}
}
}
impl<X: Group> std::ops::Sub for Hyper<X> {
type Output = Self;
fn sub(self, rhs: Hyper<X>) -> Hyper<X> {
self + (-rhs)
}
}
impl<X: Group> std::ops::Neg for Hyper<X> {
type Output = Self;
fn neg(self) -> Hyper<X> {
match self {
Inf => NegInf,
NegInf => Inf,
Real(x) => Real(-x),
}
}
}
impl<X: Group> std::iter::Sum for Hyper<X> {
fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
iter.fold(Hyper::zero(), std::ops::Add::add)
}
}
impl<X: Group> Group for Hyper<X> {
fn zero() -> Self {
Hyper::Real(X::zero())
}
}
impl<X: Group> std::ops::Add<X> for Hyper<X> {
type Output = Self;
fn add(self, other: X) -> Self {
match self {
Inf => Inf,
NegInf => NegInf,
Real(x) => Real(x + other),
}
}
}
use std::collections::VecDeque;
use std::io::{self, Write};
use std::str::FromStr;
struct Scanner {
stdin: io::Stdin,
buffer: VecDeque<String>,
}
impl Scanner {
fn new() -> Self {
Scanner {
stdin: io::stdin(),
buffer: VecDeque::new(),
}
}
fn cin<T: FromStr>(&mut self) -> T {
while self.buffer.is_empty() {
let mut line = String::new();
let _ = self.stdin.read_line(&mut line);
for w in line.split_whitespace() {
self.buffer.push_back(String::from(w));
}
}
self.buffer.pop_front().unwrap().parse::<T>().ok().unwrap()
}
fn chars(&mut self) -> Vec<char> {
self.cin::<String>().chars().collect()
}
fn vec<T: FromStr>(&mut self, n: usize) -> Vec<T> {
(0..n).map(|_| self.cin()).collect()
}
}
fn flush() {
std::io::stdout().flush().unwrap();
}