diff --git a/src/programmers/algorithm/dfsbfs/Q132266.java b/src/programmers/algorithm/dfsbfs/Q132266.java new file mode 100644 index 0000000..d177bf9 --- /dev/null +++ b/src/programmers/algorithm/dfsbfs/Q132266.java @@ -0,0 +1,55 @@ +package programmers.algorithm.dfsbfs; + +import java.util.*; + +class Q132266 { + static Queue q; + static int[] distance; + static int des; + static List[] graph; + static int ans; + public int[] solution(int n, int[][] roads, int[] sources, int destination) { + int[] answer = new int[sources.length]; + + graph = new ArrayList[n+1]; + for(int i=1;i(); + } + + for(int[] road: roads){ + int from = road[0]; + int to = road[1]; + + graph[from].add(to); + graph[to].add(from); + } + + q = new LinkedList<>(); + distance = new int[n+1]; + Arrays.fill(distance,-1); + + q.offer(destination); + distance[destination] = 0; + + bfs(); + + for(int i=0;i pq; + static List[] graph; + static int[] distance; + public int solution(int n, int s, int a, int b, int[][] fares) { + graph = new ArrayList[n+1]; + for(int i=0;i(); + } + for(int[] fare:fares){ + int from = fare[0]; + int to = fare[1]; + int cost = fare[2]; + + graph[from].add(new int[]{to,cost}); + graph[to].add(new int[]{from,cost}); + } + + int answer = Integer.MAX_VALUE; + + // S -> K + K -> A + K -> B + // S -> K + A -> K + B -> K + + int[] disS = dijkstra(n,s); // S 시작 + int[] disA = dijkstra(n,a); // A 시작 + int[] disB = dijkstra(n,b); // B 시작 + + for(int k=1;k pq = + new PriorityQueue<>((x, y) -> Integer.compare(x[1], y[1])); + + distance[start] = 0; + pq.offer(new int[]{start, 0}); + + while(!pq.isEmpty()){ + int[] cur = pq.poll(); + int now = cur[0]; + int cost = cur[1]; + + if(cost > distance[now]){ + continue; + } + + for(int[] nexts : graph[now]){ + int next = nexts[0]; + int nextCost = nexts[1]; + + if(distance[next] > distance[now] + nextCost){ + distance[next] = distance[now] + nextCost; + + pq.offer(new int[]{next,distance[next]}); + } + } + } + return distance; + } + +} \ No newline at end of file diff --git a/src/programmers/algorithm/floydwarshall/Q72413.java b/src/programmers/algorithm/floydwarshall/Q72413.java new file mode 100644 index 0000000..4784be5 --- /dev/null +++ b/src/programmers/algorithm/floydwarshall/Q72413.java @@ -0,0 +1,41 @@ +package programmers.algorithm.floydwarshall; + +import java.util.*; + +class Q72413 { + public int solution(int n, int s, int a, int b, int[][] fares) { + int INF = 1000000000; + int[][] dp = new int[n+1][n+1]; + for(int i=1;i result = new ArrayList<>(); + for(int i=0;i0 && bar[x-1][y]) + || (y>0 && pillar[x][y-1]))){ + return false; + } + } + + // 보 + if(bar[x][y]){ + // 한쪽 끝이 기둥 위 or 양쪽 끝이 다른 보와 연결 + if (!((y > 0 && pillar[x][y - 1]) + || (x 0 && pillar[x + 1][y - 1]) + || (x > 0 && x