結果

問題 No.748 yuki国のお財布事情
ユーザー 37zigen
提出日時 2021-03-17 04:41:28
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 165 ms / 2,000 ms
コード長 1,445 bytes
コンパイル時間 909 ms
コンパイル使用メモリ 82,976 KB
最終ジャッジ日時 2025-01-19 17:51:26
ジャッジサーバーID
(参考情報)
judge2 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

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