結果

問題 No.860 買い物
ユーザー ganariyaganariya
提出日時 2019-08-12 17:01:08
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 117 ms / 1,000 ms
コード長 2,659 bytes
コンパイル時間 1,581 ms
コンパイル使用メモリ 161,844 KB
実行使用メモリ 13,264 KB
最終ジャッジ日時 2023-10-14 22:04:36
合計ジャッジ時間 4,464 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,352 KB
testcase_01 AC 1 ms
4,352 KB
testcase_02 AC 2 ms
4,352 KB
testcase_03 AC 1 ms
4,348 KB
testcase_04 AC 1 ms
4,352 KB
testcase_05 AC 1 ms
4,348 KB
testcase_06 AC 5 ms
4,352 KB
testcase_07 AC 104 ms
12,084 KB
testcase_08 AC 106 ms
12,148 KB
testcase_09 AC 106 ms
11,968 KB
testcase_10 AC 103 ms
13,232 KB
testcase_11 AC 72 ms
11,880 KB
testcase_12 AC 70 ms
12,480 KB
testcase_13 AC 102 ms
12,632 KB
testcase_14 AC 104 ms
13,264 KB
testcase_15 AC 55 ms
12,028 KB
testcase_16 AC 86 ms
12,624 KB
testcase_17 AC 117 ms
12,352 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//include
//------------------------------------------
#include <vector>
#include <list>
#include <map>
#include <unordered_map>
#include <climits>
#include <set>
#include <unordered_set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <queue>
#include <random>
#include <complex>
#include <regex>
#include <locale>
#include <random>
#include <type_traits>

using namespace std;

#define SHOW_VECTOR(v) {std::cerr << #v << "\t:";for(const auto& xxx : v){std::cerr << xxx << " ";}std::cerr << "\n";}
#define SHOW_MAP(v){std::cerr << #v << endl; for(const auto& xxx: v){std::cerr << xxx.first << " " << xxx.second << "\n";}}

using LL = long long;

//------------------------------------------
//------------------------------------------

struct UnionFind {

    vector<int> par;
    vector<int> sizes;

    UnionFind(int n)
            : par(n), sizes(n, 1) {
        for (int i = 0; i < n; i++) {
            par[i] = i;
        }
    }

    int find(int x) {
        return x == par[x] ? x : par[x] = find(par[x]);
    }

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

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

    int get_size(int x) {
        return sizes[find(x)];
    }

    bool all_same() {
        bool good = true;
        for (int i = 0, n = par.size(); i < n; i++) if (find(0) != find(i)) good = false;
        return good;
    }

    int get_connectivity() {
        set<int> s;
        for (int i = 0, n = par.size(); i < n; i++) s.insert(find(i));
        return s.size();
    }

};

int main() {

    int N;
    cin >> N;

    UnionFind UF(N + 1);

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

    vector<tuple<LL, LL, LL>> edges;
    for (int i = 0; i < N; i++) {
        edges.push_back(make_tuple(C[i], i, N));
    }
    for (int i = 1; i < N; i++) {
        edges.push_back(make_tuple(D[i], i - 1, i));
    }
    sort(edges.begin(), edges.end());

    LL ans = accumulate(C.begin(), C.end(), 0LL);

    for (auto &e : edges) {
        LL cost, fm, to;
        tie(cost, fm, to) = e;
        if (!UF.same(fm, to)) {
            UF.unite(fm, to);
            ans += cost;
        }
    }

    cout << ans << endl;

}























0