Algorithm Test/백준

[백준] 1504. 특정한 최단 경로 (JAVA)

김맷돌 2021. 5. 7. 18:45
반응형

🗺 특정한 최단 경로

 

다익스트라 알고리즘을 응용한 문제 

 


문제

방향성이 없는 그래프가 주어진다. 세준이는 1번 정점에서 N번 정점으로 최단 거리로 이동하려고 한다. 또한 세준이는 두 가지 조건을 만족하면서 이동하는 특정한 최단 경로를 구하고 싶은데, 그것은 바로 임의로 주어진 두 정점은 반드시 통과해야 한다는 것이다.

 

세준이는 한번 이동했던 정점은 물론, 한번 이동했던 간선도 다시 이동할 수 있다. 하지만 반드시 최단 경로로 이동해야 한다는 사실에 주의하라. 1번 정점에서 N번 정점으로 이동할 때, 주어진 두 정점을 반드시 거치면서 최단 경로로 이동하는 프로그램을 작성하시오.

 

입력

첫째 줄에 정점의 개수 N과 간선의 개수 E가 주어진다. (2 ≤ N ≤ 800, 0 ≤ E ≤ 200,000) 둘째 줄부터 E개의 줄에 걸쳐서 세 개의 정수 a, b, c가 주어지는데, a번 정점에서 b번 정점까지 양방향 길이 존재하며, 그 거리가 c라는 뜻이다. (1 ≤ c ≤ 1,000) 다음 줄에는 반드시 거쳐야 하는 두 개의 서로 다른 정점 번호 v1과 v2가 주어진다. (v1 ≠ v2, v1 ≠ N, v2 ≠ 1)

 

출력

첫째 줄에 두 개의 정점을 지나는 최단 경로의 길이를 출력한다. 그러한 경로가 없을 때에는 -1을 출력한다.

 


🔑 IDEA

v1, v2를 무조건 지나면서 start에서 end까지 가는 방법에는 다음 두 가지가 있다.

 

1. start v1 v2 → end

2. start v2 v1 end

 

다익스트라를 이용해 각각의 최소비용을 찾고, 더 작은 값을 리턴한다.  

 

주의할 점 

  • INF = 200,000,000 (거리 최댓값 == 1,000, 간선 개수 최댓값 == 200,000 이므로)
  • 한번 이동했던 정점으로 다시 이동할 수 있다 (방문 체크를 하지 않는다)

 

💡  나의 풀이

import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;

class Edge implements Comparable<Edge> {
	int vertex;
	int cost;

	Edge(int vertex, int cost) {
		this.vertex = vertex;
		this.cost = cost;
	}

	@Override
	public int compareTo(Edge edge) {
		return this.cost - edge.cost;
	}
}

class Main {
	static int V;
	static int E;
	static List<Edge>[] edgeFrom;
	static final int INF = 200_000_000;

	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()) + 1;
		E = Integer.parseInt(st.nextToken());
		edgeFrom = new ArrayList[V];
		for(int i=0; i<V; i++) {
			edgeFrom[i] = new ArrayList<>();
		}

		for(int i=0; i<E; i++) {
			st = new StringTokenizer(br.readLine());
			int node1 = Integer.parseInt(st.nextToken());
			int node2 = Integer.parseInt(st.nextToken());
			int cost = Integer.parseInt(st.nextToken());
			edgeFrom[node1].add(new Edge(node2, cost));
			edgeFrom[node2].add(new Edge(node1, cost));
		}

		st = new StringTokenizer(br.readLine());
		int v1 = Integer.parseInt(st.nextToken());
		int v2 = Integer.parseInt(st.nextToken());

		int path1 = getShortestPath(1,v1) + getShortestPath(v1,v2) + getShortestPath(v2,V-1);
		int path2 = getShortestPath(1,v2) + getShortestPath(v2,v1) + getShortestPath(v1,V-1);

		int shortestPath = Math.min(path1, path2);

		if(shortestPath >= INF) System.out.println(-1);
		else System.out.println(shortestPath);
	}

	public static int getShortestPath(int start, int target) {
		PriorityQueue<Edge> queue = new PriorityQueue<>();
		int[] dist = new int[V];
		Arrays.fill(dist, INF);
		dist[start] = 0;

		queue.offer(new Edge(start, 0));

		while(!queue.isEmpty()) {
			Edge cheapest = queue.poll();
			int cur = cheapest.vertex;

			for (Edge next : edgeFrom[cur]) {
				if (dist[next.vertex] > dist[cur] + next.cost) {
					dist[next.vertex] = dist[cur] + next.cost;
					queue.offer(next);
				}
			}
		}
		return dist[target];
	}
}

 

 


 

 

1504번: 특정한 최단 경로

첫째 줄에 정점의 개수 N과 간선의 개수 E가 주어진다. (2 ≤ N ≤ 800, 0 ≤ E ≤ 200,000) 둘째 줄부터 E개의 줄에 걸쳐서 세 개의 정수 a, b, c가 주어지는데, a번 정점에서 b번 정점까지 양방향 길이 존

www.acmicpc.net

 

반응형