File tree Expand file tree Collapse file tree 1 file changed +46
-0
lines changed
Expand file tree Collapse file tree 1 file changed +46
-0
lines changed Original file line number Diff line number Diff line change 1+ ``` java
2+ import java.io.* ;
3+ import java.util.* ;
4+
5+ public class Main {
6+ public static void main (String [] args ) throws IOException {
7+ BufferedReader br = new BufferedReader (new InputStreamReader (System . in));
8+ int N = Integer . parseInt(br. readLine());
9+ int [][] cost = new int [N ][N ];
10+ for (int i = 0 ; i < N ; i++ ){
11+ StringTokenizer st = new StringTokenizer (br. readLine());
12+ for (int j = 0 ; j < N ; j++ ){
13+ cost[i][j] = Integer . parseInt(st. nextToken());
14+ }
15+ }
16+ boolean [] visited = new boolean [N ];
17+ PriorityQueue<int[]> pq = new PriorityQueue<> (
18+ (a, b) - > a[1 ] - b[1 ]
19+ );
20+ pq. offer(new int []{0 , 0 });
21+
22+ long answer = 0 ;
23+ int count = 0 ;
24+ while (! pq. isEmpty()){
25+ int [] cur = pq. poll();
26+ int u = cur[0 ];
27+ int c = cur[1 ];
28+ if (visited[u])
29+ continue ;
30+
31+ visited[u] = true ;
32+ answer += c;
33+ count++ ;
34+ if (count == N )
35+ break ;
36+ for (int v = 0 ; v < N ; v++ ){
37+ if (! visited[v]){
38+ pq. offer(new int []{v, cost[u][v]});
39+ }
40+ }
41+ }
42+ System . out. println(answer);
43+ }
44+ }
45+
46+ ```
You can’t perform that action at this time.
0 commit comments