結果

問題 No.748 yuki国のお財布事情
ユーザー iqueue02iqueue02
提出日時 2019-11-02 22:25:34
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 131 ms / 2,000 ms
コード長 1,511 bytes
コンパイル時間 1,718 ms
コンパイル使用メモリ 173,872 KB
実行使用メモリ 7,708 KB
最終ジャッジ日時 2023-10-13 01:38:07
合計ジャッジ時間 4,501 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,352 KB
testcase_01 AC 1 ms
4,372 KB
testcase_02 AC 1 ms
4,352 KB
testcase_03 AC 1 ms
4,356 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 1 ms
4,352 KB
testcase_06 AC 1 ms
4,348 KB
testcase_07 AC 1 ms
4,348 KB
testcase_08 AC 1 ms
4,348 KB
testcase_09 AC 2 ms
4,352 KB
testcase_10 AC 1 ms
4,352 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 1 ms
4,352 KB
testcase_13 AC 15 ms
4,352 KB
testcase_14 AC 27 ms
4,348 KB
testcase_15 AC 18 ms
4,348 KB
testcase_16 AC 51 ms
4,900 KB
testcase_17 AC 100 ms
6,320 KB
testcase_18 AC 119 ms
6,488 KB
testcase_19 AC 131 ms
6,468 KB
testcase_20 AC 117 ms
6,632 KB
testcase_21 AC 112 ms
7,708 KB
testcase_22 AC 1 ms
4,352 KB
testcase_23 AC 2 ms
4,352 KB
testcase_24 AC 2 ms
4,348 KB
testcase_25 AC 90 ms
6,856 KB
testcase_26 AC 110 ms
6,704 KB
testcase_27 AC 107 ms
6,676 KB
testcase_28 AC 89 ms
6,492 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0; i < (n);i++)
#define sz(x) int(x.size())
typedef long long ll;
typedef pair<int,int> P;

struct UnionFind{
    vector<int> data;
  /* 
    UnionFind(int sz) {
    data.assign(sz, -1);
  }*/
  
  void make(int sz) {
    data.assign(sz, -1);
  }

  bool unite(int x, int y) {
    x = find(x), y = find(y);
    if(x == y) return (false);
    if(data[x] > data[y]) swap(x, y);//親は負でサイズを保存
    data[x] += data[y];
    data[y] = x;
    return (true);
  }
 
  int find(int k) {
    if(data[k] < 0) return (k);
    return (data[k] = find(data[k]));
  }

  bool same(int x, int y){
      return find(x) == find(y);
  }
 
  int size(int k) {
    return (-data[find(k)]);
  }
};

struct Edge{ll u,v; int cost;};
using Edges = vector<Edge>;
int n, m, k;

UnionFind uf;

bool comp(const Edge &a, const Edge &b){
  return a.cost < b.cost;
}
ll res;
void kruskal(Edges &es){
  sort(es.begin(),es.end(),comp);
  for(auto e : es){
    ll cost = e.cost;
    int u = e.u,v = e.v;
    if(!uf.same(u,v)){
      uf.unite(u,v);
      res += cost;
    } 
  }
}

int main(){
  cin >> n >> m >> k;
  Edges es;
  uf.make(n);
  ll max_cost = 0;
  rep(i,m) {
    Edge e;
    cin >> e.u >> e.v >> e.cost;
    e.u--; e.v--;
    max_cost += e.cost;
    es.push_back(e);
  }
  rep(i,k) {
    int t;
    cin >> t;
    t--;
  
    uf.unite(es[t].u, es[t].v);
    res += es[t].cost;
  }
  kruskal(es);
  cout << max_cost - res << endl;
  return 0;
}
0