diff --git a/06_mst/lee/10423.java b/06_mst/lee/10423.java new file mode 100644 index 0000000..a60adba --- /dev/null +++ b/06_mst/lee/10423.java @@ -0,0 +1,72 @@ +import java.io.*; +import java.util.*; + +public class Main { + static class Edge { + int to; + int weight; + + public Edge(int to, int weight) { + this.to = to; + this.weight = weight; + } + } + + static int N, M, K; + static ArrayList<Edge>[] powerStations; + static boolean[] visited; + + public static void main(String[] args) throws Exception { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st = new StringTokenizer(br.readLine()); + + N = Integer.parseInt(st.nextToken()); + M = Integer.parseInt(st.nextToken()); + K = Integer.parseInt(st.nextToken()); + + powerStations = new ArrayList[N + 1]; + for (int i = 0; i <= N; i++) { + powerStations[i] = new ArrayList<>(); + } + st = new StringTokenizer(br.readLine()); + for (int i = 0; i < K; i++) { + int num = Integer.parseInt(st.nextToken()); + powerStations[0].add(new Edge(num, 0)); + powerStations[num].add(new Edge(0, 0)); + } + for (int i = 1; i <= M; i++) { + st = new StringTokenizer(br.readLine()); + int u = Integer.parseInt(st.nextToken()); + int v = Integer.parseInt(st.nextToken()); + int w = Integer.parseInt(st.nextToken()); + powerStations[u].add(new Edge(v, w)); + powerStations[v].add(new Edge(u, w)); + } + System.out.println(prim()); + } + + private static int prim() { + int totalWeight = 0; + visited = new boolean[N + 1]; + PriorityQueue<Edge> pq = new PriorityQueue<>((a, b) -> a.weight - b.weight); + pq.add(new Edge(1, 0)); + + while (!pq.isEmpty()) { + Edge current = pq.poll(); + int node = current.to; + int weight = current.weight; + + if (visited[node]) continue; + + visited[node] = true; + totalWeight += weight; + + for (Edge neighbor : powerStations[node]) { + if (!visited[neighbor.to]) { + pq.add(neighbor); + } + } + } + return totalWeight; + } +} \ No newline at end of file diff --git a/06_mst/lee/1197.java b/06_mst/lee/1197.java new file mode 100644 index 0000000..7f8312c --- /dev/null +++ b/06_mst/lee/1197.java @@ -0,0 +1,68 @@ +import java.io.*; +import java.util.*; + +public class Main { + static class Edge { + int to; + int weight; + + Edge(int to, int weight) { + this.to = to; + this.weight = weight; + } + } + + private static int V, E; + private static List<Edge>[] edges; + private static boolean[] visited; + + 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()); + + edges = new List[V + 1]; + for (int i = 1; i <= V; i++) { + edges[i] = new ArrayList<>(); + } + 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 c = Integer.parseInt(st.nextToken()); + edges[a].add(new Edge(b, c)); + edges[b].add(new Edge(a, c)); + } + + System.out.println(prim(1)); + } + + + private static int prim(int start) { + int totalWeight = 0; + visited = new boolean[V + 1]; + PriorityQueue<Edge> pq = new PriorityQueue<>(Comparator.comparingInt(a -> a.weight)); + + pq.add(new Edge(1, 0)); + + while (!pq.isEmpty()) { + Edge current = pq.poll(); + int node = current.to; + int weight = current.weight; + + if (visited[node]) continue; + + visited[node] = true; + totalWeight += weight; + + for (Edge neighbor : edges[node]) { + if (!visited[neighbor.to]) { + pq.add(neighbor); + } + } + } + return totalWeight; + } +} diff --git a/06_mst/lee/16398.java b/06_mst/lee/16398.java new file mode 100644 index 0000000..925ed55 --- /dev/null +++ b/06_mst/lee/16398.java @@ -0,0 +1,61 @@ +import java.io.*; +import java.util.*; + +public class Main { + + static class Edge { + int to; + int weight; + + Edge(int to, int weight) { + this.to = to; + this.weight = weight; + } + } + + private static int N; + private static int[][] planet; + private static boolean[] inMST; + + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + + N = Integer.parseInt(br.readLine()); + planet = new int[N + 1][N + 1]; + for (int i = 1; i <= N; i++) { + StringTokenizer st = new StringTokenizer(br.readLine()); + for (int j = 1; j <= N; j++) { + planet[i][j] = Integer.parseInt(st.nextToken()); + } + } + + System.out.println(prim(1)); + } + + + private static long prim(int start) { + long totalWeight = 0; + inMST = new boolean[N + 1]; + PriorityQueue<Edge> pq = new PriorityQueue<>(Comparator.comparingInt(a -> a.weight)); + + pq.add(new Edge(1, 0)); + + while (!pq.isEmpty()) { + Edge current = pq.poll(); + int node = current.to; + int weight = current.weight; + + if (inMST[node]) continue; + + inMST[node] = true; + totalWeight += weight; + + for (int i = 1; i <= N; i++) { + if (!inMST[i]) { + pq.add(new Edge(i, planet[node][i])); + } + } + } + return totalWeight; + } +} diff --git a/06_mst/lee/1647.java b/06_mst/lee/1647.java new file mode 100644 index 0000000..c64e845 --- /dev/null +++ b/06_mst/lee/1647.java @@ -0,0 +1,67 @@ +import java.io.*; +import java.util.*; + +public class Main { + static class Edge { + int to; + int weight; + + public Edge(int to, int weight) { + this.to = to; + this.weight = weight; + } + } + + static int N, M; + static ArrayList<Edge>[] cities; + private static boolean[] visited; + + public static void main(String[] args) throws Exception { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st = new StringTokenizer(br.readLine()); + + N = Integer.parseInt(st.nextToken()); + M = Integer.parseInt(st.nextToken()); + cities = new ArrayList[N + 1]; + for (int i = 1; i <= N; i++) { + cities[i] = new ArrayList<>(); + } + for (int i = 1; i <= M; i++) { + st = new StringTokenizer(br.readLine()); + int a = Integer.parseInt(st.nextToken()); + int b = Integer.parseInt(st.nextToken()); + int c = Integer.parseInt(st.nextToken()); + cities[a].add(new Edge(b, c)); + cities[b].add(new Edge(a, c)); + } + System.out.println(prim()); + } + + private static int prim() { + int totalWeight = 0; + int maxWeight = 0; + visited = new boolean[N + 1]; + PriorityQueue<Edge> pq = new PriorityQueue<>((a, b) -> a.weight - b.weight); + + pq.add(new Edge(1, 0)); + + while (!pq.isEmpty()) { + Edge current = pq.poll(); + int node = current.to; + int weight = current.weight; + + if (visited[node]) continue; + + visited[node] = true; + totalWeight += weight; + maxWeight = Math.max(maxWeight, weight); + + for (Edge neighbor : cities[node]) { + if (!visited[neighbor.to]) { + pq.add(neighbor); + } + } + } + return totalWeight - maxWeight; + } +} \ No newline at end of file diff --git a/06_mst/lee/9372.java b/06_mst/lee/9372.java new file mode 100644 index 0000000..34f11e8 --- /dev/null +++ b/06_mst/lee/9372.java @@ -0,0 +1,74 @@ +import java.io.*; +import java.util.*; + +public class Main { + private static int N, M; + private static boolean[] visited; + private static ArrayList<Integer>[] plane; + + public static void main(String[] args) throws NumberFormatException, IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringBuilder sb = new StringBuilder(); + + int T = Integer.parseInt(br.readLine()); + + for (int tc = 1; tc <= T; tc++) { + + StringTokenizer st = new StringTokenizer(br.readLine()); + N = Integer.parseInt(st.nextToken()); + M = Integer.parseInt(st.nextToken()); + + plane = new ArrayList[N + 1]; + for (int i = 1; i <= N; i++) { + plane[i] = new ArrayList<Integer>(); + } + + for (int i = 1; i <= M; i++) { + st = new StringTokenizer(br.readLine()); + int a = Integer.parseInt(st.nextToken()); + int b = Integer.parseInt(st.nextToken()); + + plane[a].add(b); + plane[b].add(a); + } + + visited = new boolean[N + 1]; + sb.append(dfs(1)).append("\n"); // 방법 1 + sb.append(bfs(1) - 1).append("\n"); // 방법 2 + sb.append((N - 1)).append("\n"); // 방법 3 (최소 신장 트리의 성질 : 간선의 개수 == 정점의 개수 - 1) + } + System.out.println(sb.toString()); + } + + private static int dfs(int start) { + visited[start] = true; + + int count = 0; + for (int destination : plane[start]) { + if (!visited[destination]) { + count += dfs(destination) + 1; + } + } + return count; + } + + private static int bfs(int start) { + ArrayDeque<Integer> queue = new ArrayDeque<>(); + queue.add(start); + visited[start] = true; + + int count = 0; + while (!queue.isEmpty()) { + count++; + int current = queue.poll(); + + for (int destination : plane[current]) { + if (!visited[destination]) { + visited[destination] = true; + queue.add(destination); + } + } + } + return count; + } +}