結果

問題 No.748 yuki国のお財布事情
ユーザー firiexpfiriexp
提出日時 2018-10-19 22:01:10
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 45 ms / 2,000 ms
コード長 1,875 bytes
コンパイル時間 943 ms
コンパイル使用メモリ 91,788 KB
実行使用メモリ 6,328 KB
最終ジャッジ日時 2023-08-15 23:43:54
合計ジャッジ時間 4,152 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 7 ms
4,380 KB
testcase_14 AC 11 ms
4,380 KB
testcase_15 AC 8 ms
4,380 KB
testcase_16 AC 19 ms
4,460 KB
testcase_17 AC 39 ms
6,016 KB
testcase_18 AC 44 ms
6,000 KB
testcase_19 AC 45 ms
6,324 KB
testcase_20 AC 41 ms
5,896 KB
testcase_21 AC 42 ms
6,272 KB
testcase_22 AC 2 ms
4,376 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 1 ms
4,380 KB
testcase_25 AC 38 ms
5,800 KB
testcase_26 AC 44 ms
6,328 KB
testcase_27 AC 43 ms
6,316 KB
testcase_28 AC 32 ms
5,268 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <limits>
#include <iostream>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <map>
#include <queue>

static const int MOD = 1000000007;
using ll = long long;
using u32 = unsigned;
using namespace std;

template<class T>
constexpr T INF = ::numeric_limits<T>::max() / 32 * 15 + 208;

template <typename T>
struct edge {
    int from, to;
    T cost;

    edge(int to, T cost) : from(-1), to(to), cost(cost) {}
    edge(int from, int to, T cost) : from(from), to(to), cost(cost) {}

    explicit operator int() const {return to;}
};

class UnionFind {
    vector<int> uni;
    int n;
public:
    explicit UnionFind(int n) : uni(static_cast<u32>(n), -1) , n(n){};

    int root(int a){
        if (uni[a] < 0) return a;
        else return (uni[a] = root(uni[a]));
    }

    bool unite(int a, int b) {
        a = root(a);
        b = root(b);
        if(a == b) return false;
        if(uni[a] > uni[b]) swap(a, b);
        uni[a] += uni[b];
        uni[b] = a;
        return true;
    }
};

template< typename T >
T kruskal(vector<edge<T>> &G, int V)
{
    sort(begin(G), end(G), [](const edge< T > &a, const edge< T > &b) { return (a.cost < b.cost); });
    UnionFind tree(V);
    T ret = 0;
    for(auto &e : G) {
        if(tree.unite(e.from, e.to)) ret += e.cost;
    }
    return (ret);
}

int main() {
    int n, m, k;
    cin >> n >> m >> k;
    vector<int> va(m), vb(m), vc(m);
    for (int i = 0; i < m; ++i) {
        scanf("%d%d%d", &va[i], &vb[i], &vc[i]);
    }
    for (int i = 0; i < k; ++i) {
        int e;
        scanf("%d", &e);
        vc[e-1] = 0;
    }
    ll sum = 0;
    for (int i = 0; i < m; ++i) {
        sum += vc[i];
    }
    vector<edge<ll>> v;
    v.reserve(m);
    for (int i = 0; i < m; ++i) {
        v.emplace_back(va[i]-1, vb[i]-1, (ll)vc[i]);
    }
    cout << sum - kruskal(v, n) << "\n";
    return 0;
}
0