結果

問題 No.748 yuki国のお財布事情
ユーザー nanaenanae
提出日時 2018-07-27 18:01:04
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 103 ms / 2,000 ms
コード長 2,494 bytes
コンパイル時間 690 ms
コンパイル使用メモリ 104,720 KB
実行使用メモリ 6,696 KB
最終ジャッジ日時 2023-09-03 20:49:43
合計ジャッジ時間 2,979 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,384 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 11 ms
4,376 KB
testcase_14 AC 20 ms
4,380 KB
testcase_15 AC 13 ms
4,376 KB
testcase_16 AC 38 ms
4,504 KB
testcase_17 AC 70 ms
4,900 KB
testcase_18 AC 88 ms
6,696 KB
testcase_19 AC 103 ms
5,232 KB
testcase_20 AC 92 ms
5,640 KB
testcase_21 AC 80 ms
6,172 KB
testcase_22 AC 1 ms
4,380 KB
testcase_23 AC 1 ms
4,380 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 64 ms
4,648 KB
testcase_26 AC 77 ms
5,332 KB
testcase_27 AC 73 ms
6,232 KB
testcase_28 AC 69 ms
4,636 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.d(63): Deprecation: usage of the `body` keyword is deprecated. Use `do` instead.
Main.d(76): Deprecation: usage of the `body` keyword is deprecated. Use `do` instead.
Main.d(89): Deprecation: usage of the `body` keyword is deprecated. Use `do` instead.
Main.d(98): Deprecation: usage of the `body` keyword is deprecated. Use `do` instead.

ソースコード

diff #

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

alias Edge = Tuple!(int, "a", int, "b", int, "cost");

void main() {
    int n, m, k;
    scan(n, m, k);

    auto es = new Edge[](m);

    long ans;

    foreach (i ; 0 .. m) {
        int ai, bi, ci;
        scan(ai, bi, ci);
        es[i] = Edge(ai-1, bi-1, ci);
        ans += ci;
    }

    auto uf = UnionFind(n);

    foreach (i ; 0 .. k) {
        int pi;
        scan(pi);
        pi--;
        ans -= es[pi].cost;
        uf.merge(es[pi].a, es[pi].b);
    }

    es.sort!"a.cost < b.cost"();

    foreach (i ; 0 .. m) {
        if (uf.numcc == 1) {
            break;
        }

        if (!uf.same(es[i].a, es[i].b)) {
            uf.merge(es[i].a, es[i].b);
            ans -= es[i].cost;
        }
    }

    writeln(ans);
}





struct UnionFind {
    private {
        int _size;
        int[] _parent;
        int _numcc;
    }

    this(int N) 
    in {
        assert(N > 0);
    }
    body {
        _size = N;
        _numcc = N;
        _parent = new int[](_size);
        foreach (i ; 0 .. _size) {
            _parent[i] = i;
        }
    }

    int findRoot(int x)
    in {
        assert(0 <= x && x < _size);
    }
    body {
        if (_parent[x] != x) {
            _parent[x] = findRoot(_parent[x]);
        }

        return _parent[x];
    }

    bool same(int x, int y)
    in {
        assert(0 <= x && x < _size);
        assert(0 <= y && y < _size);
    }
    body {
        return findRoot(x) == findRoot(y);
    }

    void merge(int x, int y)
    in {
        assert(0 <= x && x < _size);
        assert(0 <= y && y < _size);
    }
    body {
        int u = findRoot(x);
        int v = findRoot(y);

        if (u == v) return;

        _parent[v] = u;
        _numcc--;
    }

    int numcc() {
        return _numcc;
    }
}



































void scan(T...)(ref T args) {
    import std.stdio : readln;
    import std.algorithm : splitter;
    import std.conv : to;
    import std.range.primitives;

    auto line = readln().splitter();
    foreach (ref arg; args) {
        arg = line.front.to!(typeof(arg));
        line.popFront();
    }
    assert(line.empty);
}



void fillAll(R, T)(ref R arr, T value) {
    static if (is(typeof(arr[] = value))) {
        arr[] = value;
    }
    else {
        foreach (ref e; arr) {
            fillAll(e, value);
        }
    }
}
0