|
| 1 | +``` |
| 2 | +import java.io.*; |
| 3 | +import java.util.*; |
| 4 | +
|
| 5 | +public class Main { |
| 6 | + private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 7 | + private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 8 | + private static final int INF = Integer.MAX_VALUE/2; |
| 9 | + private static List<Edge>[] edges; |
| 10 | + private static Map<String, Integer> visited; |
| 11 | + private static int N; |
| 12 | + public static void main(String[] args) throws IOException { |
| 13 | + init(); |
| 14 | + int answer = dijkstra(); |
| 15 | +
|
| 16 | + bw.write(answer + "\n"); |
| 17 | + bw.flush(); |
| 18 | + bw.close(); |
| 19 | + br.close(); |
| 20 | + } |
| 21 | +
|
| 22 | + private static void init() throws IOException { |
| 23 | + N = Integer.parseInt(br.readLine()); |
| 24 | +
|
| 25 | + edges = new List[N]; |
| 26 | + visited = new HashMap<>(); |
| 27 | +
|
| 28 | + for (int i = 0; i < N; i++) { |
| 29 | + edges[i] = new ArrayList<>(); |
| 30 | + } |
| 31 | +
|
| 32 | + for (int i = 0; i < N; i++) { |
| 33 | + char[] input = br.readLine().toCharArray(); |
| 34 | + for (int j = 0; j < N; j++) { |
| 35 | + if (input[j] == '.') continue; |
| 36 | + edges[i].add(new Edge(j, input[j] - '0')); |
| 37 | + } |
| 38 | + } |
| 39 | +
|
| 40 | + for (int i = 0; i < N; i++) { |
| 41 | + char[] input = br.readLine().toCharArray(); |
| 42 | + for (int j = 0; j < N; j++) { |
| 43 | + if (input[j] == '.') continue; |
| 44 | + for (int k = 0; k < edges[i].size(); k++) { |
| 45 | + if (j == edges[i].get(k).dest) { |
| 46 | + edges[i].get(k).w2 = input[j] - '0'; |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | +
|
| 53 | + private static int dijkstra() { |
| 54 | + PriorityQueue<Edge> pq = new PriorityQueue<>((o1, o2) -> Integer.compare(o1.cost, o2.cost)); |
| 55 | + pq.add(new Edge(0, 0, 0, 0)); |
| 56 | +
|
| 57 | + while (!pq.isEmpty()) { |
| 58 | + Edge current = pq.poll(); |
| 59 | +
|
| 60 | + String currentKey = current.dest + "," + current.w1 + "," + current.w2; |
| 61 | + if (visited.containsKey(currentKey)) continue; |
| 62 | + visited.put(currentKey, current.cost); |
| 63 | +
|
| 64 | + if (current.dest == 1) { |
| 65 | + return current.cost; |
| 66 | + } |
| 67 | +
|
| 68 | + for (Edge edge : edges[current.dest]) { |
| 69 | + int w1 = current.w1 + edge.w1; |
| 70 | + int w2 = current.w2 + edge.w2; |
| 71 | + int nCost = w1 * w2; |
| 72 | +
|
| 73 | + String key = edge.dest + "," + w1 + "," + w2; |
| 74 | +
|
| 75 | + if (!visited.containsKey(key) || visited.get(key) > nCost) { |
| 76 | + pq.add(new Edge(edge.dest, w1, w2, nCost)); |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | +
|
| 81 | + return -1; |
| 82 | + } |
| 83 | +
|
| 84 | + static class Edge { |
| 85 | + int dest; |
| 86 | + int w1; |
| 87 | + int w2; |
| 88 | + int cost; |
| 89 | +
|
| 90 | + public Edge(int dest, int w1) { |
| 91 | + this.dest = dest; |
| 92 | + this.w1 = w1; |
| 93 | + this.w2 = 0; |
| 94 | + this.cost = 0; |
| 95 | + } |
| 96 | +
|
| 97 | + public Edge(int dest, int w1, int w2, int cost) { |
| 98 | + this.dest = dest; |
| 99 | + this.w1 = w1; |
| 100 | + this.w2 = w2; |
| 101 | + this.cost = cost; |
| 102 | + } |
| 103 | + } |
| 104 | +} |
| 105 | +``` |
0 commit comments