Advertisement
Guest User

Slow Leak Floyd Warshall Solution

a guest
Feb 24th, 2020
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.11 KB | None | 0 0
  1. import java.io.IOException;
  2. import java.io.InputStream;
  3. import java.util.InputMismatchException;
  4.  
  5. public class SlowLeak {
  6.  
  7.     static long INF = 1L << 33;
  8.  
  9.     public static void main(String[] args) {
  10.         FastScanner in = new FastScanner(System.in);
  11.         int N = in.nextInt();
  12.         int M = in.nextInt();
  13.         int T = in.nextInt();
  14.         int D = in.nextInt();
  15.  
  16.         int[] repairs = new int[T + 2];
  17.         repairs[0] = 0;
  18.         for (int i = 1; i <= T; i++)
  19.             repairs[i] = in.nextInt() - 1;
  20.         repairs[T + 1] = N - 1;
  21.  
  22.         long[][] minDist = new long[N][N];
  23.         for (int i = 0; i < N; i++) {
  24.             for (int j = i + 1; j < N; j++) {
  25.                 minDist[i][j] = minDist[j][i] = INF;
  26.             }
  27.         }
  28.  
  29.         for (int i = 0; i < M; i++) {
  30.             int l = in.nextInt() - 1;
  31.             int r = in.nextInt() - 1;
  32.             int c = in.nextInt();
  33.             minDist[l][r] = Math.min(minDist[l][r], c);
  34.             minDist[r][l] = Math.min(minDist[r][l], c);
  35.         }
  36.  
  37.         floydWarshall(minDist);
  38.  
  39.         long[][] nxtDist = new long[repairs.length][repairs.length];
  40.  
  41.         for (int i = 0; i < repairs.length; i++) {
  42.             for (int j = 0; j < repairs.length; j++) {
  43.                 long dist = minDist[repairs[i]][repairs[j]];
  44.                 if (dist <= D) {
  45.                     nxtDist[i][j] = dist;
  46.                 } else {
  47.                     nxtDist[i][j] = INF;
  48.                 }
  49.             }
  50.         }
  51.  
  52.         floydWarshall(nxtDist);
  53.  
  54.         if (nxtDist[0][repairs.length - 1] == INF) {
  55.             System.out.println("stuck");
  56.         } else {
  57.             System.out.println(nxtDist[0][repairs.length - 1]);
  58.         }
  59.     }
  60.  
  61.     static void floydWarshall(long[][] a) {
  62.         for (int k = 0; k < a.length; k++) {
  63.             for (int i = 0; i < a.length; i++) {
  64.                 for (int j = 0; j < a.length; j++) {
  65.                     if (a[i][j] > a[i][k] + a[k][j]) {
  66.                         a[i][j] = a[i][k] + a[k][j];
  67.                     }
  68.                 }
  69.             }
  70.         }
  71.     }
  72.  
  73.     // Matt Fontaine's Fast I/O
  74.     static class FastScanner {
  75.         private InputStream stream;
  76.         private byte[] buf = new byte[1024];
  77.         private int curChar;
  78.         private int numChars;
  79.  
  80.         public FastScanner(InputStream stream) {
  81.             this.stream = stream;
  82.         }
  83.  
  84.         int read() {
  85.             if (numChars == -1)
  86.                 throw new InputMismatchException();
  87.             if (curChar >= numChars) {
  88.                 curChar = 0;
  89.                 try {
  90.                     numChars = stream.read(buf);
  91.                 } catch (IOException e) {
  92.                     throw new InputMismatchException();
  93.                 }
  94.                 if (numChars <= 0)
  95.                     return -1;
  96.             }
  97.             return buf[curChar++];
  98.         }
  99.  
  100.         boolean isSpaceChar(int c) {
  101.             return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
  102.         }
  103.  
  104.         boolean isEndline(int c) {
  105.             return c == '\n' || c == '\r' || c == -1;
  106.         }
  107.  
  108.         int nextInt() {
  109.             return Integer.parseInt(next());
  110.         }
  111.  
  112.         long nextLong() {
  113.             return Long.parseLong(next());
  114.         }
  115.  
  116.         double nextDouble() {
  117.             return Double.parseDouble(next());
  118.         }
  119.  
  120.         String next() {
  121.             int c = read();
  122.             while (isSpaceChar(c))
  123.                 c = read();
  124.             StringBuilder res = new StringBuilder();
  125.             do {
  126.                 res.appendCodePoint(c);
  127.                 c = read();
  128.             } while (!isSpaceChar(c));
  129.             return res.toString();
  130.         }
  131.  
  132.         String nextLine() {
  133.             int c = read();
  134.             while (isEndline(c))
  135.                 c = read();
  136.             StringBuilder res = new StringBuilder();
  137.             do {
  138.                 res.appendCodePoint(c);
  139.                 c = read();
  140.             } while (!isEndline(c));
  141.             return res.toString();
  142.         }
  143.     }
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement