QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#273205#7883. Takeout Deliveringucup-team635#RE 0ms0kbRust5.9kb2023-12-02 22:02:552023-12-02 22:02:55

Judging History

你现在查看的是最新测评结果

  • [2023-12-02 22:02:55]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:0kb
  • [2023-12-02 22:02:55]
  • 提交

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: [(u32, u32, 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 cnt = vec![0; n];
    for &(a, b, _) in e.iter() {
        cnt[a as usize] += 1;
        cnt[b as usize] += 1;
    }
    let mut g = cnt.into_iter().map(|c| Vec::with_capacity(c)).collect::<Vec<_>>();
    for &(a, b, c) in e.iter() {
        g[a as usize].push((b as u32, c));
        g[b as usize].push((a 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 inf = std::i32::MAX;
    let mut val = inf;
    let mut ans = inf;
    for (a, b, c) in e {
        if (a as usize, b as usize) == (0, n - 1) {
            ans.chmin(c);
        }
        if let Some((p, ch)) = dsu.unite(a as usize, b as usize) {
            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 ----------

详细

Test #1:

score: 0
Runtime Error

input:

4 6
1 2 2
1 3 4
1 4 7
2 3 1
2 4 3
3 4 9

output:


result: