結果

問題 No.748 yuki国のお財布事情
ユーザー nebukuro09nebukuro09
提出日時 2018-10-19 21:43:23
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 228 ms / 2,000 ms
コード長 1,733 bytes
コンパイル時間 943 ms
コンパイル使用メモリ 125,012 KB
実行使用メモリ 27,180 KB
最終ジャッジ日時 2023-09-03 21:13:29
合計ジャッジ時間 4,385 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 26 ms
8,712 KB
testcase_14 AC 46 ms
11,904 KB
testcase_15 AC 33 ms
11,588 KB
testcase_16 AC 87 ms
13,028 KB
testcase_17 AC 197 ms
26,440 KB
testcase_18 AC 218 ms
26,384 KB
testcase_19 AC 228 ms
27,180 KB
testcase_20 AC 198 ms
26,840 KB
testcase_21 AC 207 ms
26,876 KB
testcase_22 AC 1 ms
4,380 KB
testcase_23 AC 1 ms
4,376 KB
testcase_24 AC 1 ms
4,380 KB
testcase_25 AC 168 ms
20,588 KB
testcase_26 AC 213 ms
27,020 KB
testcase_27 AC 211 ms
26,840 KB
testcase_28 AC 151 ms
20,548 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, std.bitmanip;

immutable int INF = 1 << 29;

void main() {
    auto s = readln.split.map!(to!int);
    auto N = s[0];
    auto M = s[1];
    auto K = s[2];
    auto G = new Tuple!(int, long)[][](N);
    auto E = new Tuple!(int, int, long)[](M);
    foreach (i; 0..M) {
        s = readln.split.map!(to!int);
        int u = s[0] - 1;
        int v = s[1] - 1;
        long c = s[2].to!long;
        G[u] ~= tuple(v, c);
        G[v] ~= tuple(u, c);
        E[i] = tuple(u, v, c);
    }
    auto X = K.iota.map!(_ => readln.chomp.to!int - 1).array;

    long ans = 0;
    auto uf = new UnionFind(N);

    foreach (i; X) {
        ans += E[i][2];
        uf.unite(E[i][0], E[i][1]);
    }

    auto pq = new BinaryHeap!(Array!(Tuple!(int, int, long)), "a[2] > b[2]")();
    foreach (e; E) pq.insert(e);

    while (!pq.empty) {
        auto e = pq.front;
        auto u = e[0];
        auto v = e[1];
        auto c = e[2];
        pq.removeFront;
        if (uf.find(u) == uf.find(v)) {
            continue;
        }
        ans += c;
        uf.unite(u, v);
    }

    writeln(E.map!(e => e[2]).sum - ans);
}



class UnionFind {
    int N;
    int[] table;

    this(int n) {
        N = n;
        table = new int[](N);
        fill(table, -1);
    }

    int find(int x) {
        return table[x] < 0 ? x : (table[x] = find(table[x]));
    }

    void unite(int x, int y) {
        x = find(x);
        y = find(y);
        if (x == y) return;
        if (table[x] > table[y]) swap(x, y);
        table[x] += table[y];
        table[y] = x;
    }
}
0