Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<queue>
- using namespace std;
- typedef long long int ll;
- int parent[1010];
- int find(int x){
- if(parent[x] == x)return x;
- return parent[x] = find(parent[x]);
- }
- void unions(int x,int y){
- int rx = find(x);
- int ry = find(y);
- parent[rx] = ry;
- }
- struct edge{
- int u,v;
- ll w;
- bool operator < (const edge& rhs)const
- {
- return w > rhs.w;
- }
- };
- typedef long long int ll;
- int main()
- {
- for(int i = 0 ; i <= 1000 ; i++)parent[i] = i;
- int n;
- scanf("%d",&n);
- priority_queue<edge> pq;
- for(int i = 1 ; i <= n ; i++){
- ll t;
- scanf("%lld",&t);
- pq.push({0,i,t});
- }
- ll arr[n+1];
- for(int i = 1 ; i <= n ; i++){
- scanf("%lld",&arr[i]);
- for(int j = i - 1 ; j >= 1 ; j --){
- pq.push({i,j,arr[i]+arr[j]});
- }
- }
- ll ans = 0;
- while(!pq.empty()){
- int u = pq.top().u,v = pq.top().v;
- ll w = pq.top().w;
- pq.pop();
- if(find(u) != find(v)){
- unions(u,v);
- ans += w;
- }
- }
- printf("%lld",ans);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment