Skip to content

05 최단 경로 #28

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

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
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
61 changes: 61 additions & 0 deletions 05_shortest_path/kim/13549.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import java.io.*;
import java.util.*;

public class Main {
static class Node {
int time;
int now;

Node(int time, int now) {
this.time = time;
this.now = now;
}
}

public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());

int N = Integer.parseInt(st.nextToken());
int K = Integer.parseInt(st.nextToken());

if (N >= K) {
System.out.println(N - K);
return;
}

int[] go = new int[140_001];
for (int i = 0; i < go.length; i++) {
go[i] = Integer.MAX_VALUE;
}

PriorityQueue<Node> queue = new PriorityQueue<>((s, e) -> Integer.compare(s.time, e.time));
queue.add(new Node(0, N));

while (!queue.isEmpty()) {
Node now = queue.poll();
int nnode = now.now;
if (nnode == K) {
System.out.println(now.time);
return;
}

int[] move = { nnode + 1, nnode - 1 };
for (int next : move) {
if (next < 0 || next > 140_000) continue;
if (go[next] > now.time + 1 && now.time + 1 <= (K-N)) {
queue.add(new Node(now.time + 1, next));
go[next] = now.time + 1;
}
}

if (nnode * 2 <= 0 || nnode * 2 > 140_000) continue;
else {
if (go[nnode * 2] > now.time && now.time <= (K-N)) {
queue.add(new Node(now.time, nnode * 2));
go[nnode * 2] = now.time;
}
}
}
}
}
56 changes: 56 additions & 0 deletions 05_shortest_path/kim/1446.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import java.io.*;
import java.util.*;


public class Main {
static class Node{
int s;
int e;
int w;
Node(int s, int e, int w) {
this.s = s;
this.e = e;
this.w = w;
}
}

public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int N = Integer.parseInt(st.nextToken());
int D = Integer.parseInt(st.nextToken());

int[] dp = new int[10_001];
Arrays.fill(dp, Integer.MAX_VALUE);
dp[0] = 0;

Set<Integer> set = new HashSet<>();
List<Node> list = new ArrayList<>();

for(int n = 0; n < N; n++) {
st = new StringTokenizer(br.readLine());

int s = Integer.parseInt(st.nextToken());
int e = Integer.parseInt(st.nextToken());
int w = Integer.parseInt(st.nextToken());

if (e > D) continue;

list.add(new Node(s, e, w));

set.add(s);
set.add(e);
}

for(int i = 0; i < 10_000; i++) {
dp[i + 1] = Math.min(dp[i+1], dp[i] + 1);

for(Node now : list) {
if(now.s == i)
dp[now.e] = Math.min(dp[i] + now.w, dp[now.e]);
}
}

System.out.println(dp[D]);
}
}
68 changes: 68 additions & 0 deletions 05_shortest_path/kim/14938.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import java.io.*;
import java.util.*;


public class Main {

public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();

StringTokenizer st = new StringTokenizer(br.readLine());
int N = Integer.parseInt(st.nextToken());
int M = Integer.parseInt(st.nextToken());
int R = Integer.parseInt(st.nextToken());

int[] items = new int[N];
boolean[] visited = new boolean[N];
int[][] map = new int[N+1][N+1];

// 아이템 초기화
st = new StringTokenizer(br.readLine());
for(int n = 0; n < N; n++)
items[n] = Integer.parseInt(st.nextToken());

// 맵 초기화
for(int i = 1; i <= N; i++) {
for(int j = 1; j <= N; j++) {
if(i == j) continue;
map[i][j] = Integer.MAX_VALUE;
}
}

for(int r = 0; r < R; r++) {
st = new StringTokenizer(br.readLine());
int s = Integer.parseInt(st.nextToken());
int e = Integer.parseInt(st.nextToken());
int w = Integer.parseInt(st.nextToken());
if (w > M) continue;
map[s][e] = w;
map[e][s] = w;
}

for(int m = 1; m <= N; m++) {
for(int s = 1; s <= N; s++) {
for(int e =1; e <= N; e++) {
if (map[s][m] == Integer.MAX_VALUE || map[m][e] == Integer.MAX_VALUE) continue;
if (map[s][m] + map[m][e] > M) continue;

map[s][e] = Math.min(map[s][e], map[s][m] + map[m][e]);
}
}
}

int max = 0;
for(int i = 1; i <= N; i++) {
int buf = 0;
Set<Integer> set = new HashSet<>();

for(int j = 1; j <= N; j++) {
if (map[i][j] != Integer.MAX_VALUE) set.add(j);
}
for(int s : set) buf += items[s-1];
max = Math.max(max, buf);
}

System.out.println(max);
}
}
85 changes: 85 additions & 0 deletions 05_shortest_path/kim/1719.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import java.io.*;
import java.util.*;


public class Main {
static int N;
static HashMap<Integer, List<Node>> routes;
static StringBuilder sb = new StringBuilder();

static class Node {
int node;
int dist;
int route;

Node(int node, int dist, int route) {
this.node = node;
this.dist = dist;
this.route = route;
}

@Override
public String toString() {
return "{node} " + this.node + " " + this.dist + " " + this.route;
}
}

public static void dijkstra(int startNode) {
PriorityQueue<Node> queue = new PriorityQueue<>((s, e) -> s.dist- e.dist);

int[] path = new int[N+1];
path[startNode] = startNode;
for(Node n : routes.getOrDefault(startNode, new ArrayList<>())) {
queue.add(new Node(n.node, n.dist, n.route));
}

while (!queue.isEmpty()) {
Node now = queue.poll();
if (path[now.node] != 0) continue;
path[now.node] = now.route;
for(Node n : routes.getOrDefault(now.node, new ArrayList<>())) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

해시맵에 getOrDefault가 있는지 처음 알았네요!! 이 함수를 쓴 이유가 무엇인지 알 수 있을까욥

if (path[n.node] == 0) {
queue.add(new Node(n.node, now.dist + n.dist, now.route));
}
}
}

for(int i = 1; i <= N; i++) {
if (i == startNode) sb.append("- ");
else sb.append(path[i]).append(" ");
}
sb.append("\n");

}

public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());

N = Integer.parseInt(st.nextToken());
int M = Integer.parseInt(st.nextToken());

routes = new HashMap<>();

for(int m = 0; m < M; m++) {
st = new StringTokenizer(br.readLine());
int s = Integer.parseInt(st.nextToken());
int e = Integer.parseInt(st.nextToken());
int w = Integer.parseInt(st.nextToken());

if (!routes.containsKey(s))
routes.put(s, new ArrayList<>());
if (!routes.containsKey(e))
routes.put(e, new ArrayList<>());

routes.get(s).add(new Node(e, w, e));
routes.get(e).add(new Node(s, w, s));
}

for(int i = 1; i <= N; i++) {
dijkstra(i);
}

System.out.println(sb);
}
}
56 changes: 56 additions & 0 deletions 05_shortest_path/kim/18352.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import java.io.*;
import java.util.*;


public class Main {
public static void main(String[] args) throws Exception {

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());

int N = Integer.parseInt(st.nextToken()); // 도시 개수
int M = Integer.parseInt(st.nextToken()); // 도로 개수
int K = Integer.parseInt(st.nextToken()); // 거리 정보
int X = Integer.parseInt(st.nextToken()); // 출발 도시

Map<Integer, List<Integer>> roads = new HashMap<>();

for(int i = 0; i < M; i++) {
st = new StringTokenizer(br.readLine());
int s = Integer.parseInt(st.nextToken());
int e = Integer.parseInt(st.nextToken());

List<Integer> buf = roads.getOrDefault(s, new ArrayList<>());
buf.add(e);
roads.put(s, buf);
}

int[] minDist = new int[N+1];
boolean[] visited = new boolean[N+1];

Queue<Integer> queue = new LinkedList<>();
queue.add(X);
visited[X] = true;
while(!queue.isEmpty()) {
int now = queue.poll();
for(int next : roads.getOrDefault(now, new ArrayList<>())) {
if (!visited[next]) {
minDist[next] = minDist[now] + 1;
queue.add(next);
visited[next] = true;
}
}
}

StringBuilder sb = new StringBuilder();
boolean isAns = false;
for(int i = 1; i <= N; i++) {
if(minDist[i] == K) {
sb.append(i).append("\n");
isAns = true;
}
}

System.out.println(isAns ? sb : -1);
}
}
58 changes: 58 additions & 0 deletions 05_shortest_path/kim/4485.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import java.io.*;
import java.util.*;


public class Main {

static int[][] move = {{1,0}, {0,1}, {-1, 0}, {0, -1}};

public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();

int test = 1;

while(true) {
int N = Integer.parseInt(br.readLine());
if (N == 0) break;

int[][] map = new int[N][N];
int[][] result = new int[N][N];
boolean[][] visited = new boolean[N][N];

for(int i = 0; i < N; i++) {
StringTokenizer st = new StringTokenizer(br.readLine());
for(int j = 0; j <N; j++) {
int num = Integer.parseInt(st.nextToken());
map[i][j] = num;
result[i][j] = num;
}
}

Queue<int[]> queue = new LinkedList<>();
queue.add(new int[] {0, 0});
visited[0][0] = true;
result[0][0] = map[0][0];

while(!queue.isEmpty()) {
int[] now = queue.poll();

for(int[] mv : move) {
int nextn = now[0] + mv[0];
int nextm = now[1] + mv[1];

if(nextn < 0 || nextm < 0 || nextn >= N || nextm >= N) continue;
if(visited[nextn][nextm] && result[nextn][nextm] <= result[now[0]][now[1]] + map[nextn][nextm]) continue;

result[nextn][nextm] = result[now[0]][now[1]] + map[nextn][nextm];
visited[nextn][nextm] = true;
queue.add(new int[] {nextn, nextm});
}
}

sb.append("Problem ").append(test++).append(": ").append(result[N-1][N-1]).append("\n");
}

System.out.println(sb);
}
}