結果
| 問題 |
No.1690 Power Grid
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2022-11-02 00:54:24 |
| 言語 | Rust (1.83.0 + proconio) |
| 結果 |
AC
|
| 実行時間 | 114 ms / 3,000 ms |
| コード長 | 3,009 bytes |
| コンパイル時間 | 15,441 ms |
| コンパイル使用メモリ | 383,672 KB |
| 実行使用メモリ | 6,944 KB |
| 最終ジャッジ日時 | 2024-07-16 12:11:23 |
| 合計ジャッジ時間 | 16,041 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 25 |
コンパイルメッセージ
warning: unused variable: `l`
--> src/main.rs:95:30
|
95 | lines.sort_by_key(|&(l, r, c)| c);
| ^ help: if this is intentional, prefix it with an underscore: `_l`
|
= note: `#[warn(unused_variables)]` on by default
warning: unused variable: `r`
--> src/main.rs:95:33
|
95 | lines.sort_by_key(|&(l, r, c)| c);
| ^ help: if this is intentional, prefix it with an underscore: `_r`
warning: field `n` is never read
--> src/main.rs:5:5
|
4 | struct UnionFind {
| --------- field in this struct
5 | n: usize,
| ^
|
= note: `#[warn(dead_code)]` on by default
warning: method `chain_cnt` is never used
--> src/main.rs:48:8
|
11 | impl UnionFind {
| -------------- method in this implementation
...
48 | 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 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 xyz = String::new();
std::io::stdin().read_line(&mut xyz).ok();
let xyz: Vec<usize> = xyz.trim().split_whitespace().map(|s| s.parse().unwrap()).collect();
let x = xyz[0]-1;
let y = xyz[1]-1;
let z = xyz[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 i in 1..1usize << n {
if i.count_ones() as usize != k { continue; }
let mut cost = (0..n).filter(|&j| ((i>>j) & 1) == 1).map(|j| a[j]).sum::<usize>();
let mut lines = vec![];
for l in 0..n {
if ((i >> l) & 1) == 0 { continue; }
for r in l+1..n {
if ((i >> r) & 1) == 0 { continue; }
lines.push((l, r, paths[l][r]));
}
}
lines.sort_by_key(|&(l, r, c)| c);
let mut uf = UnionFind::new(n);
for &(l, r, c) in lines.iter() {
if uf.find(l) == uf.find(r) { continue; }
uf.unite(l, r);
cost += c;
}
result = result.min(cost);
}
println!("{}", result);
}