ํฉ์น ํ์ ์๊ธ
๋ ์ฌ๋(A์ B)์ด ๊ฐ์ ์ถ๋ฐ์ง S์์ ์ถ๋ฐํด์ ๊ฐ์์ ๋ชฉ์ ์ง A, B๊น์ง ์ด๋ํด์ผ ํ๋ค.
๋ชจ๋ ์ง์ ๊ฐ ์ด๋ ๋น์ฉ์ด ์ฃผ์ด์ง ๊ทธ๋ํ๊ฐ ์์ ๋,
์ค๊ฐ ์ง์ ๊น์ง ํฉ์น ํ ๊ฐ๋ผ์ง๊ธฐ
ํน์ ์์ ์ฒ์๋ถํฐ ๋ฐ๋ก ์ด๋ํ๊ธฐ
์ด ๋ ์ค ๊ฐ์ฅ ๋น์ฉ์ด ์ ๊ฒ ๋๋ ๊ฒฝ์ฐ์ ์ด ํ์์๊ธ์ ๊ตฌํ๋ ๋ฌธ์ .
์ต์ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ๋ ๊ทธ๋ํ ๋ฌธ์ ์ด๊ณ , ํฉ์น ์ข
๋ฃ ์ง์ ์ด๋ผ๋ '์ค๊ฐ ์ง์ '์ด ์๊ธฐ ๋๋ฌธ์,
๋ชจ๋ ๋
ธ๋๊น์ง์ ์ต๋จ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ๋ ๋ค์ต์คํธ๋ผ ์๊ณ ๋ฆฌ์ฆ์ด ์ ํฉํ๋ค๊ณ ์๊ฐํ๋ค.
๋ฌธ์ ์์ ์ถ๋ฐ์ง๋ A, B๋ก ๋ ๊ฐ์ด๊ณ
๋์ฐฉ์ง์ ์ S๋ก ํ๋์ด๋ค.
๊ทธ๋์ ๋์ฐฉ์ง์ S์์ ์ถ๋ฐํ์ฌ A, B๊น์ง ๊ฐ๋ ์ต์ ๋น์ฉ์ ๊ฐ๊ฐ ๊ตฌํ๋ฉด ๋ ๊ฒ์ด๋ผ๊ณ ์๊ฐํ๋ค.
์๋์ ๊ฐ์ด ์ด 3๋ฒ ๋ค์ต์คํธ๋ผ๋ฅผ ๋๋ฆฐ๋ค
1.s์์ ๋ชจ๋ ์ง์ ๊น์ง ์ต์ ๋น์ฉ ๋ฐฐ์ด -> dist[]
2.s์์ A๊น์ง ์ต์ ๋น์ฉ ๊ตฌํจ -> dist[]
3.s์์ A๊น์ง ์ต์ ๋น์ฉ ๊ตฌํจ -> dist[]
๊ทธ๋ฆฌ๊ณ ํฉ์นํ๋ ๊ฒฝ์ฐ์ ์ต์ ๋น์ฉ๊ณผ ๋ฐ๋ก ๊ฐ๋ ๊ฒฝ์ฐ์ ์ต์ ๋น์ฉ์ ๊ตฌํ๋ค์, ๋ ์ค ์์ ๊ฐ์ ๊ตฌํ๋ค.
gpt์๊ฒ ๋ฌผ์ด๋ณด๋ ๋ด๊ฐ ์ค๊ณํ ๊ฒ์ฒ๋ผ 3๋ฒ ๋ค์ต์คํธ๋ผ ๋ฐฐ์ด์ ๋ง๋๋ ๊ฒ์ ๊ฐ์ง๋ง, ๊ฒฐ๊ณผ๋ฅผ ํจ์ฌ ๊ฐ๋จํ๊ฒ ๊ตฌํ ์ ์๋ค๊ณ ํ๋ค.
์๋์ ๊ฐ์ด ์ด 3๊ฐ์ ๋ค์ต์คํธ๋ผ๋ฅผ ๋๋ฆฐ๋ค.
1.s์์ ๋ชจ๋ ์ง์ ๊น์ง ์ต์ ๋น์ฉ ๋ฐฐ์ด -> dist[]
2.A์์ ๋ชจ๋ ์ง์ ๊น์ง ์ต์ ๋น์ฉ ๋ฐฐ์ด -> dist[]
3.B์์ ๋ชจ๋ ์ง์ ๊น์ง ์ต์ ๋น์ฉ ๋ฐฐ์ด -> dist[]
๊ทธ๋ฆฌ๊ณ ์๋์ ๊ฐ์ด K๋ฅผ ํ์น์ง์ ์ผ๋ก ๋๋ฆฌ๊ณ ์ต์ ๋น์ฉ์ ๊ตฌํ๋ฉด ๋์ด๋ค.
answer = min( distS[k] + distA[k] + distB[k] ) for k = 1..n
์ ๋ฆฌํ์๋ฉด, ๋ชจ๋ ์ ์ ํ์นํ๋ณด๋ก ๋๊ณ ์ต์ ๋น์ฉ์ด ๋์ค๋ ํ์น ์ง์ ์ ๊ตฌํ๋ฉด ๋๋ค.
๋น์ฉ์ด ์์ ์์๋๋ก ์ ๋ ฌํ ๊ฒ์ด๊ธฐ ๋๋ฌธ์ compareTo๋ ๋ง๋ค์ด ์ค๋ค.
class Node implements Comparable<Node> {
int to;
int cost;
Node (int to, int cost) {
this.to = to;
this.cost = cost;
}
@Override
public int compareTo(Node o){
return this.cost - o.cost;
}
}
List<List<Node>> graph = new ArrayList<>();
for (int i = 0; i <= n; i++) {
graph.add(new ArrayList<>());
}
//์๋ฐฉํฅ์ผ๋ก ๊ทธ๋ํ ๊ตฌ์ฑ
for (int [] f : fares){
int from = f[0];
int to = f[1];
int cost = f[2];
graph.get(from).add(new Node(to, cost)));
graph.get(to).add(new Node(from, cost)));
}
private int[] dijkstra(int strat, int n, List<List<Node>> graph){
//์ฐ์ ์์ ํ ์์ฑ
PriorityQueue<Node> pq = new PriorityQueue<>();
//์ต์ ๊ฑฐ๋ฆฌ๋ฅผ ์ ์ฅํ ๊ฑฐ๋ฆฌ ๋ฐฐ์ด ์์ฑ
int[] dist = new int[n + 1];
//์ด๊ธฐ๊ฐ์ ์ต๋๊ฐ์ผ๋ก ์ฑ์ฐ๊ธฐ
Arrays.fill(dist.INF);
//์์์ง์ ์ ๊ฑฐ๋ฆฌ๋ 0์ผ๋ก
dist[start] = 0;
//์์์ง์ ์ ๊ฐ์ฒด ๋ฃ๊ธฐ
pq.offer(new Node(start, 0));
while(!pq.isEmpty()){
Node now = pq.poll();
//ํ์ฌ์ ๊ฐ์ด ๋ฐฐ์ด์ ์ ์ฅ๋ ๊ฐ๋ณด๋ค ํฌ๋ฉด ๋์ด๊ฐ๊ธฐ
//now.cost : ํ์ฌ ์ง์ ์์ now.to๊น์ง์ ๋น์ฉ (๋ฐ๋ก ์ด์ด์ง ๊ฒฝ์ฐ์ ๋น์ฉ)
//dist[now.to] : ๋ฐฐ์ด์ ์ ์ฅ๋ ๋น์ฉ
if (now.cost > dist[now.to]) continue;
//ํ์ฌ ์ง์ ๊ณผ ์ฐ๊ฒฐ๋ ๋ชจ๋ ์ง์ ์ ํ์ํ๊ธฐ
for (Node next : graph.get(now.to)){
//ํ์ฌ๊น์ง ์ค๋ ๋ฐ ๋ ๋น์ฉ + ์ธ์ ๋
ธ๋๋ก ๊ฐ๋ ๋น์ฉ = ์๋ก์ด ํ๋ณด ๋น์ฉ
int newCost = now.cost + next.cost;
//๋ง์ฝ ํ์ฌ ์๊ณ ์๋ ์ต๋จ๊ฑฐ๋ฆฌ๋ณด๋ค ์๋ก ๊ณ์ฐํ ๋น์ฉ์ด ๋ ์๋ค๋ฉด
if (newCost < dist[next.to]){
//์ต๋จ ๊ฑฐ๋ฆฌ ์
๋ฐ์ดํธ๋ฅผ ํ๋ค.
dist[next.to] = newCost;
pq.offer(new Node(next.to, newCost));
}
}
}
return dist;
}
int[] distS = dijkstra(s, n, graph);
int[] distA = dijkstra(a, n, graph);
int[] distB = dijkstra(b, n, graph);
int answer = INF;
// ์ค๊ฐ ํฉ์น ์ง์ k(1~n) ํ์
for (int k = 1; k <= n; k++) {
long total = (long)distS[k] + distA[k] + distB[k];
answer = (int)Math.min(answer, total);
}
return answer;
์ด ๋ฌธ์ ๋ฅผ ํ๋ฉด์ ๋ค์ต์คํธ๋ผ๋ฅผ ์์ ํ ์ดํดํ์ง ๋ชปํ๋ค๋ ๊ฒ์ ๊นจ๋ซ๊ณ ... ์ฝ๋ ํ๋ฆ์ ๋ค์ ์ ๋ฆฌํด๋ณด์๋ค.
๋ค์ต์คํธ๋ผ ์ ๋ฆฌ
import java.util.*; //import ์จ์ฃผ๊ธฐ
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 this.cost - o.cost;
}
}
class Solution {
static final int INF = Integer.MAX_VALUE; //์ด๊ฒ๋ ์ฒดํฌ
static List<List<Node>> graph;
static int N;
public int solution(int n, int s, int a, int b, int[][] fares) {
N = n;
graph = new ArrayList<>();
for (int i = 0; i <= n; i ++) {
graph.add(new ArrayList<>());
}
for (int i = 0; i < fares.length; i++) {
int from = fares[i][0];
int to = fares[i][1];
int cost = fares[i][2];
graph.get(from).add(new Node(to, cost));
graph.get(to).add(new Node(from, cost));
}
//s -> ๋ชจ๋ ์ง์ ์ ์ต๋จ๊ฑฐ๋ฆฌ
//a -> ๋ชจ๋ ์ง์
//b -> ๋ชจ๋ ์ง์
int[] dist_s = dijstra(s);
int[] dist_a = dijstra(a);
int[] dist_b = dijstra(b);
int min_fare = INF;
for (int k = 1; k <= n ; k ++) {
min_fare = Math.min(min_fare, dist_s[k] + dist_a[k] + dist_b[k]);
}
return min_fare;
}
//start์์ ๋ชจ๋ ์ง์ ๊น์ง์ ์ต์๊ฑฐ๋ฆฌ
public int[] dijstra(int start) {
PriorityQueue<Node> pq = new PriorityQueue<>();
int[] dist = new int[N + 1];
Arrays.fill(dist, INF); //์ด๊ฒ๋ ํ์ธ Array.๋ฅผ ์จ์ผ ํ๋ค
dist[start] = 0;
pq.offer(new Node(start, 0));
while(!pq.isEmpty()) {
Node curr = pq.poll();
if (curr.cost > dist[curr.to]) continue;
for (Node next : graph.get(curr.to)) {
int new_cost = curr.cost + next.cost;
if (new_cost < dist[next.to]) { //์กฐ๊ฑด์ ๊ฑธ์ด์ ๋ง์กฑํ ๊ฒฝ์ฐ์๋ง ํ์ ๋ฃ์ด์ผ ํจ
dist[next.to] = new_cost;
pq.offer(new Node(next.to, new_cost));
}
}
}
return (dist);
}
}