-
[KOITP] 워프자료구조 및 알고리즘/문제풀이 2017. 1. 16. 08:02
출 처 : http://koitp.org/problem/SDS_PRO_10_3/read/
시간 제한 메모리 제한 제출 횟수 정답 횟수 (비율) 정답자 수 1.0 초 512 MB 1895 235 (12%) 165 < Comment >
기존에 풀던 것들은 다익스트라를 딱히 최적화 해 주지 않아도 풀 수 있는 문제들이었으나, 이번 문제와 같은 경우에는 최적화를 해 주지 않으면 시간 초과가 발생했다. 기본적으로 다익스트라를 잘못 이해하고 있었던 것 같다. 우선 큐 대신에 우선순위 큐를 써서 가장 짧은 경로부터 릴랙싱을 진행한다. 재귀호출식으로 구현을 했었는데, 그렇게 하게 되면 DFS 방식으로 돌아가기 때문에 다소 적합하지 않은 것 같다.
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Comparator; import java.util.PriorityQueue; import java.util.Queue; import java.util.StringTokenizer; public class Warp { public static int N = 0; public static int M = 0; public static long INF = Long.MAX_VALUE; public static ArrayList<Node> adjust[] = null; public static long Relaxing[] = null; public static boolean isVisited[] = null; public static class Node { int to; long coast; long relaxing; public Node(int to, int coast, long relaxing) { this.to = to; this.coast = coast; this.relaxing = relaxing; } public int getTo() { return to; } public long getCoast() { return coast; } public long getRelaxing() { return relaxing; } public void setRelaxing(long relaxing) { this.relaxing = relaxing; } } public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); StringTokenizer st = new StringTokenizer(br.readLine()); N = Integer.parseInt(st.nextToken()); M = Integer.parseInt(st.nextToken()); isVisited = new boolean[N + 1]; Relaxing = new long[N + 1]; for (int i = 0 ; i <= N ; i++) { Relaxing[i] = INF; } Relaxing[1] = 0; adjust = new ArrayList[N + 1]; for (int i = 1 ; i <= N ; i++) { adjust[i] = new ArrayList<>(); } int a = 0; int b = 0; int c = 0; for (int i = 1 ; i <= M ; i++) { st = new StringTokenizer(br.readLine()); a = Integer.parseInt(st.nextToken()); b = Integer.parseInt(st.nextToken()); c = Integer.parseInt(st.nextToken()); adjust[a].add(new Node(b, c, INF)); } Dijkstra(); if (Relaxing[N] == INF) { bw.write("-1"); } else { bw.write("" + Relaxing[N]); } bw.flush(); bw.close(); } public static void Dijkstra() { PriorityQueue<Node> pq = new PriorityQueue<>(N, new Comparator<Node>() { @Override public int compare(Node arg0, Node arg1) { if (arg0.getRelaxing() > arg1.getRelaxing()) return 1; if (arg0.getRelaxing() < arg1.getRelaxing()) return -1; return 0; } }); pq.add(new Node(1, 0, 0)); while (!pq.isEmpty()) { int node = pq.poll().getTo(); if (isVisited[node]) continue; isVisited[node] = true; for (Node to : adjust[node]) { int toNode = to.getTo(); long toCoast = to.getCoast(); long newRel = Relaxing[node] + toCoast; if (Relaxing[toNode] > newRel) { Relaxing[toNode] = newRel; to.setRelaxing(newRel); pq.add(to); } } } } }