結果

問題 No.1690 Power Grid
ユーザー phsplsphspls
提出日時 2022-11-16 00:05:01
言語 Rust
(1.77.0)
結果
WA  
実行時間 -
コード長 3,135 bytes
コンパイル時間 4,132 ms
コンパイル使用メモリ 169,112 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-16 22:34:46
合計ジャッジ時間 5,951 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 1 ms
4,348 KB
testcase_04 AC 1 ms
4,348 KB
testcase_05 AC 1 ms
4,348 KB
testcase_06 AC 52 ms
4,348 KB
testcase_07 AC 52 ms
4,348 KB
testcase_08 AC 49 ms
4,348 KB
testcase_09 AC 49 ms
4,348 KB
testcase_10 AC 1 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 1 ms
4,348 KB
testcase_13 AC 1 ms
4,348 KB
testcase_14 AC 11 ms
4,348 KB
testcase_15 AC 2 ms
4,348 KB
testcase_16 AC 88 ms
4,348 KB
testcase_17 AC 51 ms
4,348 KB
testcase_18 AC 21 ms
4,348 KB
testcase_19 AC 89 ms
4,348 KB
testcase_20 AC 91 ms
4,348 KB
testcase_21 AC 1 ms
4,348 KB
testcase_22 WA -
testcase_23 AC 1 ms
4,348 KB
testcase_24 AC 6 ms
4,348 KB
testcase_25 AC 3 ms
4,348 KB
testcase_26 AC 2 ms
4,348 KB
testcase_27 AC 1 ms
4,348 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: field `n` is never read
 --> 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
  --> Main.rs:48:8
   |
11 | impl UnionFind {
   | -------------- method in this implementation
...
48 |     fn chain_cnt(&mut self, i: usize) -> usize {
   |        ^^^^^^^^^

warning: 2 warnings emitted

ソースコード

diff #

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 i in 0..n {
        for j in 0..n {
            for k in 0..n {
                paths[i][j] = paths[i][j].min(paths[i][k] + paths[k][j]);
            }
        }
    }
    let mut result = INF;
    for i in 0..1usize<<n {
        if i.count_ones() as usize != k { continue; }
        let targets = (0..n).filter(|&j| ((i >> j) & 1) == 1).collect::<Vec<usize>>();
        let limit = targets.len();
        let mut lines = Vec::with_capacity(k*(k-1) / 2);
        for j in 0..limit {
            for k in j+1..limit {
                lines.push((targets[j], targets[k], paths[targets[j]][targets[k]]));
            }
        }
        lines.sort_by_key(|&(_, _, c)| c);
        let mut uf = UnionFind::new(n);
        let mut costs = targets.iter().map(|&j| a[j]).sum::<usize>();
        for &(l, r, cost) in lines.iter() {
            let pl = uf.find(l);
            let pr = uf.find(r);
            if pl == pr { continue; }
            uf.unite(pl, pr);
            costs += cost;
        }
        result = result.min(costs);
    }
    println!("{}", result);
}
0