結果

問題 No.748 yuki国のお財布事情
ユーザー beetbeet
提出日時 2018-10-19 22:14:08
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 133 ms / 2,000 ms
コード長 1,732 bytes
コンパイル時間 2,895 ms
コンパイル使用メモリ 211,256 KB
実行使用メモリ 13,212 KB
最終ジャッジ日時 2023-08-12 01:13:04
合計ジャッジ時間 5,467 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 16 ms
4,376 KB
testcase_14 AC 28 ms
5,276 KB
testcase_15 AC 19 ms
4,912 KB
testcase_16 AC 51 ms
7,524 KB
testcase_17 AC 102 ms
12,244 KB
testcase_18 AC 122 ms
12,580 KB
testcase_19 AC 133 ms
13,212 KB
testcase_20 AC 121 ms
12,416 KB
testcase_21 AC 117 ms
12,804 KB
testcase_22 AC 2 ms
4,376 KB
testcase_23 AC 1 ms
4,380 KB
testcase_24 AC 1 ms
4,380 KB
testcase_25 AC 96 ms
12,756 KB
testcase_26 AC 115 ms
12,752 KB
testcase_27 AC 111 ms
11,868 KB
testcase_28 AC 95 ms
12,100 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using Int = long long;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}


template<typename T>
struct Kruskal{
  
  struct edge{
    Int from,to;
    T cost;
    Int used;
    edge(){}
    edge(Int from,Int to,T cost):
      from(from),to(to),cost(cost),used(0){}
    bool operator<(const edge& e) const{
      return cost<e.cost;
    }
  };

  Int n;
  vector<Int> p,r;
  vector<edge> edges;

  Kruskal(){}
  Kruskal(Int n):n(n){}

  void init(Int n){
    r.assign(n,1);
    p.resize(n);
    iota(p.begin(),p.end(),0);
  }
  
  Int find(Int x){
    return (x==p[x]?x:p[x]=find(p[x]));
  }

  bool same(Int x,Int y){
    return find(x)==find(y);
  }

  void unite(Int x,Int y){
    x=find(x);y=find(y);
    if(x==y) return;
    if(r[x]<r[y]) swap(x,y);
    r[x]+=r[y];
    p[y]=x;
  }

  void add_edge(Int u,Int v,T c){
    edges.emplace_back(u,v,c);
  }
  
  Int build(){
    sort(edges.begin(),edges.end());
    init(n);
    Int res=0;
    for(auto &e:edges){
      if(!e.cost.first||!same(e.from,e.to)){
	res+=e.cost.second;
	unite(e.from,e.to);
	e.used=1;
      }
    }
    return res;
  }
};

//INSERT ABOVE HERE
signed main(){
  Int n,m,k;
  cin>>n>>m>>k;
  vector<Int> a(m),b(m),c(m);
  Int sum=0;
  for(Int i=0;i<m;i++){
    cin>>a[i]>>b[i]>>c[i];
    a[i]--;b[i]--;
    sum+=c[i];
  }
  vector<Int> e(k);
  for(Int i=0;i<k;i++) cin>>e[i];
  vector<Int> u(m);
  for(Int i=0;i<k;i++) u[--e[i]]=1;

  using P = pair<Int, Int>;
  Kruskal<P> G(n);  
  for(Int i=0;i<m;i++)
    G.add_edge(a[i],b[i],P(!u[i],c[i]));

  Int res=G.build();
  
  cout<<sum-res<<endl;
  return 0;
}
0