diff --git "a/junsu/week14/BOJ1238 \355\214\214\355\213\260/BOJ1238.java" "b/junsu/week14/BOJ1238 \355\214\214\355\213\260/BOJ1238.java" new file mode 100644 index 0000000..00ec040 --- /dev/null +++ "b/junsu/week14/BOJ1238 \355\214\214\355\213\260/BOJ1238.java" @@ -0,0 +1,95 @@ +//BOJ1238 파티, 골드3 +//다익스트라 알고리즘 +//목적지로 간 후 다시 되돌아오는 거라서 다익스트라 목적지에서 시작점으로 한번 더 써주면 된다. +//visited 안쓴 버전 +//실수한부분 - pq.add 위치 잘못씀; +//pq.add(new Node(next.to, dist[next.to])); +import java.io.*; +import java.util.*; + +public class BOJ1238 { + static class Node implements Comparable{ + int to; + int cost; + + public Node(int to, int cost) { + this.to = to; + this.cost = cost; + } + + @Override + public int compareTo(Node o) { + return this.cost - o.cost; + } + } + static int N, M, X, MaxAns; + static int[] dist; + static boolean[] visited; + static int INF = 987654321; + static ArrayList[] nodeList; + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); + StringTokenizer st = new StringTokenizer(br.readLine()); + + N = Integer.parseInt(st.nextToken()); + M = Integer.parseInt(st.nextToken()); + X = Integer.parseInt(st.nextToken()); + nodeList = new ArrayList[N+1]; + for(int i = 1; i <= N; i++) { + nodeList[i] = new ArrayList<>(); + } + + for(int i = 0; i < M; i++) { + st = new StringTokenizer(br.readLine()); + int from = Integer.parseInt(st.nextToken()); + int to = Integer.parseInt(st.nextToken()); + int cost = Integer.parseInt(st.nextToken()); + nodeList[from].add(new Node(to, cost)); + } + + int temp; + for(int i = 1; i <= N; i++) { + dijstra(i); + temp = dist[X]; + //파티후 귀가 + dijstra(X); + temp += dist[i]; + + MaxAns = Math.max(MaxAns, temp); +// bw.write(MaxAns+"\n"); + } + + bw.write(MaxAns+"\n"); + bw.flush(); + bw.close(); + br.close(); + } + + // 파티하러 X 마을로 출발 + static void dijstra(int start) { + dist = new int[N+1]; + Arrays.fill(dist, INF); + +// //다익스트라 알고리즘 + PriorityQueue pq = new PriorityQueue<>(); + pq.add(new Node(start, 0)); + dist[start] = 0; + + while(!pq.isEmpty()) { + Node curNode = pq.poll(); + int cur = curNode.to; + int cost = curNode.cost; + + for(Node next : nodeList[cur]) { + if(dist[next.to] > dist[cur] + next.cost) { + dist[next.to] = dist[cur] + next.cost; + pq.add(new Node(next.to, dist[next.to])); + } +// 실수한부분 - pq.add 위치 잘못씀; +// pq.add(new Node(next.to, dist[next.to])); + } + } + + } +} \ No newline at end of file diff --git "a/junsu/week14/BOJ1261 \354\225\214\352\263\240\354\212\244\355\214\237/BOJ1261.java" "b/junsu/week14/BOJ1261 \354\225\214\352\263\240\354\212\244\355\214\237/BOJ1261.java" new file mode 100644 index 0000000..2308fef --- /dev/null +++ "b/junsu/week14/BOJ1261 \354\225\214\352\263\240\354\212\244\355\214\237/BOJ1261.java" @@ -0,0 +1,92 @@ +//BOJ1261 알고스팟, 골드4 +//다익스트라 알고리즘, 벽 부수고 이동하기랑 비슷 +//최단경로가 아닌 벽 부순 갯수 기준이라서 우선순위 큐 써야함 +import java.io.*; +import java.util.*; + +public class BOJ1261 { + static class Node implements Comparable{ + int x; + int y; + int cost; + + public Node(int x, int y, int cost) { + this.x = x; + this.y = y; + this.cost = cost; + } + @Override + public int compareTo(Node o) { + return this.cost - o.cost; + } + } + static int N, M, ans; + static int[][] map; + static int INF = 987654321; + static int[] dx = {0, 1, 0, -1}; //우 하 좌 상 + static int[] dy = {1, 0, -1, 0}; + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); + StringTokenizer st = new StringTokenizer(br.readLine()); + M = Integer.parseInt(st.nextToken()); + N = Integer.parseInt(st.nextToken()); + + map = new int[N+1][M+1]; + + for(int i = 1; i <= N; i++) { +// st = new StringTokenizer(br.readLine()); +// String temp = st.nextToken(); + String temp = br.readLine(); + for(int j = 1; j <= M; j++) { + map[i][j] = temp.charAt(j-1) - '0'; + } + } + +// for(int i = 1; i <= N; i++) { +// for(int j = 1; j <= M; j++) { +// System.out.print(map[i][j] + " "); +// } +// System.out.println(); +// } + ans = dijkstra(); + + bw.write(ans + "\n"); + bw.flush(); + bw.close(); + br.close(); + } + static int dijkstra() { + PriorityQueue pq = new PriorityQueue<>(); + pq.add(new Node(1, 1, 0)); // x, y, 벽을 부순 횟수 + boolean[][] visit = new boolean[N+1][M+1]; + visit[1][1] =true; + + while (!pq.isEmpty()) { + Node cur = pq.poll(); + int curX = cur.x; + int curY = cur.y; + int curCost = cur.cost; + + if (curX == N && curY == M) { + return curCost; + } + + for (int dic = 0; dic < 4; dic++) { + int nextX = curX + dx[dic]; + int nextY = curY + dy[dic]; + if (nextX <= 0 || nextY <= 0 || nextX > N || nextY > M) continue; + + if(!visit[nextX][nextY] && map[nextX][nextY] == 0) { + visit[nextX][nextY] = true; + pq.add(new Node(nextX, nextY, curCost)); + } + if(!visit[nextX][nextY] && map[nextX][nextY] == 1) { + visit[nextX][nextY] = true; + pq.add(new Node(nextX, nextY, curCost+1)); + } + } + } + return 0; + } +} diff --git "a/junsu/week14/BOJ14284 \352\260\204\354\204\240 \354\235\264\354\226\264\352\260\200\352\270\2602/BOJ14284.java" "b/junsu/week14/BOJ14284 \352\260\204\354\204\240 \354\235\264\354\226\264\352\260\200\352\270\2602/BOJ14284.java" new file mode 100644 index 0000000..8aef34f --- /dev/null +++ "b/junsu/week14/BOJ14284 \352\260\204\354\204\240 \354\235\264\354\226\264\352\260\200\352\270\2602/BOJ14284.java" @@ -0,0 +1,76 @@ +//BOJ14284 간선 이어가기2, 골드5 +//데이크스트라 알고리즘 +import java.io.*; +import java.util.*; + +public class BOJ14284 { + static class Node implements Comparable{ + int to; + int cost; + public Node(int to, int cost) { + this.to = to; + this.cost = cost; + } + @Override + public int compareTo(Node o) { + return this.cost - o.cost; + } + } + static int N, M, ans; + static ArrayList[] nodeList; + static int[] dist; + static int INF = 987654321; + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); + StringTokenizer st = new StringTokenizer(br.readLine()); + + N = Integer.parseInt(st.nextToken()); + M = Integer.parseInt(st.nextToken()); + dist = new int[N+1]; + nodeList = new ArrayList[N+1]; + for(int i = 1; i <= N; i++) { + dist[i] = INF; + nodeList[i] = new ArrayList<>(); + } + + 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 c = Integer.parseInt(st.nextToken()); + nodeList[a].add(new Node(b, c)); + nodeList[b].add(new Node(a, c)); + } + + st = new StringTokenizer(br.readLine()); + int s = Integer.parseInt(st.nextToken()); + int t = Integer.parseInt(st.nextToken()); + + dijkstra(s, t); + + bw.write(ans + "\n"); + bw.flush(); + bw.close(); + br.close(); + } + static void dijkstra(int start, int end) { + PriorityQueue pq = new PriorityQueue<>(); + pq.add(new Node(start, 0)); + dist[start] = 0; + + while(!pq.isEmpty()) { + Node curNode = pq.poll(); + int cur = curNode.to; + int cost = curNode.cost; + + for(Node next : nodeList[cur]) { + if(dist[next.to] > dist[cur] + next.cost) { + dist[next.to] = dist[cur] + next.cost; + pq.add(new Node(next.to, dist[next.to])); + } + } + } + ans = dist[end]; + } +} diff --git "a/junsu/week14/BOJ14719 \353\271\227\353\254\274/BOJ14719.java" "b/junsu/week14/BOJ14719 \353\271\227\353\254\274/BOJ14719.java" new file mode 100644 index 0000000..2001283 --- /dev/null +++ "b/junsu/week14/BOJ14719 \353\271\227\353\254\274/BOJ14719.java" @@ -0,0 +1,85 @@ +//BOJ14719 빗물, 골드5 +//구현, 시뮬레이션 문제 +//구현 문제라서 크게 피드백이 없다. +// 처음에 빈 공간과 이미 방문한 곳을 동시에 처리해서 에러가 발생했다. +// 빈 공간과 이미 방문한 곳을 따로 처리해줘야함 +import java.io.*; +import java.util.*; + +public class BOJ14719 { + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); + StringTokenizer st = new StringTokenizer(br.readLine()); + + int H = Integer.parseInt(st.nextToken()); + int W = Integer.parseInt(st.nextToken()); + int[][] map = new int[H+1][W+1]; + boolean[][] visited = new boolean[H+1][W+1]; + + st = new StringTokenizer(br.readLine()); + for(int i = 1; i <= W; i++) { + int num = Integer.parseInt(st.nextToken()); + int cnt = 0; + for(int j = H; j >= 1; j--) { + if(cnt++ == num) break; + map[j][i] = 1; + } + } + +// for(int i = 1; i <= H; i++) { +// for(int j = 1; j <= W; j++) { +// System.out.print(map[i][j] + " "); +// } +// System.out.println(); +// } + + int ans = 0; + int N, M, endN, endM; + Loop1 : + for(int i = 1; i <= H; i++) { + N = M = endN = endM = 0; + for(int j = 1; j <= W; j++) { + if(H == 1 && W == 1) { + break Loop1; + } + // 빈 공간 + if(map[i][j] == 0) continue; + + // 이미 방문한 곳은 N과 M 리셋해줘야함 + if(visited[i][j]) { + N = 0; + M = 0; + continue; + } + + //블록 처음 발견했을때 + if(N == 0 && M == 0) { + N = i; + M = j; + }else { //끝 블록 발견했을때 + endN = i; + endM = j; +// System.out.println("시작"+N+ " " + endN+ " "+M+" "+endM); + + //실수한 부분 : x, y 범위 설정을 잘못 생각함 + for(int x = i; x <= i; x++) { + for(int y = M + 1; y < endM; y++) { + visited[x][y] = true; + if(map[x][y] == 0) { +// System.out.println(x + " " + y); + ans++; + } + } + } + N = M = endN = endM = 0; + j--; + } + } + } + bw.write(ans+"\n"); + bw.flush(); + bw.close(); + br.close(); + } +} diff --git "a/junsu/week14/BOJ1504 \355\212\271\354\240\225\355\225\234 \354\265\234\353\213\250 \352\262\275\353\241\234/BOJ1504.java" "b/junsu/week14/BOJ1504 \355\212\271\354\240\225\355\225\234 \354\265\234\353\213\250 \352\262\275\353\241\234/BOJ1504.java" new file mode 100644 index 0000000..50b82d6 --- /dev/null +++ "b/junsu/week14/BOJ1504 \355\212\271\354\240\225\355\225\234 \354\265\234\353\213\250 \352\262\275\353\241\234/BOJ1504.java" @@ -0,0 +1,99 @@ +//BOJ1504 특정한 최단 경로, 골드4 +//다익스트라 +//에러 원인 1 - ans 누적합 과정에서 생겼다 +//두 가지 경로의 값을 비교할 때 각각 별도의 변수를 사용해 계산하고, 그 결과를 비교해야 합니다. +//예를 들어, 두 경로를 각각 따로 저장하고 마지막에 비교하는 방식으로 코드를 수정 +//에러 원인 2 - INF = 범위 설정 +// INF를 987654321 로 하면 89퍼 에러 발생한다. +// INF를 200000000 으로 하면 해결, 오버플로 이슈인듯 +import java.io.*; +import java.util.*; + +public class BOJ1504 { + static class Node implements Comparable { + int to; + int cost; + public Node(int to, int cost) { + this.to = to; + this.cost = cost; + } + @Override + public int compareTo(Node o) { + return this.cost - o.cost; + } + } + static int N, E, ans; + static int[] dist; + static int INF = 200000000; + static ArrayList[] nodeList; + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); + StringTokenizer st = new StringTokenizer(br.readLine()); + + N = Integer.parseInt(st.nextToken()); + E = Integer.parseInt(st.nextToken()); + dist = new int[N+1]; + nodeList = new ArrayList[N+1]; + for(int i = 1; i <= N; i++) { + nodeList[i] = new ArrayList<>(); + } + + for(int i = 0; i < E; i++) { + st = new StringTokenizer(br.readLine()); + int from = Integer.parseInt(st.nextToken()); + int to = Integer.parseInt(st.nextToken()); + int cost = Integer.parseInt(st.nextToken()); + nodeList[from].add(new Node(to, cost)); + nodeList[to].add(new Node(from, cost)); + } + st = new StringTokenizer(br.readLine()); + int v1 = Integer.parseInt(st.nextToken()); + int v2 = Integer.parseInt(st.nextToken()); + + int res1 = 0; + res1 += dijkstra(1, v1); + res1 += dijkstra(v1, v2); + res1 += dijkstra(v2, N); + + int res2 = 0; + res2 += dijkstra(1, v2); + res2 += dijkstra(v2, v1); + res2 += dijkstra(v1, N); + ans = Math.min(res1, res2); + + if(ans >= INF) { + ans = -1; + } + + bw.write(ans + "\n"); + bw.flush(); + bw.close(); + br.close(); + } + static int dijkstra(int start, int end) { + Arrays.fill(dist, INF); + + PriorityQueue pq = new PriorityQueue<>(); + pq.add(new Node(start, 0)); + dist[start] = 0; + + while(!pq.isEmpty()) { + Node curNode = pq.poll(); + int cur = curNode.to; + int cost = curNode.cost; + + for(Node next : nodeList[cur]) { + if(dist[next.to] > dist[cur] + next.cost) { + dist[next.to] = dist[cur] + next.cost; + pq.add(new Node(next.to, dist[next.to])); + } + } + } +// if(dist[end] != INF) { +// ans += dist[end]; +//// System.out.println(dist[end]); +// } + return dist[end]; + } +} diff --git "a/junsu/week14/BOJ1719 \355\203\235\353\260\260/BOJ1719.java" "b/junsu/week14/BOJ1719 \355\203\235\353\260\260/BOJ1719.java" new file mode 100644 index 0000000..51a58af --- /dev/null +++ "b/junsu/week14/BOJ1719 \355\203\235\353\260\260/BOJ1719.java" @@ -0,0 +1,63 @@ +//BOJ1719 택배, 골드3 +//플로이드 워샬 +//첫번째 방문 위치를 따로 저장해주면 된다. +import java.io.*; +import java.util.*; + +public class BOJ1719 { + static int n, m, ans; + static int INF = 987654321; + static int[][] map, res; + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); + StringTokenizer st = new StringTokenizer(br.readLine()); + + n = Integer.parseInt(st.nextToken()); + m = Integer.parseInt(st.nextToken()); + map = new int[n+1][n+1]; + res = new int[n+1][n+1]; + + for(int i = 1; i <= n; i++) { + for(int j = 1; j <= n; j++) { + map[i][j] = INF; + } + } + + for(int i = 0; i < m; i++) { + st = new StringTokenizer(br.readLine()); + int from = Integer.parseInt(st.nextToken()); + int to = Integer.parseInt(st.nextToken()); + int cost = Integer.parseInt(st.nextToken()); + map[from][to] = cost; + map[to][from] = cost; + res[from][to] = to; //초기화 같이해줘야한다. + res[to][from] = from; + } + + //플로이드 워샬 알고리즘 + for(int k = 1; k <= n; k++) { + for(int i = 1; i <= n; i++) { + for(int j = 1; j <= n; j++) { + if(map[i][j] > map[i][k] + map[k][j]) { + map[i][j] = map[i][k] + map[k][j]; +// res[i][j] = k; //이것도 가능 + res[i][j] = res[i][k]; //첫번째 방문 위치 저장 + } + } + } + } + + for(int i = 1; i <= n; i++) { + for(int j = 1; j <= n; j++) { + if(i == j) bw.write("- "); + else bw.write(res[i][j] + " "); + } + bw.write("\n"); + } + + bw.flush(); + bw.close(); + br.close(); + } +} diff --git "a/junsu/week14/BOJ1916 \354\265\234\354\206\214\353\271\204\354\232\251 \352\265\254\355\225\230\352\270\260/BOJ1916.java" "b/junsu/week14/BOJ1916 \354\265\234\354\206\214\353\271\204\354\232\251 \352\265\254\355\225\230\352\270\260/BOJ1916.java" new file mode 100644 index 0000000..b1f3aec --- /dev/null +++ "b/junsu/week14/BOJ1916 \354\265\234\354\206\214\353\271\204\354\232\251 \352\265\254\355\225\230\352\270\260/BOJ1916.java" @@ -0,0 +1,82 @@ +//BOJ1916 최소비용 구하기 +//다익스트라 알고리즘 +import java.io.*; +import java.util.*; + +public class BOJ1916 { + static class Node implements Comparable{ + int to; + int cost; + + public Node(int to, int cost) { + this.to = to; + this.cost = cost; + } + + @Override + public int compareTo(Node o) { + return this.cost - o.cost; + } + } + static int N, M; + static int[] dist; + static boolean[] visited; + static int INF = 987654321; + static ArrayList[] nodeList; + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); + StringTokenizer st; + + N = Integer.parseInt(br.readLine()); + M = Integer.parseInt(br.readLine()); + dist = new int[N+1]; + nodeList = new ArrayList[N+1]; + for(int i = 1; i <= N; i++) { + nodeList[i] = new ArrayList<>(); + dist[i] = INF; + } + + for(int i = 0; i < M; i++) { + st = new StringTokenizer(br.readLine()); + int from = Integer.parseInt(st.nextToken()); + int to = Integer.parseInt(st.nextToken()); + int cost = Integer.parseInt(st.nextToken()); + nodeList[from].add(new Node(to, cost)); + } + + + st = new StringTokenizer(br.readLine()); + int start = Integer.parseInt(st.nextToken()); + int end = Integer.parseInt(st.nextToken()); + +// //다익스트라 알고리즘 + PriorityQueue pq = new PriorityQueue<>(); + visited = new boolean[N+1]; + pq.add(new Node(start, 0)); + dist[start] = 0; + + while(!pq.isEmpty()) { + Node curNode = pq.poll(); + int cur = curNode.to; + int cost = curNode.cost; + + if(visited[cur]) continue; + visited[cur] = true; + + for(Node node : nodeList[cur]) { + if(dist[node.to] > dist[cur] + node.cost) { + dist[node.to] = dist[cur] + node.cost; + } + pq.add(new Node(node.to, dist[node.to])); + } + + } + +// System.out.println(Arrays.toString(dist)); + bw.write(dist[end]+"\n"); + bw.flush(); + bw.close(); + br.close(); + } +} diff --git "a/junsu/week14/BOJ5972 \355\203\235\353\260\260 \353\260\260\354\206\241/BOJ5972.java" "b/junsu/week14/BOJ5972 \355\203\235\353\260\260 \353\260\260\354\206\241/BOJ5972.java" new file mode 100644 index 0000000..d7cb9fe --- /dev/null +++ "b/junsu/week14/BOJ5972 \355\203\235\353\260\260 \353\260\260\354\206\241/BOJ5972.java" @@ -0,0 +1,74 @@ +//BOJ5972 택배 배송, 골드5 +//다익스트라 알고리즘 +import java.io.*; +import java.util.*; + +public class BOJ5972 { + static class Node implements Comparable{ + int to; + int cost; + + public Node(int to, int cost) { + this.to = to; + this.cost = cost; + } + + @Override + public int compareTo(Node o) { + return this.cost - o.cost; + } + } + static int N, M, ans; + static int[] dist; + static int INF = 987654321; + static ArrayList[] nodeList; + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); + StringTokenizer st = new StringTokenizer(br.readLine()); + N = Integer.parseInt(st.nextToken()); + M = Integer.parseInt(st.nextToken()); + ans = Integer.MAX_VALUE; + dist = new int[N+1]; + nodeList = new ArrayList[N+1]; + for(int i = 1; i <= N; i++) { + nodeList[i] = new ArrayList<>(); + dist[i] = INF; + } + + for(int i = 0; i < M; i++) { + st = new StringTokenizer(br.readLine()); + int from = Integer.parseInt(st.nextToken()); + int to = Integer.parseInt(st.nextToken()); + int cost = Integer.parseInt(st.nextToken()); + nodeList[from].add(new Node(to, cost)); + nodeList[to].add(new Node(from, cost)); //양방향이라서 둘 다 넣는다. + } + + dijkstra(1); + bw.write(ans+"\n"); + bw.flush(); + bw.close(); + br.close(); + } + static void dijkstra(int start) { + PriorityQueue pq = new PriorityQueue<>(); + pq.add(new Node(start, 0)); + dist[start] = 0; + + while(!pq.isEmpty()) { + Node curNode = pq.poll(); + int cur = curNode.to; + int cost = curNode.cost; + + for(Node next : nodeList[cur]) { + if(dist[next.to] > dist[cur] + next.cost) { + dist[next.to] = dist[cur] + next.cost; + //pq 넣어줄때 new Node로 넣는데 이때 실수하기 쉬움 + pq.add(new Node(next.to, dist[next.to])); + } + } + } + ans = dist[N]; + } +}