Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Week11] 크루스칼, 프림 - 선희 #91

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 94 additions & 0 deletions 11-Kruskal&Prim/Sunny/네트워크 연결.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package Baekjoon;
// 백준 1922번 - 네트워크 연결
import java.util.*;
import java.io.*;
class Edge implements Comparable<Edge> {
private int distance;
private int nodeA;
private int nodeB;

public Edge(int distance, int nodeA, int nodeB) {
this.distance = distance;
this.nodeA = nodeA;
this.nodeB = nodeB;
}

public int getDistance() {
return distance;
}

public int getNodeA() {
return nodeA;
}

public int getNodeB() {
return nodeB;
}

// 거리(비용)가 짧은 것이 높은 우선순위를 가지도록 설정
@Override
public int compareTo(Edge other) {
if (this.distance < other.distance) {
return -1;
}
return 1;
}
}
public class Solution_1922 {
public static int n; // 컴퓨터 수
public static int m; // 간선 수
public static ArrayList<Edge> edges = new ArrayList<>(); // 간선 정보
public static int[] parent = new int[1001];
public static int result;

// 특정 원소가 속한 집합을 찾기
public static int findParent(int x) {
// 루트 노드가 아니라면, 루트 노드를 찾을 때까지 재귀적으로 호출
if (x == parent[x]) return x;
return parent[x] = findParent(parent[x]);
}

// 두 원소가 속한 집합을 합치기
public static void unionParent(int a, int b) {
a = findParent(a);
b = findParent(b);
if (a < b) parent[b] = a;
else parent[a] = b;
}

public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
n = Integer.parseInt(br.readLine());
m = Integer.parseInt(br.readLine());
StringTokenizer st;
for (int i = 0; i < m; i++) {
st = new StringTokenizer(br.readLine());
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
int d = Integer.parseInt(st.nextToken());
edges.add(new Edge(d, a, b));
}

// 부모 테이블상에서, 부모를 자기 자신으로 초기화
for (int i = 1; i <= n; i++) {
parent[i] = i;
}

// 간선을 비용순으로 정렬
Collections.sort(edges);

// 간선을 하니씩 확인하며
for (int i = 0; i < edges.size(); i++) {
int cost = edges.get(i).getDistance();
int a = edges.get(i).getNodeA();
int b = edges.get(i).getNodeB();
// 사이클이 발생하지 않는 경우에만 집합에 포함
if (findParent(a) != findParent(b)) {
unionParent(a, b);
result += cost;
}
}
System.out.println(result);
}

}
18 changes: 18 additions & 0 deletions 11-Kruskal&Prim/Sunny/위장.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package Programmers;
// 프로그래머스 - 위장
import java.util.*;
public class Main {
public int solution(String[][] clothes) {
HashMap<String, Integer> map = new HashMap<>();
for (int i = 0; i < clothes.length; i++) {
map.put(clothes[i][1], map.getOrDefault(clothes[i][1], 0) + 1);
}

int answer = 1;
for (String key : map.keySet()) {
int count = map.get(key) + 1;
answer *= count;
}
return answer - 1;
}
}
97 changes: 97 additions & 0 deletions 11-Kruskal&Prim/Sunny/최소 스패닝 트리.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package Baekjoon;
// 백준 1197번 - 최소 스패닝 트리
import java.util.*;
import java.io.*;

class Edge implements Comparable<Edge> {
private int distance;
private int nodeA;
private int nodeB;

public Edge(int distance, int nodeA, int nodeB) {
this.distance = distance;
this.nodeA = nodeA;
this.nodeB = nodeB;
}

public int getDistance() {
return distance;
}

public int getNodeA() {
return nodeA;
}

public int getNodeB() {
return nodeB;
}

// 거리(비용)가 짧은 것이 높은 우선순위를 가지도록 설정
@Override
public int compareTo(Edge other) {
if (this.distance < other.distance) {
return -1;
}
return 1;
}
}
public class Solution_1197 {
public static int v; // 정점의 개수
public static int e; // 간선의 개수

public static ArrayList<Edge> edges = new ArrayList<>(); // 간선 정보
public static int[] parent = new int[100001];
public static int result;

// 특정 원소가 속한 집합을 찾기
public static int findParent(int x) {
// 루트 노드가 아니라면, 루트 노드를 찾을 때까지 재귀적으로 호출
if (x == parent[x]) return x;
return parent[x] = findParent(parent[x]);
}

// 두 원소가 속한 집합을 합치기
public static void unionParent(int a, int b) {
a = findParent(a);
b = findParent(b);
if (a < b) parent[b] = a;
else parent[a] = b;
}

public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
v = Integer.parseInt(st.nextToken());
e = Integer.parseInt(st.nextToken());
for (int i = 0; i < e; i++) {
st = new StringTokenizer(br.readLine());
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
int d = Integer.parseInt(st.nextToken());
edges.add(new Edge(d, a, b));
}

// 부모 테이블상에서, 부모를 자기 자신으로 초기화
for (int i = 1; i <= v; i++) {
parent[i] = i;
}

// 간선을 비용순으로 정렬
Collections.sort(edges);

// 간선을 하니씩 확인하며
for (int i = 0; i < edges.size(); i++) {
int cost = edges.get(i).getDistance();
int a = edges.get(i).getNodeA();
int b = edges.get(i).getNodeB();
// 사이클이 발생하지 않는 경우에만 집합에 포함
if (findParent(a) != findParent(b)) {
unionParent(a, b);
result += cost;
}
}
System.out.println(result);

}

}