結果

問題 No.748 yuki国のお財布事情
ユーザー 37zigen37zigen
提出日時 2021-03-17 04:41:28
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 136 ms / 2,000 ms
コード長 1,445 bytes
コンパイル時間 1,268 ms
コンパイル使用メモリ 87,272 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-26 13:31:23
合計ジャッジ時間 3,962 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 1 ms
6,940 KB
testcase_06 AC 1 ms
6,940 KB
testcase_07 AC 1 ms
6,940 KB
testcase_08 AC 2 ms
6,944 KB
testcase_09 AC 2 ms
6,944 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 2 ms
6,940 KB
testcase_12 AC 2 ms
6,944 KB
testcase_13 AC 16 ms
6,940 KB
testcase_14 AC 28 ms
6,940 KB
testcase_15 AC 19 ms
6,940 KB
testcase_16 AC 51 ms
6,940 KB
testcase_17 AC 104 ms
6,944 KB
testcase_18 AC 121 ms
6,940 KB
testcase_19 AC 136 ms
6,940 KB
testcase_20 AC 123 ms
6,944 KB
testcase_21 AC 116 ms
6,940 KB
testcase_22 AC 2 ms
6,944 KB
testcase_23 AC 2 ms
6,940 KB
testcase_24 AC 2 ms
6,944 KB
testcase_25 AC 94 ms
6,944 KB
testcase_26 AC 114 ms
6,940 KB
testcase_27 AC 109 ms
6,940 KB
testcase_28 AC 92 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

struct UnionFind {
  vector<int> parent;
  
  UnionFind(int n) {
    parent.assign(n, -1);
  }
  
  int root(int x) {
    return parent[x] < 0 ? x : (parent[x] = root(parent[x]));
  }
  
  void unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y) return;
    if (parent[x] > parent[y]) swap(x, y);
    parent[x] += parent[y];
    parent[y] = x;
  }
  
  bool equiv(int x, int y) {
    return root(x) == root(y);
  }
};

struct Edge {
  int u, v;
  long cost;
  bool operator < (Edge const& o) {
    return cost < o.cost;
  }
};

long kruskal(int N, vector<Edge> &edges, UnionFind uf) {
  vector<Edge> mintree_edges; 
  //UnionFind uf(N);
  long cost=0;
  sort(edges.begin(), edges.end());
  for (Edge &e : edges) {
    if (uf.equiv(e.u, e.v)) continue;
    uf.unite(e.u, e.v);
    //mintree_edges.push_back(e);
    cost+=e.cost;
  }
  return cost;
  //return mintree_edges;
}

int main() {
  int N, M, K;
  cin >> N >> M >> K;
  vector<Edge> edges;
  long ans=0;
  for (int i=0;i<M;++i) {
    int a, b;
    long c;
    cin >> a >> b >> c;
    --a;--b;
    Edge e;
    e.u=a;
    e.v=b;
    e.cost=c;
    edges.push_back(e);
    ans += c;
  }
  UnionFind uf(N);
  for (int i=0;i<K;++i) {
    int e;
    cin >> e;
    --e;
    uf.unite(edges[e].u, edges[e].v);
    ans -= edges[e].cost;
  }
  ans -= kruskal(N, edges, uf);
  cout << ans << endl;
}
0