結果

問題 No.860 買い物
ユーザー okok
提出日時 2019-08-10 14:38:40
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,217 bytes
コンパイル時間 785 ms
コンパイル使用メモリ 69,788 KB
実行使用メモリ 7,676 KB
最終ジャッジ日時 2023-09-26 23:22:02
合計ジャッジ時間 2,967 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 5 ms
4,380 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 AC 79 ms
6,448 KB
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 56 ms
6,512 KB
testcase_16 WA -
testcase_17 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

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

#define MAX 110000

int per[MAX], siz[MAX];

void init(int n){
  for(int i = 0; i < n; i++){
    per[i] = i;
    siz[i] = 1;
  }
}

int find(int x){

  if(x == per[x]) return x;
  else return per[x] = find(per[x]);
}

void unite(int x, int y){
  x = find(x);
  y = find(y);

  if(x == y) return;

  if(siz[x] > siz[y]){
    siz[x] += siz[y];
    per[y] = x;
  } else {
    siz[y] += siz[x];
    per[x] = y;
  }
}

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


int kruskal(vector<pair<int,pair<int,int> > > &edge){
  int res = 0;
  
  sort(edge.begin(), edge.end());

  for(int i = 0; i < edge.size(); i++){
    if(same(edge[i].second.first, edge[i].second.second)) continue;

    unite(edge[i].second.first, edge[i].second.second);

    res += edge[i].first;
  }

  return res;
}

signed main(){

  int N, sum = 0;
  int C, D;
  vector<pair<int,pair<int,int> > > edge;

  cin>>N;

  for(int i = 1; i <= N; i++){
    cin>>C>>D;
    sum += C;
    edge.push_back(make_pair(C ,make_pair(0,i)));
    if(i != 1) edge.push_back(make_pair(D ,make_pair(i-1,i)));
  }

  init(N+2);
  
  cout<<sum + kruskal(edge)<<endl;
  
  return 0;
}
0