Submission #2634668
Source Code Expand
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class Main { int h, w, sx, sy, gx, gy; int[][] pss; public static void main(String args[]) { new Main().run(); } void run() { FastReader sc = new FastReader(); h = sc.nextInt(); w = sc.nextInt(); sx = sc.nextInt() - 1; sy = sc.nextInt() - 1; gx = sc.nextInt() - 1; gy = sc.nextInt() - 1; pss = new int[h][w]; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { pss[i][j] = sc.nextInt(); } } solve(); } void solve() { List<List<Pair>> graph = new ArrayList<>(); for (int i = 0; i < h * w; i++) { graph.add(new LinkedList<>()); } for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { int index = i * w + j; if (i > 0) { graph.get(index).add(new Pair(index - w, pss[i][j] * pss[i - 1][j])); graph.get(index - w).add(new Pair(index, pss[i][j] * pss[i - 1][j])); } if (i < h - 1) { graph.get(index).add(new Pair(index + w, pss[i][j] * pss[i + 1][j])); graph.get(index + w).add(new Pair(index, pss[i][j] * pss[i + 1][j])); } if (j > 0) { graph.get(index).add(new Pair(index - 1, pss[i][j] * pss[i][j - 1])); graph.get(index - 1).add(new Pair(index, pss[i][j] * pss[i][j - 1])); } if (j < w - 1) { graph.get(index).add(new Pair(index + 1, pss[i][j] * pss[i][j + 1])); graph.get(index + 1).add(new Pair(index, pss[i][j] * pss[i][j + 1])); } } } Prim p = new Prim(graph); p.minimumSpanningTree(sy * w + sx); long ans = p.mstCost; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { ans += pss[i][j]; } } System.out.println(ans); } class Prim { List<List<Pair>> graph; List<List<Pair>> mst; long mstCost; boolean[] visited; Prim(List<List<Pair>> graph) { this.graph = graph; visited = new boolean[graph.size()]; } Prim(int[] froms, int[] tos, long[] costs, int numVertex) { graph = new ArrayList<>(); for (int i = 0; i < numVertex; i++) { graph.add(new LinkedList<>()); } for (int i = 0; i < froms.length; i++) { graph.get(froms[i]).add(new Pair(tos[i], costs[i])); graph.get(tos[i]).add(new Pair(froms[i], costs[i])); } visited = new boolean[numVertex]; } long minimumSpanningTree(int start) { long cost = 0; mst = new ArrayList<>(); for (int i = 0; i < graph.size(); i++) { mst.add(new LinkedList<>()); } visited[start] = true; PriorityQueue<Edge> pQueue = new PriorityQueue<>(Collections.reverseOrder()); for (Pair e : graph.get(start)) { pQueue.offer(new Edge(start, e.vertex, e.cost)); } while (true) { if (pQueue.isEmpty()) { break; } Edge e = pQueue.poll(); if (visited[e.to]) { continue; } visited[e.to] = true; mst.get(e.from).add(new Pair(e.to, e.cost)); cost += e.cost; for (Pair p : graph.get(e.to)) { pQueue.offer(new Edge(e.to, p.vertex, p.cost)); } } mstCost = cost; return cost; } } class Pair implements Comparable<Pair>{ int vertex; long cost; Pair(int vertex, long cost) { this.vertex = vertex; this.cost = cost; } public int compareTo(Pair p) { if (cost == p.cost) { return 0; } else { return cost < p.cost ? -1 : 1; } } } class Edge implements Comparable<Edge>{ int from; int to; long cost; Edge(int from, int to, long cost) { this.from = from; this.to = to; this.cost = cost; } public int compareTo(Edge p) { if (cost == p.cost) { return 0; } else { return cost < p.cost ? -1 : 1; } } } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Submission Info
Submission Time | |
---|---|
Task | D - Game on a Grid |
User | ynish |
Language | Java8 (OpenJDK 1.8.0) |
Score | 100 |
Code Size | 6435 Byte |
Status | AC |
Exec Time | 226 ms |
Memory | 33548 KB |
Judge Result
Set Name | Sample | All | ||||
---|---|---|---|---|---|---|
Score / Max Score | 0 / 0 | 100 / 100 | ||||
Status |
|
|
Set Name | Test Cases |
---|---|
Sample | subtask0_sample_01.txt, subtask0_sample_02.txt, subtask0_sample_03.txt |
All | subtask0_sample_01.txt, subtask0_sample_02.txt, subtask0_sample_03.txt, subtask1_01.txt, subtask1_02.txt, subtask1_03.txt, subtask1_04.txt, subtask1_05.txt, subtask1_06.txt, subtask1_07.txt, subtask1_08.txt, subtask1_09.txt, subtask1_10.txt, subtask1_11.txt, subtask1_12.txt, subtask1_13.txt, subtask1_14.txt, subtask1_15.txt, subtask1_16.txt, subtask1_17.txt, subtask1_18.txt, subtask1_19.txt, subtask1_20.txt, subtask1_21.txt, subtask1_22.txt, subtask1_23.txt, subtask1_24.txt, subtask1_25.txt, subtask1_26.txt, subtask1_27.txt, subtask1_28.txt, subtask1_29.txt |
Case Name | Status | Exec Time | Memory |
---|---|---|---|
subtask0_sample_01.txt | AC | 76 ms | 19156 KB |
subtask0_sample_02.txt | AC | 71 ms | 20692 KB |
subtask0_sample_03.txt | AC | 77 ms | 21460 KB |
subtask1_01.txt | AC | 70 ms | 21460 KB |
subtask1_02.txt | AC | 74 ms | 23508 KB |
subtask1_03.txt | AC | 72 ms | 19540 KB |
subtask1_04.txt | AC | 74 ms | 21204 KB |
subtask1_05.txt | AC | 74 ms | 18388 KB |
subtask1_06.txt | AC | 73 ms | 19412 KB |
subtask1_07.txt | AC | 186 ms | 29252 KB |
subtask1_08.txt | AC | 226 ms | 32800 KB |
subtask1_09.txt | AC | 186 ms | 31172 KB |
subtask1_10.txt | AC | 177 ms | 29508 KB |
subtask1_11.txt | AC | 177 ms | 30656 KB |
subtask1_12.txt | AC | 191 ms | 33548 KB |
subtask1_13.txt | AC | 188 ms | 30016 KB |
subtask1_14.txt | AC | 188 ms | 32820 KB |
subtask1_15.txt | AC | 195 ms | 30904 KB |
subtask1_16.txt | AC | 156 ms | 31060 KB |
subtask1_17.txt | AC | 84 ms | 18260 KB |
subtask1_18.txt | AC | 85 ms | 21460 KB |
subtask1_19.txt | AC | 81 ms | 17876 KB |
subtask1_20.txt | AC | 151 ms | 31828 KB |
subtask1_21.txt | AC | 168 ms | 30788 KB |
subtask1_22.txt | AC | 183 ms | 30136 KB |
subtask1_23.txt | AC | 166 ms | 29780 KB |
subtask1_24.txt | AC | 74 ms | 19540 KB |
subtask1_25.txt | AC | 77 ms | 18644 KB |
subtask1_26.txt | AC | 82 ms | 20820 KB |
subtask1_27.txt | AC | 86 ms | 19540 KB |
subtask1_28.txt | AC | 87 ms | 19412 KB |
subtask1_29.txt | AC | 73 ms | 17748 KB |