๋ฐ์ํ
[BOJ] 1916๋ฒ ์ต์๋น์ฉ ๊ตฌํ๊ธฐ
https://www.acmicpc.net/problem/1916
๋ฌธ์ ์ ๊ทผ
์์ ๊ฐ์ค์น๋ฅผ ๊ฐ์ง ๊ฐ์ ์ผ๋ก ์ด๋ฃจ์ด์ง ๊ทธ๋ํ์์ ์ต๋จ ๊ฒฝ๋ก๋ฅผ ์ฐพ๋ ๋ฌธ์ ์ฌ์ ๋ค์ต์คํธ๋ผ์ ๋ฒจ๋งํฌ๋ ์๊ณ ๋ฆฌ์ฆ 2๊ฐ๋ฅผ ๋ชจ๋ ์ ์ฉํด์ ๊ตฌํํ๋ค.
๋ค์ต์คํธ๋ผ ์๊ณ ๋ฆฌ์ฆ์ ์ฐ์ ์์ ํ๋ฅผ ์ด์ฉํด์ ์๊ฐ๋ณต์ก๋๋ฅผ ์ค์๊ณ , ๋ค์ต์คํธ๋ผ์ ๋ฒจ๋งํฌ๋ ์๊ณ ๋ฆฌ์ฆ์ ํฌ์คํธ์ ์ ๋ฆฌํ๋ค.
[์๊ณ ๋ฆฌ์ฆ] ๋ค์ต์คํธ๋ผ & ๋ฒจ๋งํฌ๋
Code
๋ค์ต์คํธ๋ผ
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
class Node implements Comparable<Node> {
int to;
int cost;
public Node(int to, int cost) {
this.to = to;
this.cost = cost;
}
@Override
public int compareTo(Node o) {
return cost - o.cost;
}
}
public class BOJ1916 {
static int[] distance;
static ArrayList<ArrayList<Node>> list; // ์ธ์ ๋ฆฌ์คํธ
static boolean[] visited;
static int start, end;
static int n,m;
public static void main(String[] args) throws IOException {
initD();
System.out.println(dijkstra(start, end));
}
public static void initD() throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
n = Integer.parseInt(br.readLine());
m = Integer.parseInt(br.readLine());
distance = new int[n+1];
list = new ArrayList<>();
for(int i = 0; i<=n; i++){
list.add(i, new ArrayList<>());
}
for(int i = 0; i<m; i++){
StringTokenizer st = new StringTokenizer(br.readLine());
int from = Integer.parseInt(st.nextToken());
int to = Integer.parseInt(st.nextToken());
int cost = Integer.parseInt(st.nextToken());
list.get(from).add(new Node(to, cost));
}
StringTokenizer st = new StringTokenizer(br.readLine());
start = Integer.parseInt(st.nextToken());
end = Integer.parseInt(st.nextToken());
}
public static int dijkstra(int start, int end){
// ์ฐ์ ์์ ํ๋ฅผ ์ด์ฉํ ๋ค์ต์คํธ๋ผ ์๊ณ ๋ฆฌ์ฆ
PriorityQueue<Node> pq = new PriorityQueue<>();
// ์ต๋จ๊ฒฝ๋ก ํ
์ด๋ธ ์ด๊ธฐํ
Arrays.fill(distance, Integer.MAX_VALUE);
pq.offer(new Node(start, 0));
visited = new boolean[n + 1];
distance[start] = 0;
while(!pq.isEmpty()){
Node node = pq.poll();
int cur = node.to;
if(cur == end) break;
if(visited[cur]) continue;
visited[cur] = true;
for (Node next : list.get(cur)) {
if(!visited[next.to] && distance[next.to] > distance[cur] + next.cost){
distance[next.to] = distance[cur] + next.cost;
pq.add(new Node(next.to, distance[next.to]));
}
}
}
return distance[end];
}
}
๋ฒจ๋งํฌ๋
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
class Bus{
int from;
int to;
int cost;
public Bus(int from, int to, int cost) {
this.from = from;
this.to = to;
this.cost = cost;
}
}
public class BOJ1916 {
static Bus[] buses; // ๊ฐ์ ๋ค์ ์ ๋ณด๋ฅผ ์ ์ฅ
static int[] distance;
static boolean[] visited;
static int start, end;
static int n,m;
public static void main(String[] args) throws IOException {
initB();
System.out.println(bellmanFord(start));
}
public static void initB() throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
n = Integer.parseInt(br.readLine());
m = Integer.parseInt(br.readLine());
distance = new int[n + 1];
visited = new boolean[n + 1];
buses = new Bus[m];
for(int i = 0; i<m; i++){
StringTokenizer st = new StringTokenizer(br.readLine());
int from = Integer.parseInt(st.nextToken());
int to = Integer.parseInt(st.nextToken());
int cost = Integer.parseInt(st.nextToken());
Bus bus = new Bus(from, to, cost);
buses[i] = bus;
}
StringTokenizer st = new StringTokenizer(br.readLine());
start = Integer.parseInt(st.nextToken());
end = Integer.parseInt(st.nextToken());
}
public static int bellmanFord(int start){
// ์ต๋จ ๊ฒฝ๋ก ํ
์ด๋ธ ์ด๊ธฐํ
Arrays.fill(distance, Integer.MAX_VALUE);
distance[start] = 0;
for(int i = 1; i<=n; i++){
for(int j = 0; j<m; j++){
Bus bus = buses[j];
if(distance[bus.from] == Integer.MAX_VALUE) continue;
if(distance[bus.to] > distance[bus.from] + bus.cost){
distance[bus.to] = distance[bus.from] + bus.cost;
}
}
}
return distance[end];
}
}
๋ฐ์ํ
'Algorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Algorithm/Java][๋ฐฑ์ค] 2252๋ฒ ์ค ์ธ์ฐ๊ธฐ (0) | 2022.05.24 |
---|---|
[Algorithm/Java][๋ฐฑ์ค] 1197๋ฒ ์ต์ ์คํจ๋ ํธ๋ฆฌ (0) | 2022.05.22 |
[Algorithm/Java][๋ฐฑ์ค] 1806๋ฒ ๋ถ๋ถํฉ (0) | 2022.05.18 |
[Algorithm/Java][๋ฐฑ์ค] 1700๋ฒ ๋ฉํฐํญ ์ค์ผ์ค๋ง (0) | 2022.05.17 |
[Algorithm/Java][๋ฐฑ์ค] 1062๋ฒ ๊ฐ๋ฅด์นจ (0) | 2022.05.16 |