-
Notifications
You must be signed in to change notification settings - Fork 3
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
kimyeheun
wants to merge
9
commits into
main
Choose a base branch
from
05-kim
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+384
−0
Open
05 최단 경로 #28
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
801c674
Merge branch 'SSAFY-Coding-Test:main' into main
kimyeheun e525822
폴더 업로
kimyeheun 42f20ba
지름길_1446.java
kimyeheun 91a5a69
특정 거리의 도시 찾기_18352.java
kimyeheun 32e9b18
숨바꼭질3_13549.java
kimyeheun e2b5d0f
녹색 옷 입은 애가 젤다지?_4485.java
kimyeheun 584087a
서강그라운드_14938.java
kimyeheun 7f6a54f
택배_1719.java
kimyeheun 8e00d2e
Merge pull request #27 from kimyeheun/main
kimyeheun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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<>())) { | ||
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); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
해시맵에 getOrDefault가 있는지 처음 알았네요!! 이 함수를 쓴 이유가 무엇인지 알 수 있을까욥