1 file changed
+44
-0
lines changedLines changed: 44 additions & 0 deletions
@@ -0,0 +1,44 @@
1
+// Authored by : haneulkimdev
2
+// Co-authored by : -
3
+// http://boj.kr/4bf034b6c2d945d29716a5483d78bb28
4
+#include <bits/stdc++.h>
5
+using namespace std;
6
+#define X first
7
+#define Y second
8
+
9
+int n;
10
+vector<pair<int, int>> adj[1001];
11
+bool chk[1001];
12
+
13
+int main(void) {
14
+ ios::sync_with_stdio(0);
15
+ cin.tie(0);
16
+ cin >> n;
17
+ for (int i = 1; i <= n; i++) {
18
+ for (int j = 1; j <= n; j++) {
19
+ int c;
20
+ cin >> c;
21
+ if (i >= j) continue;
22
+ adj[i].push_back({c, j});
23
+ adj[j].push_back({c, i});
24
+ }
25
+ }
26
+ int cnt = 0;
27
+ long long ans = 0;
28
+ priority_queue<pair<int, int>, vector<pair<int, int>>,
29
+ greater<pair<int, int>>>
30
+ pq;
31
+ chk[1] = true;
32
+ for (auto nxt : adj[1]) pq.push(nxt);
33
+ while (cnt < n - 1) {
34
+ auto cur = pq.top();
35
+ pq.pop();
36
+ if (chk[cur.Y]) continue;
37
+ ans += cur.X;
38
+ chk[cur.Y] = true;
39
+ cnt++;
40
+ for (auto nxt : adj[cur.Y])
41
+ if (!chk[nxt.Y]) pq.push(nxt);
42
+ }
43
+ cout << ans;
44
+}
0 commit comments