-
Notifications
You must be signed in to change notification settings - Fork 0
/
BOJ16398.cpp
59 lines (57 loc) · 1010 Bytes
/
BOJ16398.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#define MAX 1001
#define INF 1000000001
using namespace std;
vector <pair <int,int> > v[MAX];
priority_queue < pair<int ,pair< int,int > > > q;
int arr[MAX][MAX]={0};
int parent[MAX]={0};
long long sum=0;
int minusI(int a){
return -1*a;
}
int find(int x){
if(parent[x]==x)
return x;
else
return parent[x]=find(parent[x]);
}
bool unionfind(int i,int j){
i=find(i);
j=find(j);
if(i==j)
return false;
else if(i>j)
parent[i]=j;
else
parent[j]=i;
return true;
}
int main(void){
int n;
cin>>n;
for(int i=1;i<=n;i++){
parent[i]=i;
for(int j=1;j<=n;j++){
cin>>arr[i][j];
if(i==j)
continue;
else if(i<j){
q.push({minusI(arr[i][j]),{i,j}});
//cout<<arr[i][j];
}
v[i].push_back({j,arr[i][j]});
}
}
while(!q.empty()){
int weight=minusI(q.top().first);
if(unionfind(q.top().second.first,q.top().second.second)){
sum+=weight;
}
q.pop();
}
cout<<sum;
}