結果

問題 No.860 買い物
ユーザー 0w10w1
提出日時 2019-09-28 04:22:35
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 119 ms / 1,000 ms
コード長 822 bytes
コンパイル時間 2,594 ms
コンパイル使用メモリ 216,844 KB
実行使用メモリ 11,460 KB
最終ジャッジ日時 2023-10-26 01:58:55
合計ジャッジ時間 6,278 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 5 ms
4,348 KB
testcase_07 AC 118 ms
7,260 KB
testcase_08 AC 119 ms
7,260 KB
testcase_09 AC 118 ms
7,260 KB
testcase_10 AC 118 ms
7,260 KB
testcase_11 AC 81 ms
11,460 KB
testcase_12 AC 79 ms
11,460 KB
testcase_13 AC 112 ms
10,932 KB
testcase_14 AC 110 ms
7,260 KB
testcase_15 AC 59 ms
8,292 KB
testcase_16 AC 98 ms
7,260 KB
testcase_17 AC 107 ms
11,460 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

int main() {
  int N;
  cin >> N;

  vector<int> C(N);
  vector<int> D(N);
  for (int i = 0; i < N; ++i) {
    cin >> C[i] >> D[i];
  }

  vector<tuple<int, int, int>> edges;
  for (int i = 0; i < N; ++i) {
    edges.emplace_back(C[i], N, i);
  }

  for (int i = 1; i < N; ++i) {
    edges.emplace_back(D[i], i - 1, i);
  }

  sort(edges.begin(), edges.end());

  vector<int> fa(N + 1);
  iota(fa.begin(), fa.end(), 0);
  function<int(int)> find = [&](int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); };

  int64_t ans = accumulate(C.begin(), C.end(), 0LL);
  for (auto t : edges) {
    int w, u, v;
    tie(w, u, v) = t;
    int fu = find(u);
    int fv = find(v);
    if (fu != fv) {
      fa[fu] = fv;
      ans += w;
    }
  }

  cout << ans << endl;

  return 0;
}
0