From a33fd0a7be0220fd10c64d0c98c6b069754799d1 Mon Sep 17 00:00:00 2001 From: nathan29849 Date: Sun, 26 Mar 2023 18:42:45 +0900 Subject: [PATCH] =?UTF-8?q?nathan0321=20=ED=9A=A1=EB=8B=A8=EB=B3=B4?= =?UTF-8?q?=EB=8F=84=20java=20=ED=92=80=EC=9D=B4=20=EC=8B=A4=ED=8C=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\213\250\353\263\264\353\217\2040321.java" | 98 +++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 "nathan/java/src/main/java/org/example/mar/\355\232\241\353\213\250\353\263\264\353\217\2040321.java" diff --git "a/nathan/java/src/main/java/org/example/mar/\355\232\241\353\213\250\353\263\264\353\217\2040321.java" "b/nathan/java/src/main/java/org/example/mar/\355\232\241\353\213\250\353\263\264\353\217\2040321.java" new file mode 100644 index 0000000..65dcc0d --- /dev/null +++ "b/nathan/java/src/main/java/org/example/mar/\355\232\241\353\213\250\353\263\264\353\217\2040321.java" @@ -0,0 +1,98 @@ +package org.example.mar; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.Arrays; +import java.util.Comparator; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.PriorityQueue; +import java.util.Set; +import java.util.StringTokenizer; + +public class 횡단보도0321 { + + public static void main(String[] args) throws IOException { + 횡단보도0321 m = new 횡단보도0321(); + int ans = m.solution(); + System.out.println(ans); + } + int N; + int M; + Map> nodes = new HashMap<>(); + int ANS; + + class Point { + int dest; + int cost; + + public Point(int dest, int cost) { + this.dest = dest; + this.cost = cost; + } + } + int solution() throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st = new StringTokenizer(br.readLine()); + N = Integer.parseInt(st.nextToken()); + M = Integer.parseInt(st.nextToken()); + + int a, b; + for (int i = 1; i < M+1; i++) { + st = new StringTokenizer(br.readLine()); + a = Integer.parseInt(st.nextToken()); + b = Integer.parseInt(st.nextToken()); + keyCheck(a, b, i); + keyCheck(b, a, i); + } + run(); + return ANS; + } + + void keyCheck(int a, int b, int cost) { + if (!nodes.containsKey(a)) { + nodes.put(a, new HashSet<>()); + } + nodes.get(a).add(new Point(b, cost)); + } + + void run() { + boolean[] visited = new boolean[N+1]; + int[] dists = new int[N + 1]; + Arrays.fill(dists, Integer.MAX_VALUE); + PriorityQueue q = new PriorityQueue<>(Comparator.comparingInt(o -> o[1])); + q.add(new int[]{1, 0}); // [현재 위치, cost] + dists[1] = 0; + while (!q.isEmpty()) { + int[] now = q.poll(); + int point = now[0]; + int dist = now[1]; + if (!visited[point]) { + visited[point] = true; + for (Point next : nodes.get(point)) { + if (next.cost + M <= dist) { + M++; + } + int tmp = next.cost + M; + if (dists[next.dest] > tmp + dist) { + dists[next.dest] = tmp + dist; + q.add(new int[]{next.dest, dists[next.dest]}); + } + } + } + } + ANS = dists[N]; + } + +} + + +//4 6 +//1 3 +//3 2 +//2 4 +//2 3 +//1 2 +//3 4