회원가입
로그인
Toggle navigation
문제
문제
전체 문제
문제 출처
단계별로 풀어보기
알고리즘 분류
추가된 문제
문제 순위
문제
푼 사람이 한 명인 문제
아무도 못 푼 문제
최근 제출된 문제
최근 풀린 문제
랜덤
출처
ICPC
Olympiad
한국정보올림피아드
한국정보올림피아드시․도지역본선
전국 대학생 프로그래밍 대회 동아리 연합
대학교 대회
카카오 코드 페스티벌
Coder's High
ICPC
Regionals
World Finals
Korea Regional
Africa and the Middle East Regionals
Europe Regionals
Latin America Regionals
North America Regionals
South Pacific Regionals
문제집
대회
1
채점 현황
랭킹
게시판
그룹
더 보기
재채점 기록
블로그
강의
실험실
도움말
BOJ Stack
BOJ Book
전체
공지
자유
질문
오타/오역/요청
게시판 공지
홍보
업데이트
solved.ac
글쓰기
질문 도움말
자주묻는 질문
왜 틀렸는지 알려주시면 감사하겠습니다 ㅠ..(java)
2887번 - 행성 터널
qorhkdrbs
3년 전
0
11%넘어서 실패뜨는데, 혹시 이유 알려주시면 감사하겠습니다 .. ㅠㅠ
import java.util.Arrays; import java.util.PriorityQueue; import java.util.Scanner; class Planet { int i; int x; int y; int z; public Planet(int i,int x,int y,int z) { this.i = i; this.x = x; this.y = y; this.z = z; } } class Edge implements Comparable<Edge>{ int start; int to; int weight; public Edge(int start,int to,int weight) { this.start = start; this.to = to; this.weight = weight; } public int compareTo(Edge e) { return this.weight-e.weight; } } public class Main { static int[] parent; public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); Planet[] array = new Planet[n]; parent = new int[n]; PriorityQueue<Edge> pq = new PriorityQueue<>(); for(int i=0; i<n; i++) { array[i] = new Planet(i,sc.nextInt(),sc.nextInt(),sc.nextInt()); } // Arrays.sort(array,(o1,o2)->o1.x-o2.x); for(int i=1; i<n; i++) { pq.offer(new Edge(array[i-1].i,array[i].i,Math.abs(array[i].x-array[i-1].x))); } // Arrays.sort(array,(o1,o2)->o1.y-o2.y); for(int i=1; i<n; i++) { pq.offer(new Edge(array[i-1].i,array[i].i,Math.abs(array[i].y-array[i-1].y))); } // Arrays.sort(array,(o1,o2)->o1.z-o2.z); for(int i=1; i<n; i++) { pq.offer(new Edge(array[i-1].i,array[i].i,Math.abs(array[i].z-array[i-1].z))); } //parent init for(int i=0; i<n; i++) { parent[i] = i; } //union int sum=0; int count = 0; while(!pq.isEmpty()) { Edge e = pq.poll(); if(getParent(e.start)!= getParent(e.to) ) { sum += e.weight; if(++count == n-1) break; union(e.start,e.to); } } System.out.println(sum); } static public int getParent(int x) { if(parent[x] == x) return x; return getParent(parent[x]); } static public void union(int x,int y) { x = getParent(x); y = getParent(y); if(x<y) { parent[y] = x; }else { parent[x] = y; } } }
댓글을 작성하려면
로그인
해야 합니다.
qorhkdrbs 3년 전
11%넘어서 실패뜨는데, 혹시 이유 알려주시면 감사하겠습니다 .. ㅠㅠ