๐ ๋ฌธ์ ๋งํฌ
๐ ํ์ด ๊ณผ์
- ๋ค์ต์คํธ๋ผ ์๊ณ ๋ฆฌ์ฆ ์ ํ ๋ฌธ์ ์
๋๋ค.
- ๋ค์ต์คํธ๋ผ ์๊ณ ๋ฆฌ์ฆ ๊ฐ๋
์ ์ดํดํ๊ณ ์ฒ์ ์ ํ ๋ ๊ธฐ๋ณธ๊ธฐ๋ฅผ ์ตํ๊ธฐ ์ข์ ๋ฌธ์ ์
๋๋ค.
- ๋ฐ๋ก ํ์ด๋ ์์ฑํ์ง ์๊ฒ ์ต๋๋ค. ์ฃผ์๊ณผ ์ฝ๋๋ฅผ ๋ฐ๋ผ๊ฐ์๋ฉด ์ถฉ๋ถํ ์ดํดํ์ค ์ ์์ ๊ฒ์ด๋ผ ์๊ฐํฉ๋๋ค
- ์ด๋ฒ ๋ฌธ์ ๋ ์ฝ๋๋ง ๋ด์ฃผ์๋ฉด ์ข์ ๊ฒ ๊ฐ์์!
๐ป ์ฝ๋
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.PriorityQueue;
import java.util.StringTokenizer;
public class Main {
static int v,e,k;
static int[] dist;
static List<Point>[] list;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine()," ");
v = Integer.parseInt(st.nextToken()); // ์ ์ ์ ๊ฐ์
e = Integer.parseInt(st.nextToken()); // ๊ฐ์ ์ ๊ฐ์
k = Integer.parseInt(br.readLine()); // ์์ ์ ์ ์ ๋ฒํธ
list = new ArrayList[v + 1];
dist = new int[v + 1];
Arrays.fill(dist,Integer.MAX_VALUE); // ์ด๊ธฐํ
for (int i = 1; i < v + 1; i++) {
list[i] = new ArrayList<>();
}
for (int i = 0; i < e; i++) {
st = new StringTokenizer(br.readLine());
int start = Integer.parseInt(st.nextToken());
int end = Integer.parseInt(st.nextToken());
int weight = Integer.parseInt(st.nextToken());
list[start].add(new Point(end,weight)); // start์์ end๋ก ๊ฐ๋ ๊ฐ์ค์น
}
StringBuilder sb = new StringBuilder();
solve(k);
for (int i = 1; i < v + 1; i++) {
if (dist[i] == Integer.MAX_VALUE) {
sb.append("INF\n");
} else {
sb.append(dist[i])
.append("\n");
}
}
System.out.println(sb);
}
private static void solve(int start){
PriorityQueue<Point> queue = new PriorityQueue<>();
boolean[] visited = new boolean[v + 1];
queue.offer(new Point(start, 0));
dist[start] = 0;
PriorityQueue<Point> queue1 = new PriorityQueue<>(Collections.reverseOrder());
while(!queue.isEmpty()){
Point nowPoint = queue.poll();
int cur = nowPoint.end;
if(visited[cur]){
continue;
}
visited[cur] = true;
for(Point point : list[cur]){
if(dist[point.end] > dist[cur] + point.weight){
dist[point.end] = dist[cur] + point.weight;
queue.offer(new Point(point.end, dist[point.end]));
}
}
}
} // ๋ค์ต์คํธ๋ผ ์๊ณ ๋ฆฌ์ฆ
static class Point implements Comparable<Point>{
int end;
int weight;
Point(int end, int weight) {
this.weight =weight;
this.end =end;
}
@Override
public int compareTo(Point point){
return this.weight - point.weight;
}
}
}
'Algorithm > ๋ฐฑ์ค' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๋ฐฑ์ค 1991๋ฒ : ํธ๋ฆฌ ์ํ (Java) (0) | 2022.02.12 |
---|---|
๋ฐฑ์ค 11725๋ฒ : ํธ๋ฆฌ์ ๋ถ๋ชจ ์ฐพ๊ธฐ (Java) (0) | 2022.02.12 |
๋ฐฑ์ค 2206๋ฒ : ๋ฒฝ ๋ถ์๊ณ ์ด๋ํ๊ธฐ (Java) (0) | 2022.02.12 |
๋ฐฑ์ค 11660๋ฒ : ๊ตฌ๊ฐ ํฉ ๊ตฌํ๊ธฐ 5(Java) (0) | 2022.01.17 |
๋ฐฑ์ค 1149๋ฒ : RGB๊ฑฐ๋ฆฌ (Java) (0) | 2022.01.17 |