結果

問題 No.748 yuki国のお財布事情
ユーザー potoooooooopotoooooooo
提出日時 2018-12-16 21:33:20
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 126 ms / 2,000 ms
コード長 1,572 bytes
コンパイル時間 3,046 ms
コンパイル使用メモリ 179,184 KB
実行使用メモリ 7,232 KB
最終ジャッジ日時 2023-10-25 22:21:34
合計ジャッジ時間 6,589 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 1 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 15 ms
4,348 KB
testcase_14 AC 27 ms
4,348 KB
testcase_15 AC 18 ms
4,348 KB
testcase_16 AC 50 ms
4,592 KB
testcase_17 AC 99 ms
6,440 KB
testcase_18 AC 111 ms
6,704 KB
testcase_19 AC 126 ms
7,232 KB
testcase_20 AC 113 ms
6,704 KB
testcase_21 AC 110 ms
6,968 KB
testcase_22 AC 2 ms
4,348 KB
testcase_23 AC 2 ms
4,348 KB
testcase_24 AC 2 ms
4,348 KB
testcase_25 AC 87 ms
6,440 KB
testcase_26 AC 107 ms
7,232 KB
testcase_27 AC 107 ms
7,232 KB
testcase_28 AC 91 ms
5,912 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

// one-based numbering
struct union_find {
    vector<int> data;
    // i: (data[i] < 0) -> group size, (data[i] > 0) -> parent;
    union_find(int n) {
      data.resize(n+1, -1);
    }
    int find(int x) {
        if(data[x] < 0) return x;
        else return data[x] = find(data[x]);
    }
    int size(int x) {
      return -data[find(x)];
    }
    void unite(int x, int y) {
        x = find(x); y = find(y);
        if(x == y) return;
        if (data[x] > data[y]) swap(x, y);
        data[x] += data[y];
        data[y] = x;
    }
    bool same(int x, int y) {
        x = find(x); y = find(y);
        return x == y;
    }
};

template<class Numeric> struct edge {
  int from, to; Numeric cost;
  bool operator< (struct edge &e) {
    return cost < e.cost;
  }
};

template<class Numeric> Numeric kruskal(int n, vector< edge<Numeric> > v, union_find u) {
  sort(v.begin(), v.end());
  Numeric mincost = 0;
  for (int i = 0; i < v.size(); i++) {
    if (!u.same(v[i].from, v[i].to)){
      mincost += v[i].cost;
      u.unite(v[i].from, v[i].to);
    }
  }
  return mincost;
}

int main(){
    int n, m, k; cin >> n >> m >> k;
    vector< edge<long long> > v(m);
    long long ans = 0;
    for (int i = 0; i < m; ++i) {
        cin >> v[i].from >> v[i].to >> v[i].cost;
        ans += v[i].cost;
    }
    union_find u(n);
    for (int i = 0; i < k; ++i) {
        int c; cin >> c; c--;
        u.unite(v[c].from, v[c].to);
        ans -= v[c].cost;
    }
    cout << ans - kruskal(n, v, u) << endl;
    return 0;
}
0