티스토리 뷰
다익스트라 알고리즘은 음수를 갖지 않는 유향 그래프에서
출발점과 도착점 사이의 최단거리를 구하는 알고리즘이다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.HashMap; | |
import java.util.LinkedList; | |
import java.util.Scanner; | |
/** | |
* 백준, 1753, 다익스트라 알고리즘 | |
* | |
* @author whitebeard | |
* | |
*/ | |
public class Problem1753 { | |
static int V; // 정점의 개수 | |
static int E; // 간선의 개수 | |
static int START_V; // 시작 정점의 번호 | |
static int[] dist; // 시작점 부터의 거리 | |
static int[] visited; // 방문여부 체크 | |
static HashMap<Integer, LinkedList<Node>> maps; // 간선 정보 | |
public static void main(String[] args) { | |
Scanner sc = new Scanner(System.in); | |
V = sc.nextInt(); | |
E = sc.nextInt(); | |
START_V = sc.nextInt(); | |
maps = new HashMap<>(); | |
dist = new int[V + 1]; | |
visited = new int[V + 1]; | |
for (int i = 0; i <= V; i++) { | |
dist[i] = Integer.MAX_VALUE - 1; | |
} | |
for (int i = 0; i < E; i++) { | |
int from = sc.nextInt(); | |
int to = sc.nextInt(); | |
int cost = sc.nextInt(); | |
if (!maps.containsKey(from)) | |
maps.put(from, new LinkedList<>()); | |
maps.get(from).add(new Node(from, to, cost)); | |
} | |
sc.close(); | |
// 시작 | |
dist[START_V] = 0; | |
for (int i = 0; i < V; i++) { | |
// 최소값을 가지는 정점 확인 | |
int minCost = Integer.MAX_VALUE; | |
int from = -1; | |
for (int k = 1; k < dist.length; k++) { | |
if (visited[k] == 0 && minCost > dist[k]) { | |
from = k; | |
minCost = dist[k]; | |
} | |
} | |
// 최소값 정점 체크 | |
visited[from] = 1; | |
// from 에서 출발하는 경로기 있을 때 처리 | |
if (maps.containsKey(from)) { | |
for (Node next : maps.get(from)) { | |
int to = next.to; | |
int cost = next.cost; | |
// from 으로 가는 경로가 계산 되어 있는 경우만 처리 | |
if (dist[from] != Integer.MAX_VALUE - 1 && dist[to] > dist[from] + cost) { | |
dist[to] = dist[from] + cost; | |
} | |
} | |
} | |
} | |
for (int i = 1; i < dist.length; i++) { | |
System.out.println(dist[i] == Integer.MAX_VALUE - 1 ? "INF" : dist[i]); | |
} | |
} | |
} | |
class Node { | |
public int from; | |
public int to; | |
public int cost; | |
public Node(int from, int to, int cost) { | |
super(); | |
this.from = from; | |
this.to = to; | |
this.cost = cost; | |
} | |
} |
반응형
'알고리즘 > 백준' 카테고리의 다른 글
[백준][DP] 1463 1로 만들기 (0) | 2017.10.25 |
---|---|
[백준][세그먼트 트리] 1275 커피숍2 (0) | 2017.10.25 |
[백준][다이나믹] 2533 사회망 서비스 (0) | 2017.09.15 |
[백준][세그먼트트리] 6549 히스토그램에서 가장 큰 사각형 (0) | 2017.09.14 |
[백준][DP] 5557 1학년 (0) | 2017.08.23 |
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- Tez
- bash
- Python
- mysql
- 하둡
- SPARK
- java
- 하이브
- 다이나믹
- AWS
- error
- Linux
- oozie
- 알고리즘
- ubuntu
- Hadoop
- yarn
- emr
- build
- 파이썬
- airflow
- nodejs
- SQL
- 백준
- S3
- hbase
- 정올
- HDFS
- 오류
- HIVE
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
글 보관함