結果

問題 No.860 買い物
ユーザー nmnmnmnmnmnmnmnmnmnmnmnmnmnm
提出日時 2018-05-01 23:04:20
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 110 ms / 1,000 ms
コード長 2,082 bytes
コンパイル時間 1,072 ms
コンパイル使用メモリ 106,748 KB
実行使用メモリ 13,116 KB
最終ジャッジ日時 2023-09-10 08:48:15
合計ジャッジ時間 4,099 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 5 ms
4,380 KB
testcase_07 AC 109 ms
12,720 KB
testcase_08 AC 109 ms
12,612 KB
testcase_09 AC 110 ms
11,824 KB
testcase_10 AC 109 ms
11,932 KB
testcase_11 AC 77 ms
12,292 KB
testcase_12 AC 76 ms
12,168 KB
testcase_13 AC 107 ms
12,480 KB
testcase_14 AC 108 ms
11,756 KB
testcase_15 AC 60 ms
12,516 KB
testcase_16 AC 89 ms
11,940 KB
testcase_17 AC 103 ms
13,116 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#include <iomanip>
using namespace std;

typedef long long ll;

#define sz size()
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(c) (c).begin(), (c).end()
#define rep(i,a,b) for(ll i=(a);i<(b);++i)
#define per(i,a,b) for(ll i=(b-1);i>=(a);--i)
#define clr(a, b) memset((a), (b) ,sizeof(a))
#define ctos(c) string(1,c)
#define print(x) cout<<#x<<" = "<<x<<endl;

#define MOD 1000000007

vector<int> uf, usz;
int nc;
  
void init(int n){
  vector<int> uf_(n);
  vector<int> usz_(n, 1);
  uf = uf_;
  usz = usz_;
  nc = n;

  for(int i = 0; i < uf.size(); i++){
    uf[i] = i;
  }
}
  
int find(int a){
  return (uf[a] == a) ? a : uf[a] = find(uf[a]);
}
  
void union_(int a, int b){
  a = find(a);
  b = find(b);
  
  if(a != b){
    if(usz[a] >= usz[b]){
      swap(a, b);
    }
    uf[a] = b;
    usz[b] += usz[a];
    nc--;
  }
}
  
int check(int a, int b){
  return (find(a) == find(b)) ? 1 : 0;
}
  
int get_size(int a){
  return usz[find(a)];
}

long long minST(vector<pair<long long,pair<long long, long long> > > &v, long long V){
  init(V);
  long long ret = 0;
  sort(v.begin(),v.end());
  for(long long i = 0; i < v.sz; i++){
    long long from = v[i].se.fi;
    long long to = v[i].se.se;
    long long cost = v[i].fi;
    if(check(from,to)==0){
      union_(from,to);
      ret += cost;
    }
  }
  return ret;
}

int main() {
  ll n;
  cin>>n;
  vector<ll> vc,vd;
  ll ans = 0;
  rep(i,0,n){
    ll a,b;
    cin>>a>>b;
    ans += a;
    vc.pb(a);
    vd.pb(b);
  }
  vector<pair<long long,pair<long long, long long> > > v;
  rep(i,0,vc.sz){
    v.pb(mp(vc[i],mp(0,i+1)));
  }
  rep(i,1,vd.sz){
    v.pb(mp(vd[i],mp(i,i+1)));
  }
  printf("%lld\n", ans+minST(v,n+1));
  return 0;
}
0