結果

問題 No.860 買い物
ユーザー face4face4
提出日時 2019-08-09 23:43:19
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 128 ms / 1,000 ms
コード長 1,109 bytes
コンパイル時間 894 ms
コンパイル使用メモリ 85,020 KB
実行使用メモリ 9,256 KB
最終ジャッジ日時 2023-09-26 22:10:58
合計ジャッジ時間 3,211 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 5 ms
4,376 KB
testcase_07 AC 127 ms
8,192 KB
testcase_08 AC 128 ms
8,208 KB
testcase_09 AC 128 ms
9,120 KB
testcase_10 AC 128 ms
9,256 KB
testcase_11 AC 92 ms
8,632 KB
testcase_12 AC 89 ms
8,448 KB
testcase_13 AC 119 ms
8,500 KB
testcase_14 AC 120 ms
8,444 KB
testcase_15 AC 73 ms
8,268 KB
testcase_16 AC 109 ms
8,196 KB
testcase_17 AC 117 ms
8,756 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: 関数 ‘int main()’ 内:
main.cpp:40:26: 警告: narrowing conversion of ‘- d.std::vector<long long int>::operator[](((std::vector<long long int>::size_type)i))’ from ‘__gnu_cxx::__alloc_traits<std::allocator<long long int>, long long int>::value_type’ {aka ‘long long int’} to ‘int’ [-Wnarrowing]
   40 |         if(i)   pq.push({-d[i], {i-1, i}});
main.cpp:41:18: 警告: narrowing conversion of ‘- c.std::vector<long long int>::operator[](((std::vector<long long int>::size_type)i))’ from ‘__gnu_cxx::__alloc_traits<std::allocator<long long int>, long long int>::value_type’ {aka ‘long long int’} to ‘int’ [-Wnarrowing]
   41 |         pq.push({-c[i], {n, i}});

ソースコード

diff #

#include<iostream>
#include<vector>
#include<algorithm>
#include<numeric>
#include<queue>
using namespace std;
typedef long long ll;

struct UF{
    vector<int> p;
    int n;

    UF(int siz){
        n = siz;
        p.resize(n, 0);
        for(int i = 0; i < n; i++)  p[i] = i;
    }

    int parent(int x){
        if(p[x] != x)   p[x] = parent(p[x]);
        return p[x];
    }

    void unite(int x, int y){
        x = parent(x), y = parent(y);
        p[x] = y;
    }
};

// すげえ
int main(){
    int n;
    cin >> n;
    vector<ll> c(n), d(n);
    for(int i = 0; i < n; i++)  cin >> c[i] >> d[i];
    ll ans = accumulate(c.begin(), c.end(), 0ll);
    UF uf(n+1);
    priority_queue<pair<int,pair<int,int>>> pq;
    for(int i = 0; i < n; i++){
        if(i)   pq.push({-d[i], {i-1, i}});
        pq.push({-c[i], {n, i}});
    }
    while(!pq.empty()){
        auto p = pq.top();  pq.pop();
        int i = p.second.first, j = p.second.second;
        if(uf.parent(i) != uf.parent(j)){
            uf.unite(i, j);
            ans += -p.first;
        }
    }
    cout << ans << endl;
    return 0;
}
0