본문 바로가기

백준/그래프

최단경로 -1753번

728x90

https://www.acmicpc.net/problem/1753

 

1753번: 최단경로

첫째 줄에 정점의 개수 V와 간선의 개수 E가 주어진다. (1≤V≤20,000, 1≤E≤300,000) 모든 정점에는 1부터 V까지 번호가 매겨져 있다고 가정한다. 둘째 줄에는 시작 정점의 번호 K(1≤K≤V)가 주어진다.

www.acmicpc.net

문제

방향그래프가 주어지면 주어진 시작점에서 다른 모든 정점으로의 최단 경로를 구하는 프로그램을 작성하시오. 단, 모든 간선의 가중치는 10 이하의 자연수이다.

입력

첫째 줄에 정점의 개수 V와 간선의 개수 E가 주어진다. (1≤V≤20,000, 1≤E≤300,000) 모든 정점에는 1부터 V까지 번호가 매겨져 있다고 가정한다. 둘째 줄에는 시작 정점의 번호 K(1≤K≤V)가 주어진다. 셋째 줄부터 E개의 줄에 걸쳐 각 간선을 나타내는 세 개의 정수 (u, v, w)가 순서대로 주어진다. 이는 u에서 v로 가는 가중치 w인 간선이 존재한다는 뜻이다. u와 v는 서로 다르며 w는 10 이하의 자연수이다. 서로 다른 두 정점 사이에 여러 개의 간선이 존재할 수도 있음에 유의한다.

출력

첫째 줄부터 V개의 줄에 걸쳐, i번째 줄에 i번 정점으로의 최단 경로의 경로값을 출력한다. 시작점 자신은 0으로 출력하고, 경로가 존재하지 않는 경우에는 INF를 출력하면 된다.

예제 입력 1 복사

5 6 1 5 1 1 1 2 2 1 3 3 2 3 4 2 4 5 3 4 6

예제 출력 1 복사

0 2 3 7 INF

 

package a0825;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.PriorityQueue;
import java.util.StringTokenizer;

public class 최단경로 {

	static int v,e;
	static int s;
	static StringTokenizer st;
	static ArrayList<Pair>[]arr;
	//static ArrayList<ArrayList<Pair>>arr;
	static int[] distance;
	static boolean[] visit;
//	static boolean[] visit;
	static class Pair{
		
		int x; int y;
		Pair(int x, int y){
			this.x = x;
			this.y = y;
		
		}
	}
	
	
	public static void main(String[] args) throws IOException {
		
		BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
		st = new StringTokenizer(br.readLine());
		
		v= Integer.parseInt(st.nextToken());
		e= Integer.parseInt(st.nextToken());
		
		s= Integer.parseInt(br.readLine());
		PriorityQueue<Pair>pq = new PriorityQueue<>((o1,o2)->o1.y-o2.y); //최소힙
		
		arr= new ArrayList[v+1];
		visit = new boolean[v+1];
		distance = new int[v+1];
		for(int i=0; i<=v; i++) {
			arr[i]=new ArrayList<>();
			distance[i]=Integer.MAX_VALUE;
		}
		for(int i=0; i<e; i++) {
			
			st = new StringTokenizer(br.readLine());
			int u = Integer.parseInt(st.nextToken());
			int v = Integer.parseInt(st.nextToken());
			int w = Integer.parseInt(st.nextToken());
			
			arr[u].add(new Pair(v,w));
			//arr.get(v).add(new Pair(u,w));
		
		}
		distance[s]=0;
	
		pq.add(new Pair(s,0));
		
		while(!pq.isEmpty()) {
			
			Pair p = pq.poll();
			int dist = p.y;
			int current = p.x;
			
			if(distance[current]<dist) {continue;}
			//visit[current]=true;
			for(int i=0; i<arr[current].size(); i++) {
				int next = arr[current].get(i).x; //다음
				int nw = arr[current].get(i).y;
				if((distance[current]+nw < distance[next])) {
					distance[next]=distance[current]+nw;
					pq.add(new Pair(next,distance[next]));
				}
			}
		}
		
		for(int i=1; i<=v; i++) {
		if(distance[i]==Integer.MAX_VALUE) {System.out.println("INF");continue;}
		System.out.println(distance[i]);
	}
	
	}
}

- 다익스트라를 이용해서 풀면된다 

- 한정점에서 다른 정점까지의 가중치의 값이 최소일때를 출력하면 된다 -> 다익스트라

'백준 > 그래프' 카테고리의 다른 글

이진검색트리 - 5639번 (java)  (0) 2021.08.11
트리의 부모찾기  (0) 2021.04.26
키 순서 / 플로이드 와샬  (0) 2021.04.24