結果
問題 | No.2630 Colorful Vertices and Cheapest Paths |
ユーザー | naut3 |
提出日時 | 2024-02-16 22:39:49 |
言語 | Rust (1.77.0 + proconio) |
結果 |
AC
|
実行時間 | 459 ms / 2,500 ms |
コード長 | 3,928 bytes |
コンパイル時間 | 12,536 ms |
コンパイル使用メモリ | 401,080 KB |
実行使用メモリ | 6,820 KB |
最終ジャッジ日時 | 2024-09-28 21:15:59 |
合計ジャッジ時間 | 19,848 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 132 ms
6,820 KB |
testcase_01 | AC | 313 ms
6,820 KB |
testcase_02 | AC | 383 ms
6,816 KB |
testcase_03 | AC | 1 ms
6,816 KB |
testcase_04 | AC | 1 ms
6,816 KB |
testcase_05 | AC | 1 ms
6,816 KB |
testcase_06 | AC | 162 ms
6,820 KB |
testcase_07 | AC | 354 ms
6,820 KB |
testcase_08 | AC | 312 ms
6,816 KB |
testcase_09 | AC | 355 ms
6,816 KB |
testcase_10 | AC | 456 ms
6,820 KB |
testcase_11 | AC | 459 ms
6,820 KB |
testcase_12 | AC | 455 ms
6,820 KB |
testcase_13 | AC | 458 ms
6,820 KB |
testcase_14 | AC | 459 ms
6,820 KB |
testcase_15 | AC | 314 ms
6,816 KB |
testcase_16 | AC | 313 ms
6,816 KB |
testcase_17 | AC | 311 ms
6,816 KB |
testcase_18 | AC | 165 ms
6,816 KB |
testcase_19 | AC | 268 ms
6,820 KB |
testcase_20 | AC | 229 ms
6,820 KB |
testcase_21 | AC | 320 ms
6,816 KB |
ソースコード
#![allow(non_snake_case, unused_imports, unused_must_use)] use std::io::{self, prelude::*}; use std::str; fn main() { let (stdin, stdout) = (io::stdin(), io::stdout()); let mut scan = Scanner::new(stdin.lock()); let mut out = io::BufWriter::new(stdout.lock()); macro_rules! input { ($T: ty) => { scan.token::<$T>() }; ($T: ty, $N: expr) => { (0..$N).map(|_| scan.token::<$T>()).collect::<Vec<_>>() }; } let N = input!(usize); let M = input!(usize); let edges = (0..M) .map(|_| (input!(usize) - 1, input!(usize) - 1)) .collect::<Vec<_>>(); let C = (0..N).map(|_| input!(usize) - 1).collect::<Vec<_>>(); let W = input!(usize, 10); let Q = input!(usize); let mut query = vec![]; let mut ans = vec![usize::MAX; Q]; for _ in 0..Q { let (u, v) = (input!(usize) - 1, input!(usize) - 1); query.push((u, v)); } for s in 0..1 << 10 { let mut uf = UnionFind::new(N); let mut cost = 0; for i in 0..10 { if (s >> i) & 1 == 1 { cost += W[i]; } } for &(u, v) in edges.iter() { if (s >> C[u]) & 1 == 1 && (s >> C[v]) & 1 == 1 { uf.unite(u, v); } } for (i, &(u, v)) in query.iter().enumerate() { if uf.issame(u, v) { ans[i] = std::cmp::min(ans[i], cost); } } } for a in ans { if a == usize::MAX { writeln!(out, "-1"); } else { writeln!(out, "{}", a); } } } pub struct UnionFind { size: usize, par: Vec<usize>, rank: Vec<usize>, cnt: Vec<usize>, } impl UnionFind { /// generate unionfind has given size pub fn new(size: usize) -> Self { return Self { size: size, par: vec![size; size], rank: vec![0; size], cnt: vec![1; size], }; } /// check whether set s1 (∋ a) and set s2 (∋ b) are equal pub fn issame(&mut self, a: usize, b: usize) -> bool { assert!(a < self.size && b < self.size); return self.root(a) == self.root(b); } /// unite set s1 (∋ a) and set s2 (∋ b) pub fn unite(&mut self, mut a: usize, mut b: usize) { a = self.root(a); b = self.root(b); if a != b { if self.rank[a] < self.rank[b] { std::mem::swap(&mut a, &mut b); } self.par[b] = a; if self.rank[a] == self.rank[b] { self.rank[a] += 1; } self.cnt[a] += self.cnt[b]; } } /// get the size of set s1 (∋ a) pub fn size(&mut self, x: usize) -> usize { let r = self.root(x); return self.cnt[r]; } fn root(&mut self, x: usize) -> usize { if self.par[x] == self.size { return x; } else { self.par[x] = self.root(self.par[x]); return self.par[x]; } } } struct Scanner<R> { reader: R, buf_str: Vec<u8>, buf_iter: str::SplitWhitespace<'static>, } impl<R: BufRead> Scanner<R> { fn new(reader: R) -> Self { Self { reader, buf_str: vec![], buf_iter: "".split_whitespace(), } } fn token<T: str::FromStr>(&mut self) -> T { loop { if let Some(token) = self.buf_iter.next() { return token.parse().ok().expect("Failed parse"); } self.buf_str.clear(); self.reader .read_until(b'\n', &mut self.buf_str) .expect("Failed read"); self.buf_iter = unsafe { let slice = str::from_utf8_unchecked(&self.buf_str); std::mem::transmute(slice.split_whitespace()) } } } }