-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
141 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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,63 @@ | ||
package week14_0617; | ||
|
||
import java.io.*; | ||
import java.util.*; | ||
|
||
public class 최소비용구하기 { | ||
static class Node { | ||
int vertex; | ||
int dist; | ||
|
||
Node(int vertex, int dist) { | ||
this.vertex = vertex; | ||
this.dist = dist; | ||
} | ||
} | ||
|
||
public static void main(String[] args) throws IOException { | ||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | ||
int N = Integer.parseInt(br.readLine()); | ||
int M = Integer.parseInt(br.readLine()); | ||
|
||
ArrayList<ArrayList<Node>> graph = new ArrayList<>(); | ||
for (int i = 0; i <= N; i++) { | ||
graph.add(new ArrayList<>()); | ||
} | ||
|
||
for (int i = 0; i < M; i++) { | ||
StringTokenizer st = new StringTokenizer(br.readLine()); | ||
int start = Integer.parseInt(st.nextToken()); | ||
int end = Integer.parseInt(st.nextToken()); | ||
int cost = Integer.parseInt(st.nextToken()); | ||
|
||
graph.get(start).add(new Node(end, cost)); | ||
} | ||
|
||
StringTokenizer st = new StringTokenizer(br.readLine()); | ||
int start = Integer.parseInt(st.nextToken()); | ||
int end = Integer.parseInt(st.nextToken()); | ||
|
||
int[] dist = new int[N + 1]; | ||
Arrays.fill(dist, Integer.MAX_VALUE); | ||
dist[start] = 0; | ||
|
||
PriorityQueue<Node> pq = new PriorityQueue<>((o1, o2) -> o1.dist - o2.dist); | ||
pq.offer(new Node(start, 0)); | ||
|
||
while (!pq.isEmpty()) { | ||
Node curNode = pq.poll(); | ||
|
||
if (dist[curNode.vertex] < curNode.dist) continue; | ||
|
||
for (Node adjNode : graph.get(curNode.vertex)) { | ||
if (dist[adjNode.vertex] > curNode.dist + adjNode.dist) { | ||
dist[adjNode.vertex] = curNode.dist + adjNode.dist; | ||
|
||
pq.offer(new Node(adjNode.vertex, dist[adjNode.vertex])); | ||
} | ||
} | ||
} | ||
|
||
System.out.println(dist[end]); | ||
} | ||
} |
This file contains 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,76 @@ | ||
package week14_0617; | ||
|
||
import java.io.*; | ||
import java.util.*; | ||
|
||
public class 특정거리의도시찾기 { | ||
static class Node { | ||
int vertex; | ||
int dist; | ||
|
||
Node(int vertex, int dist) { | ||
this.vertex = vertex; | ||
this.dist = dist; | ||
} | ||
} | ||
|
||
public static void main(String[] args) throws IOException { | ||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | ||
StringBuilder sb = new StringBuilder(); | ||
StringTokenizer st; | ||
|
||
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()); // 출발 도시 번호 | ||
|
||
ArrayList<ArrayList<Node>> graph = new ArrayList<>(); | ||
|
||
for (int i = 0; i <= N; i++) { | ||
graph.add(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()); | ||
|
||
graph.get(a).add(new Node(b, 1)); | ||
} | ||
|
||
int[] dist = new int[N + 1]; | ||
|
||
Arrays.fill(dist, Integer.MAX_VALUE); | ||
dist[X] = 0; | ||
|
||
PriorityQueue<Node> pq = new PriorityQueue<>((o1, o2) -> o1.dist - o2.dist); | ||
pq.add(new Node(X, 0)); | ||
|
||
while (!pq.isEmpty()) { | ||
Node curNode = pq.remove(); | ||
|
||
if (dist[curNode.vertex] < curNode.dist) continue; | ||
|
||
for (Node adjNode : graph.get(curNode.vertex)) { | ||
if (dist[adjNode.vertex] > curNode.dist + adjNode.dist) { | ||
dist[adjNode.vertex] = curNode.dist + adjNode.dist; | ||
|
||
pq.add(new Node(adjNode.vertex, dist[adjNode.vertex])); | ||
} | ||
} | ||
} | ||
|
||
for (int i = 1; i < dist.length; i++) { | ||
if (dist[i] == K) { | ||
sb.append(i).append("\n"); | ||
} | ||
} | ||
|
||
if (sb.length() == 0) { | ||
System.out.println(-1); | ||
} else { | ||
System.out.println(sb); | ||
} | ||
} | ||
} |