結果

問題 No.860 買い物
ユーザー ahe100ahe100
提出日時 2019-08-10 00:01:56
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 172 ms / 1,000 ms
コード長 1,184 bytes
コンパイル時間 1,820 ms
コンパイル使用メモリ 174,096 KB
実行使用メモリ 22,888 KB
最終ジャッジ日時 2023-09-26 22:14:53
合計ジャッジ時間 4,327 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
9,896 KB
testcase_01 AC 5 ms
10,028 KB
testcase_02 AC 4 ms
9,836 KB
testcase_03 AC 4 ms
10,020 KB
testcase_04 AC 4 ms
9,880 KB
testcase_05 AC 5 ms
9,864 KB
testcase_06 AC 9 ms
10,396 KB
testcase_07 AC 172 ms
22,784 KB
testcase_08 AC 170 ms
22,724 KB
testcase_09 AC 164 ms
22,888 KB
testcase_10 AC 167 ms
22,848 KB
testcase_11 AC 117 ms
22,744 KB
testcase_12 AC 116 ms
22,676 KB
testcase_13 AC 169 ms
22,676 KB
testcase_14 AC 141 ms
22,844 KB
testcase_15 AC 99 ms
22,872 KB
testcase_16 AC 150 ms
22,872 KB
testcase_17 AC 147 ms
22,768 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i,n) for(ll i = 0;i<((ll)(n));i++)
#define reg(i,a,b) for(ll i = ((ll)(a));i<=((ll)(b));i++)
#define irep(i,n) for(ll i = ((ll)(n)-1);i>=0;i--)
#define ireg(i,a,b) for(ll i = ((ll)(b));i>=((ll)(a));i--)

/*
*/

ll n,c[100010],d[100010],ans=0;
vector<ll> v[100010],cost[100010];

ll prim(ll root, vector<ll> v[], vector<ll> c[]){
	ll ans=0;
	int used[100010]={};
	priority_queue<pair<ll,ll>, vector<pair<ll,ll>>, greater<pair<ll,ll>>> Q;
	Q.push({0,root});
	while(!Q.empty()){
		pair<ll,ll> p = Q.top(); Q.pop();
		if(used[p.second]!=0)continue;
		used[p.second]=1;
		ans+=p.first;
		for(int i=0; i<v[p.second].size(); ++i){
			ll q = v[p.second][i];
			if(used[q]==0)Q.push({c[p.second][i], q});
		}
	}
	return ans;
}

void init(){
	cin>>n;
	reg(i,1,n)cin>>c[i]>>d[i];
	reg(i,1,n)ans+=c[i];	
	reg(i,1,n){
		v[i].push_back(0);
		v[0].push_back(i);
		cost[i].push_back(c[i]);
		cost[0].push_back(c[i]);
	}
	reg(i,2,n){
		v[i].push_back(i-1);
		v[i-1].push_back(i);
		cost[i].push_back(d[i]);
		cost[i-1].push_back(d[i]);
	}
}

int main(void){
	init();
	ans += prim(0,v,cost);
	cout<<ans<<endl;
	return 0;
}
0