Skip to content
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
78 changes: 78 additions & 0 deletions w18/yongseon/네트워크연결.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package w18.yongseon;

import java.util.ArrayList;
import java.util.List;
import java.util.PriorityQueue;
import java.util.Scanner;

public class 네트워크연결 {

static class Edge implements Comparable<Edge> {
int dest;
int weight;

public Edge(int dest, int weight) {
this.dest = dest;
this.weight = weight;
}

@Override
public int compareTo(Edge o) {
return Integer.compare(this.weight, o.weight);
}
}

public static List<List<Edge>> graph = new ArrayList<>();

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

// 컴퓨터 개수
int n = sc.nextInt();
// 연결할 수 있는 선의 개수
int m = sc.nextInt();

for (int i = 0; i <= n ; i++) {
graph.add(new ArrayList<>());
}

// 간선 정보 입력
for (int i = 0; i < m; i++) {
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();

graph.get(a).add(new Edge(b, c));
graph.get(b).add(new Edge(a, c));
}

System.out.println(prim(n));
}

public static int prim(int n) {
boolean[] visited = new boolean[n+1];
PriorityQueue<Edge> pq = new PriorityQueue<>();

int totalWeight = 0;
pq.offer(new Edge(1, 0));

while (!pq.isEmpty()) {
Edge current = pq.poll();

if (visited[current.dest]) {
continue;
}

visited[current.dest] = true;
totalWeight = current.weight;

for (Edge edge : graph.get(current.dest)) {
if (!visited[edge.dest]) {
pq.offer(edge);
}
}
}

return totalWeight;
}
}
72 changes: 72 additions & 0 deletions w18/yongseon/물대기.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package w18.yongseon;

import java.util.*;

public class 물대기 {
static class Edge implements Comparable<Edge> {
int dest;
int weight;

public Edge(int dest, int weight) {
this.dest = dest;
this.weight = weight;
}

@Override
public int compareTo(Edge o) {
return Integer.compare(this.weight, o.weight);
}
}

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();

// 비용 정보 입력 받기
int[] wellCost = new int[n + 1]; // 논의 우물 파기 비용
for (int i = 1; i <= n; i++) {
wellCost[i] = sc.nextInt();
}

// 논들 사이의 연결 비용 입력 받기
int[][] connectCost = new int[n + 1][n + 1];
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
connectCost[i][j] = sc.nextInt();
}
}

// 프림 알고리즘을 위한 우선순위 큐 사용
PriorityQueue<Edge> pq = new PriorityQueue<>();
boolean[] visited = new boolean[n + 1];
int totalCost = 0;

// 가상의 0번 정점에서 각 논으로 우물 파기 비용으로 연결되는 간선 추가
for (int i = 1; i <= n; i++) {
pq.offer(new Edge(i, wellCost[i]));
}

int count = 0;
while (!pq.isEmpty() && count < n) {
Edge current = pq.poll();

if (visited[current.dest]) {
continue;
}

visited[current.dest] = true;
totalCost += current.weight;
count++;

// 현재 정점과 연결된 다른 논들로의 간선을 추가
for (int i = 1; i <= n; i++) {
if (!visited[i]) {
pq.offer(new Edge(i, connectCost[current.dest][i]));
}
}
}

System.out.println(totalCost);
}
}

89 changes: 89 additions & 0 deletions w18/yongseon/정복자.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package w18.yongseon;

import java.util.ArrayList;
import java.util.List;
import java.util.PriorityQueue;
import java.util.Scanner;

public class 정복자 {
private static List<List<Edge>> graph = new ArrayList<>();

public static class Edge implements Comparable<Edge> {
int dest;
int weight;

public Edge(int dest, int weight) {
this.dest = dest;
this.weight = weight;
}

@Override
public int compareTo(Edge o) {
return Integer.compare(this.weight, o.weight);
}
}

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// 도시의 개수
int n = sc.nextInt();

// 도로의 개수
int m = sc.nextInt();

// 정복할 때마다 증가하는 비용
int t = sc.nextInt();

for (int i = 0; i <= n; i++) {
graph.add(new ArrayList<>());
}

for (int i = 0; i < m; i++) {
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();

graph.get(a).add(new Edge(b, c));
graph.get(b).add(new Edge(a, c));
}

System.out.println(prim(n, t));
}

private static int prim(int n, int t) {
boolean[] visited = new boolean[n + 1];
PriorityQueue<Edge> pq = new PriorityQueue<>();
int totalWeight = 0;
int conqueredCities = 0;

pq.offer(new Edge(1, 0));

while (!pq.isEmpty()) {
Edge current = pq.poll();

if (visited[current.dest]) {
continue;
}

visited[current.dest] = true;

totalWeight += current.weight;

// 도시를 하나 정복할 때마다 정복한 도시의 수를 증가
conqueredCities++;

// 도시를 정복할 때마다 모든 도로의 비용이 t만큼 증가함
if (conqueredCities > 2) { // 처음 시작 시에는 비용 증가 없음
totalWeight += t*(conqueredCities-2);
}

for (Edge edge : graph.get(current.dest)) {
if (!visited[edge.dest]) {
pq.offer(edge);
}
}
}

return totalWeight;
}
}
78 changes: 78 additions & 0 deletions w18/yongseon/최소스패닝트리.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package w18.yongseon;

import java.util.ArrayList;
import java.util.List;
import java.util.PriorityQueue;
import java.util.Scanner;

public class 최소스패닝트리 {
static List<List<Edge>> graph = new ArrayList<>();

static class Edge implements Comparable<Edge> {
int dest;
int weight;

public Edge(int dest, int weight) {
this.dest = dest;
this.weight = weight;
}

@Override
public int compareTo(Edge o) {
return Integer.compare(this.weight, o.weight);
}
}

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

// 정점의 개수
int v = sc.nextInt();
// 간선의 개수
int e = sc.nextInt();

for (int i = 0; i <= v; i++) {
graph.add(new ArrayList<>());
}

// 간선 정보 입력
for (int i = 0; i < e; i++) {
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();

graph.get(a).add(new Edge(b, c));
graph.get(b).add(new Edge(a, c));
}

System.out.println(prim(v));
}

private static int prim(int v) {
boolean[] visited = new boolean[v+1];
PriorityQueue<Edge> pq = new PriorityQueue<>();

int totalWeight = 0;

pq.offer(new Edge(1, 0));

while (!pq.isEmpty()) {
Edge current = pq.poll();

if (visited[current.dest]) {
continue;
}

visited[current.dest] = true;
totalWeight += current.weight;

for (Edge edge : graph.get(current.dest)) {
if (!visited[edge.dest]) {
pq.offer(edge);
}
}
}

return totalWeight;
}
}