結果
問題 | No.1690 Power Grid |
ユーザー | phspls |
提出日時 | 2022-10-18 17:59:22 |
言語 | Rust (1.77.0 + proconio) |
結果 |
AC
|
実行時間 | 114 ms / 3,000 ms |
コード長 | 3,883 bytes |
コンパイル時間 | 15,998 ms |
コンパイル使用メモリ | 377,552 KB |
実行使用メモリ | 6,912 KB |
最終ジャッジ日時 | 2024-06-29 01:22:37 |
合計ジャッジ時間 | 17,453 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
5,248 KB |
testcase_01 | AC | 1 ms
5,376 KB |
testcase_02 | AC | 1 ms
5,376 KB |
testcase_03 | AC | 1 ms
5,376 KB |
testcase_04 | AC | 1 ms
5,376 KB |
testcase_05 | AC | 1 ms
5,376 KB |
testcase_06 | AC | 62 ms
6,784 KB |
testcase_07 | AC | 62 ms
6,784 KB |
testcase_08 | AC | 63 ms
6,912 KB |
testcase_09 | AC | 62 ms
6,784 KB |
testcase_10 | AC | 1 ms
5,376 KB |
testcase_11 | AC | 1 ms
5,376 KB |
testcase_12 | AC | 1 ms
5,376 KB |
testcase_13 | AC | 1 ms
5,376 KB |
testcase_14 | AC | 12 ms
5,376 KB |
testcase_15 | AC | 1 ms
5,376 KB |
testcase_16 | AC | 104 ms
6,784 KB |
testcase_17 | AC | 57 ms
5,376 KB |
testcase_18 | AC | 25 ms
5,376 KB |
testcase_19 | AC | 98 ms
6,784 KB |
testcase_20 | AC | 114 ms
6,784 KB |
testcase_21 | AC | 1 ms
5,376 KB |
testcase_22 | AC | 39 ms
5,376 KB |
testcase_23 | AC | 1 ms
5,376 KB |
testcase_24 | AC | 6 ms
5,376 KB |
testcase_25 | AC | 2 ms
5,376 KB |
testcase_26 | AC | 1 ms
5,376 KB |
testcase_27 | AC | 1 ms
5,376 KB |
コンパイルメッセージ
warning: field `n` is never read --> src/main.rs:4:5 | 3 | struct UnionFind { | --------- field in this struct 4 | n: usize, | ^ | = note: `#[warn(dead_code)]` on by default warning: method `chain_cnt` is never used --> src/main.rs:47:8 | 10 | impl UnionFind { | -------------- method in this implementation ... 47 | fn chain_cnt(&mut self, i: usize) -> usize { | ^^^^^^^^^
ソースコード
const INF: usize = 1usize << 60; struct UnionFind { n: usize, parents: Vec<usize>, depths: Vec<usize>, chains: Vec<usize>, } impl UnionFind { fn new(n: usize) -> Self { UnionFind { n: n, parents: (0..n).collect(), depths: vec![0; n], chains: vec![1; n], } } fn equiv(&mut self, a: usize, b: usize) -> bool { self.find(a) == self.find(b) } fn unite(&mut self, a: usize, b: usize) { if self.equiv(a, b) { return; } let x = self.parents[a]; let y = self.parents[b]; if self.depths[x] > self.depths[y] { self.parents[x] = self.parents[y]; self.chains[y] += self.chains[x]; } else { self.parents[y] = self.parents[x]; self.chains[x] += self.chains[y]; if self.depths[x] == self.depths[y] { self.depths[x] += 1; } } } fn find(&mut self, a: usize) -> usize { if self.parents[a] == a { return a; } let p = self.find(self.parents[a]); self.parents[a] = p; p } fn chain_cnt(&mut self, i: usize) -> usize { let idx = self.find(i); self.chains[idx] } } fn combinations(n: usize, r: usize) -> Vec<Vec<usize>> { fn dfs(cur: usize, limit: usize, size_limit: usize, stack: &mut Vec<usize>, result: &mut Vec<Vec<usize>>) { if cur == limit { result.push(stack.clone().to_owned()); return; } if limit - cur > size_limit - stack.len() { dfs(cur+1, limit, size_limit, stack, result); } if size_limit > stack.len() { stack.push(cur); dfs(cur+1, limit, size_limit, stack, result); stack.pop(); } } let mut stack = vec![]; let mut result = vec![]; dfs(0, n, r, &mut stack, &mut result); result } fn main() { let mut nmk = String::new(); std::io::stdin().read_line(&mut nmk).ok(); let nmk: Vec<usize> = nmk.trim().split_whitespace().map(|s| s.parse().unwrap()).collect(); let n = nmk[0]; let m = nmk[1]; let k = nmk[2]; let mut a = String::new(); std::io::stdin().read_line(&mut a).ok(); let a: Vec<usize> = a.trim().split_whitespace().map(|s| s.parse().unwrap()).collect(); let mut paths = vec![vec![INF; n]; n]; for _ in 0..m { let mut temp = String::new(); std::io::stdin().read_line(&mut temp).ok(); let temp: Vec<usize> = temp.trim().split_whitespace().map(|s| s.parse().unwrap()).collect(); let x = temp[0]-1; let y = temp[1]-1; let z = temp[2]; paths[x][y] = z; paths[y][x] = z; } for k in 0..n { for i in 0..n { for j in 0..n { paths[i][j] = paths[i][j].min(paths[i][k] + paths[k][j]); } } } let mut result = INF; for combs in combinations(n, k).iter() { let mut uf = UnionFind::new(k); let mut idxmap = vec![n; n]; let mut costs = 0usize; combs.iter().enumerate().for_each(|(idx, &i)| { idxmap[i] = idx; costs += a[i]; }); let mut lines = vec![]; for i in 0..k { for j in i+1..k { if idxmap[combs[i]] == n || idxmap[combs[j]] == n { continue; } lines.push((combs[i], combs[j], paths[combs[i]][combs[j]])); } } lines.sort_by_key(|&(_, _, cost)| cost); for &(u, v, cost) in lines.iter() { let u = idxmap[u]; let v = idxmap[v]; if u == n || v == n { continue; } if uf.find(u) == uf.find(v) { continue; } uf.unite(u, v); costs += cost; } result = result.min(costs); } println!("{}", result); }