회원가입
로그인
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
글쓰기
질문 도움말
자주묻는 질문
메모리초과 왜 때문이죠..?
2406번 - 안정적인 네트워크
tmdwkd2
3년 전
0
문제가 무엇인지 도무지 모르겠습니다..
import java.util.ArrayList; import java.util.PriorityQueue; import java.util.Scanner; public class Main { static int n, m; static int p[]; static int line; static class point implements Comparable<point> { int y; int x; int weight; public point(int y, int x, int weight) { super(); this.y = y; this.x = x; this.weight = weight; } @Override public int compareTo(point o) { // TODO Auto-generated method stub return Integer.compare(this.weight, o.weight); } } static PriorityQueue<point> pq; public static void main(String[] args) { Scanner sc = new Scanner(System.in); n = sc.nextInt(); m = sc.nextInt(); p = new int[n+1]; pq = new PriorityQueue<>(); for (int i = 1; i < n+1; i++) { p[i] = i; } for (int i = 0; i < m; i++) { int a = sc.nextInt(); int b = sc.nextInt(); union(a, b); line++; } int w; for (int i = 1; i < n+1; i++) { for (int j = 1; j < n+1; j++) { w = sc.nextInt(); if(i < j) { pq.offer(new point(i, j, w)); } } } int cnt = 0; int sum = 0; ArrayList<int[]> ans = new ArrayList<>(); while (!pq.isEmpty()) { point cur = pq.poll(); if (cur.y == 1 || cur.x == 1) continue; if (union(cur.y, cur.x)) { cnt++; sum += cur.weight; ans.add(new int[] { cur.y, cur.x }); line++; } // mst로 연결된 간선의 갯수가 1번 정점빼고 다 연결됨 if (line == n - 2) break; } System.out.println(sum + " " + cnt); for (int[] is : ans) { System.out.println(is[0] + " " + is[1]); } } private static boolean union(int a, int b) { a = find(a); b = find(b); if(a == b) { return false; } if (a < b) { p[b] = a; return true; } else { p[a] = b; return true; } } private static int find(int x) { // TODO Auto-generated method stub if (p[x] == x) return x; return p[x] = find(p[x]); } }
댓글을 작성하려면
로그인
해야 합니다.
tmdwkd2 3년 전
문제가 무엇인지 도무지 모르겠습니다..