結果

問題 No.748 yuki国のお財布事情
ユーザー xxxasdfghjkxxxasdfghjk
提出日時 2018-10-22 15:13:23
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 72 ms / 2,000 ms
コード長 1,709 bytes
コンパイル時間 817 ms
コンパイル使用メモリ 72,140 KB
実行使用メモリ 6,680 KB
最終ジャッジ日時 2023-08-12 05:58:57
合計ジャッジ時間 2,783 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 8 ms
4,376 KB
testcase_14 AC 13 ms
4,380 KB
testcase_15 AC 9 ms
4,380 KB
testcase_16 AC 26 ms
4,464 KB
testcase_17 AC 45 ms
5,764 KB
testcase_18 AC 59 ms
6,484 KB
testcase_19 AC 72 ms
6,492 KB
testcase_20 AC 65 ms
6,280 KB
testcase_21 AC 57 ms
6,376 KB
testcase_22 AC 1 ms
4,380 KB
testcase_23 AC 1 ms
4,380 KB
testcase_24 AC 1 ms
4,376 KB
testcase_25 AC 40 ms
5,708 KB
testcase_26 AC 51 ms
6,680 KB
testcase_27 AC 47 ms
6,580 KB
testcase_28 AC 49 ms
5,700 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<cstdio>
#include<queue>
#include<utility>
#include<cstring>
#include<stack>
#include<algorithm>
#include<cmath>
#include<iostream>
#define MAX_N 100001
#define INF 2147483647
#define REP(i,n) for(int i=0;i<(int)(n);i++)
void init(int n);
int find(int n);
void unite(int x,int y);
bool same(int x, int y);
int dx[4] = {1,0,0,-1};
int dy[4] = {0,1,-1,0};
using namespace std;
typedef long long ll;
struct edge{
  ll from,to,cost;
};
typedef vector<vector<edge> > AdjList;
AdjList graph;
typedef pair<int , int> P;
bool comp(const edge& e1, const edge& e2){
  return e1.cost < e2.cost;
}
int E,V;
edge es[100001];
ll kruskal(){
  sort(es,es+E,comp);
  ll res = 0;
  for(int i=0;i<E;i++){
    edge e = es[i];
    if(!same(e.to,e.from)){
    
      unite(e.to,e.from);
      res += e.cost;
    }
  }
  return res;
}


int main()
{
  ll N,M,K,r,t,f,count,merge=0,sum=0;
  scanf("%lld %lld %lld",&N,&M,&K);
  E = M;
  V = N;
  init(V);
  REP(i,M){
    scanf("%lld %lld %lld",&f,&t,&r);
    sum += r;
    es[i] = (edge){--f,--t,r};
  }
  REP(i,K){
    cin >> count;
    merge += es[count-1].cost;
    unite(es[count-1].from,es[count-1].to);
  }
  cout << (sum-(kruskal() + merge)) << endl;
  return 0;
}

int par[MAX_N];
int ranks[MAX_N];

//n要素で初期化
void init(int n){
  REP(i,n){
    par[i] = i;
    ranks[i] = 0;
  }

}

//木の根を求める
int find(int x){
  if(par[x] == x){
    return x;
  }else{
    return par[x] = find(par[x]);
  }
}

void unite(int x,int y){
  x = find(x);
  y = find(y);
  if(x == y) return ;
  if(ranks[x] < ranks[y]){
    par[x] = y;
  }else{
    par[y] = x;
    if(ranks[x] == ranks[y]) ranks[x]++;
  }
}

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

0