QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#273200 | #7883. Takeout Delivering | ucup-team635# | WA | 179ms | 61792kb | Rust | 5.7kb | 2023-12-02 21:59:48 | 2023-12-02 21:59:49 |
Judging History
answer
use std::collections::*;
use std::io::Write;
type Map<K, V> = BTreeMap<K, V>;
type Set<T> = BTreeSet<T>;
type Deque<T> = VecDeque<T>;
fn main() {
input! {
n: usize,
m: usize,
e: [(usize1, usize1, i32); m],
}
let mut e = e;
for e in e.iter_mut() {
*e = (e.0.min(e.1), e.0.max(e.1), e.2);
}
e.sort_by_key(|e| e.2);
let mut g = vec![vec![]; n];
for &(a, b, c) in e.iter() {
g[a].push((b as u32, c));
g[b].push((b as u32, c));
}
let mut dp = vec![false; n];
let mut ep = vec![false; n];
dp[0] = true;
ep[n - 1] = true;
let mut dsu = DSU::new(n);
let mut tree = vec![vec![]; n];
let inf = std::i32::MAX;
let mut val = inf;
let mut ans = inf;
for (a, b, c) in e {
if (a, b) == (0, n - 1) {
ans.chmin(c);
}
if let Some((p, ch)) = dsu.unite(a, b) {
tree[a].push((b, c));
tree[b].push((a, c));
if dp[p] != dp[ch] || ep[p] != ep[ch] {
for &r in [p, ch].iter() {
if (!dp[r] && dp[r ^ p ^ ch]) || (!ep[r] && ep[r ^ p ^ ch]) {
for &(b, w) in g[r].iter() {
let b = b as usize;
let mut p = [dsu.root(0), dsu.root(n - 1)];
let mut q = [dsu.root(r), dsu.root(b)];
p.sort();
q.sort();
if p[0] != p[1] && p == q {
val.chmin(w);
}
}
}
}
}
dp[p] |= dp[ch];
ep[p] |= ep[ch];
let mut x = std::mem::take(&mut g[ch]);
for (b, w) in x {
if dsu.same(p, b as usize) {
continue;
}
g[p].push((b, w));
}
}
if val < inf {
ans.chmin(c + val);
}
}
println!("{}", ans);
}
// ---------- begin input macro ----------
// reference: https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
#[macro_export]
macro_rules! input {
(source = $s:expr, $($r:tt)*) => {
let mut iter = $s.split_whitespace();
input_inner!{iter, $($r)*}
};
($($r:tt)*) => {
let s = {
use std::io::Read;
let mut s = String::new();
std::io::stdin().read_to_string(&mut s).unwrap();
s
};
let mut iter = s.split_whitespace();
input_inner!{iter, $($r)*}
};
}
#[macro_export]
macro_rules! input_inner {
($iter:expr) => {};
($iter:expr, ) => {};
($iter:expr, $var:ident : $t:tt $($r:tt)*) => {
let $var = read_value!($iter, $t);
input_inner!{$iter $($r)*}
};
}
#[macro_export]
macro_rules! read_value {
($iter:expr, ( $($t:tt),* )) => {
( $(read_value!($iter, $t)),* )
};
($iter:expr, [ $t:tt ; $len:expr ]) => {
(0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
};
($iter:expr, chars) => {
read_value!($iter, String).chars().collect::<Vec<char>>()
};
($iter:expr, bytes) => {
read_value!($iter, String).bytes().collect::<Vec<u8>>()
};
($iter:expr, usize1) => {
read_value!($iter, usize) - 1
};
($iter:expr, $t:ty) => {
$iter.next().unwrap().parse::<$t>().expect("Parse error")
};
}
// ---------- end input macro ----------
//---------- begin union_find ----------
pub struct DSU {
p: Vec<i32>,
}
impl DSU {
pub fn new(n: usize) -> DSU {
assert!(n < std::i32::MAX as usize);
DSU { p: vec![-1; n] }
}
pub fn init(&mut self) {
self.p.iter_mut().for_each(|p| *p = -1);
}
pub fn root(&self, mut x: usize) -> usize {
assert!(x < self.p.len());
while self.p[x] >= 0 {
x = self.p[x] as usize;
}
x
}
pub fn same(&self, x: usize, y: usize) -> bool {
assert!(x < self.p.len() && y < self.p.len());
self.root(x) == self.root(y)
}
pub fn unite(&mut self, x: usize, y: usize) -> Option<(usize, usize)> {
assert!(x < self.p.len() && y < self.p.len());
let mut x = self.root(x);
let mut y = self.root(y);
if x == y {
return None;
}
if self.p[x] > self.p[y] {
std::mem::swap(&mut x, &mut y);
}
self.p[x] += self.p[y];
self.p[y] = x as i32;
Some((x, y))
}
pub fn parent(&self, x: usize) -> Option<usize> {
assert!(x < self.p.len());
let p = self.p[x];
if p >= 0 {
Some(p as usize)
} else {
None
}
}
pub fn sum<F>(&self, mut x: usize, mut f: F) -> usize
where
F: FnMut(usize),
{
while let Some(p) = self.parent(x) {
f(x);
x = p;
}
x
}
pub fn size(&self, x: usize) -> usize {
assert!(x < self.p.len());
let r = self.root(x);
(-self.p[r]) as usize
}
}
//---------- end union_find ----------
// ---------- begin chmin, chmax ----------
pub trait ChangeMinMax {
fn chmin(&mut self, x: Self) -> bool;
fn chmax(&mut self, x: Self) -> bool;
}
impl<T: PartialOrd> ChangeMinMax for T {
fn chmin(&mut self, x: Self) -> bool {
*self > x && {
*self = x;
true
}
}
fn chmax(&mut self, x: Self) -> bool {
*self < x && {
*self = x;
true
}
}
}
// ---------- end chmin, chmax ----------
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 2000kb
input:
4 6 1 2 2 1 3 4 1 4 7 2 3 1 2 4 3 3 4 9
output:
5
result:
ok 1 number(s): "5"
Test #2:
score: 0
Accepted
time: 179ms
memory: 61792kb
input:
300000 299999 80516 80517 597830404 110190 110191 82173886 218008 218009 954561262 250110 250111 942489774 66540 66541 156425292 34947 34948 239499776 273789 273790 453201232 84428 84429 439418398 98599 98600 326095035 55636 55637 355015760 158611 158612 684292473 43331 43332 43265001 171621 171622 ...
output:
1999991697
result:
ok 1 number(s): "1999991697"
Test #3:
score: -100
Wrong Answer
time: 142ms
memory: 60096kb
input:
300000 299999 207226 231742 414945003 84591 210444 175968953 46327 51582 612565723 18773 141119 82562646 76139 286963 762958587 131867 224820 928900783 215240 216181 405340417 144725 290878 195350550 267216 268752 846015171 31413 255927 389339642 45219 147512 489502910 113391 215402 555706684 53359 ...
output:
2147483647
result:
wrong answer 1st numbers differ - expected: '1989898633', found: '2147483647'