結果

問題 No.2630 Colorful Vertices and Cheapest Paths
ユーザー akakimidoriakakimidori
提出日時 2024-02-16 21:40:35
言語 Rust
(1.77.0)
結果
AC  
実行時間 336 ms / 2,500 ms
コード長 4,723 bytes
コンパイル時間 2,748 ms
コンパイル使用メモリ 198,892 KB
実行使用メモリ 6,548 KB
最終ジャッジ日時 2024-02-16 21:40:43
合計ジャッジ時間 7,924 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 108 ms
6,548 KB
testcase_01 AC 291 ms
6,548 KB
testcase_02 AC 213 ms
6,548 KB
testcase_03 AC 1 ms
6,548 KB
testcase_04 AC 1 ms
6,548 KB
testcase_05 AC 1 ms
6,548 KB
testcase_06 AC 88 ms
6,548 KB
testcase_07 AC 165 ms
6,548 KB
testcase_08 AC 155 ms
6,548 KB
testcase_09 AC 195 ms
6,548 KB
testcase_10 AC 267 ms
6,548 KB
testcase_11 AC 267 ms
6,548 KB
testcase_12 AC 295 ms
6,548 KB
testcase_13 AC 294 ms
6,548 KB
testcase_14 AC 266 ms
6,548 KB
testcase_15 AC 314 ms
6,548 KB
testcase_16 AC 309 ms
6,548 KB
testcase_17 AC 336 ms
6,548 KB
testcase_18 AC 93 ms
6,548 KB
testcase_19 AC 136 ms
6,548 KB
testcase_20 AC 121 ms
6,548 KB
testcase_21 AC 161 ms
6,548 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: type alias `Map` is never used
 --> Main.rs:4:6
  |
4 | type Map<K, V> = BTreeMap<K, V>;
  |      ^^^
  |
  = note: `#[warn(dead_code)]` on by default

warning: type alias `Set` is never used
 --> Main.rs:5:6
  |
5 | type Set<T> = BTreeSet<T>;
  |      ^^^

warning: type alias `Deque` is never used
 --> Main.rs:6:6
  |
6 | type Deque<T> = VecDeque<T>;
  |      ^^^^^

warning: 3 warnings emitted

ソースコード

diff #

use std::io::Write;
use std::collections::*;

type Map<K, V> = BTreeMap<K, V>;
type Set<T> = BTreeSet<T>;
type Deque<T> = VecDeque<T>;

fn run() {
    let color = 10;
    input! {
        n: usize,
        m: usize,
        e: [(usize1, usize1); m],
        c: [usize1; n],
        w: [i64; color],
        q: usize,
        ask: [(usize1, usize1); q],
    }
    let mut ans = vec![std::i64::MAX; q];
    for bit in 1..(1 << 10) {
        let mut dsu = DSU::new(n);
        for &(a, b) in e.iter() {
            if bit >> c[a] & 1 == 1 && bit >> c[b] & 1 == 1 {
                dsu.unite(a, b);
            }
        }
        let sum = w.iter().enumerate().filter(|p| bit >> p.0 & 1 == 1).map(|p| *p.1).sum::<i64>();
        for (ans, &(a, b)) in ans.iter_mut().zip(ask.iter()) {
            if dsu.same(a, b) {
                ans.chmin(sum);
            }
        }
    }
    let out = std::io::stdout();
    let mut out = std::io::BufWriter::new(out.lock());
    for mut a in ans {
        if a == std::i64::MAX {
            a = -1;
        }
        writeln!(out, "{}", a).ok();
    }
}

fn main() {
    run();
}

// ---------- 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 ----------
0