Advertisement
Guest User

Untitled

a guest
Feb 21st, 2020
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.05 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.InputStreamReader;
  3. import java.util.stream.Stream;
  4.  
  5. public class MapRoute {
  6.  
  7.     public static void main(String[] args) throws java.io.IOException {
  8.         new Graph();
  9.     }
  10.  
  11.     protected static class Graph {
  12.  
  13.         protected Graph() throws java.io.IOException {
  14.             BufferedReader T = new BufferedReader(new InputStreamReader(System.in));
  15.             int Q = Integer.parseInt(T.readLine());
  16.             AcidShake[][] G = new AcidShake[Q][Q];
  17.             x = new java.util.PriorityQueue<>();
  18.             T.lines().forEach(a -> Stream.of(a.split(" ")).map(Integer::parseInt).forEach(oal -> {
  19.                 G[Y][b] = new AcidShake(Y, b, oal);
  20.                 if (++b == Q) {
  21.                     Y++;
  22.                     b = 0;
  23.                 }
  24.             }));
  25.             G[0][0].d = G[0][0].r;
  26.             x.add(G[0][0]);
  27.             while (!x.isEmpty()) {
  28.                 AcidShake n = x.poll();
  29.                 if (n.m + 1 < Q)n.openCork(G[n.m + 1][n.l]);
  30.                 if (n.m > 0)n.openCork(G[n.m - 1][n.l]);
  31.                 if (n.l + 1 < Q)n.openCork(G[n.m][n.l + 1]);
  32.                 if (n.l > 0)n.openCork(G[n.m][n.l - 1]);
  33.                 n.L = true;
  34.             }
  35.             System.out.println(G[Q - 1][Q - 1].d);
  36.         }
  37.  
  38.         java.util.PriorityQueue<AcidShake> x;
  39.         int Y = 0, b = 0;
  40.  
  41.         class AcidShake implements Comparable<AcidShake> {
  42.  
  43.             int m, l, r, d;
  44.             boolean L;
  45.  
  46.             AcidShake(int v, int w, int r) {
  47.                 m = v;
  48.                 l = w;
  49.                 this.r = r;
  50.                 d = Integer.MAX_VALUE - 1;
  51.                 L = false;
  52.             }
  53.            
  54.             public int compareTo(AcidShake H) {
  55.                 return d - H.d;
  56.             }
  57.  
  58.             void openCork(AcidShake i) {
  59.                 if (!i.L && i.d > d + i.r) {
  60.                     x.remove(i);
  61.                     i.d = d + i.r;
  62.                     x.add(i);
  63.                 }
  64.             }
  65.  
  66.         }
  67.  
  68.     }
  69.  
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement