회원가입
로그인
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
문제집
대회
채점 현황
게시판
그룹
더 보기
재채점 기록
블로그
도움말
전체
공지
자유
질문
오타/오역/요청
게시판 공지
홍보
업데이트
solved.ac
글쓰기
질문 도움말
자주묻는 질문
Kruskal 알고리즘 반례가 있을까요?..12%
4386번 - 별자리 만들기
kiw970923
2년 전
0
아무리 다시 풀어봐도 어디서 잘못된건지 모르겠습니다..
import java.util.*; import java.io.*; public class Main{ static class Star{ double x, y; Star(double x, double y){ this.x = x; this.y = y; } } static class Edge implements Comparable<Edge>{ int v1, v2; double distance; Edge(int v1, int v2, double distance){ this.v1 = v1; this.v2 = v2; this.distance = distance; } @Override public int compareTo(Edge e){ if(distance - e.distance > 0) return 1; return -1; } } static Star[] stars; static int[] p; public static void main(String[] args) throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); stars = new Star[n]; p = new int[n]; List<Edge> edgeList = new ArrayList<>(); for(int i = 0; i < n; i++){ StringTokenizer st = new StringTokenizer(br.readLine()); double x = Double.parseDouble(st.nextToken()); double y = Double.parseDouble(st.nextToken()); stars[i] = new Star(x, y); for(int j = 0; j < i; j++){ double d = Math.sqrt(Math.pow(stars[i].x - stars[j].x, 2) + Math.pow(stars[i].y - stars[j].y, 2)); edgeList.add(new Edge(i, j, d)); } p[i] = i; } Collections.sort(edgeList); int cnt = 0; double cost = 0; for(Edge temp : edgeList){ if(find(temp.v1) != find(temp.v2)){ union(temp.v1, temp.v2); cnt++; cost += temp.distance; } if(cnt == n - 1) break; } System.out.println(Math.round(cost * 100) / 100.0); } static int find(int target){ if(target == p[target]) return target; return p[target] = find(p[target]); } static void union(int i, int j){ p[i] = find(j); } }
댓글을 작성하려면
로그인
해야 합니다.
kiw970923 2년 전
아무리 다시 풀어봐도 어디서 잘못된건지 모르겠습니다..