Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.InputMismatchException;
- public class SlowLeak {
- static long INF = 1L << 33;
- public static void main(String[] args) {
- FastScanner in = new FastScanner(System.in);
- int N = in.nextInt();
- int M = in.nextInt();
- int T = in.nextInt();
- int D = in.nextInt();
- int[] repairs = new int[T + 2];
- repairs[0] = 0;
- for (int i = 1; i <= T; i++)
- repairs[i] = in.nextInt() - 1;
- repairs[T + 1] = N - 1;
- long[][] minDist = new long[N][N];
- for (int i = 0; i < N; i++) {
- for (int j = i + 1; j < N; j++) {
- minDist[i][j] = minDist[j][i] = INF;
- }
- }
- for (int i = 0; i < M; i++) {
- int l = in.nextInt() - 1;
- int r = in.nextInt() - 1;
- int c = in.nextInt();
- minDist[l][r] = Math.min(minDist[l][r], c);
- minDist[r][l] = Math.min(minDist[r][l], c);
- }
- floydWarshall(minDist);
- long[][] nxtDist = new long[repairs.length][repairs.length];
- for (int i = 0; i < repairs.length; i++) {
- for (int j = 0; j < repairs.length; j++) {
- long dist = minDist[repairs[i]][repairs[j]];
- if (dist <= D) {
- nxtDist[i][j] = dist;
- } else {
- nxtDist[i][j] = INF;
- }
- }
- }
- floydWarshall(nxtDist);
- if (nxtDist[0][repairs.length - 1] == INF) {
- System.out.println("stuck");
- } else {
- System.out.println(nxtDist[0][repairs.length - 1]);
- }
- }
- static void floydWarshall(long[][] a) {
- for (int k = 0; k < a.length; k++) {
- for (int i = 0; i < a.length; i++) {
- for (int j = 0; j < a.length; j++) {
- if (a[i][j] > a[i][k] + a[k][j]) {
- a[i][j] = a[i][k] + a[k][j];
- }
- }
- }
- }
- }
- // Matt Fontaine's Fast I/O
- static class FastScanner {
- private InputStream stream;
- private byte[] buf = new byte[1024];
- private int curChar;
- private int numChars;
- public FastScanner(InputStream stream) {
- this.stream = stream;
- }
- int read() {
- if (numChars == -1)
- throw new InputMismatchException();
- if (curChar >= numChars) {
- curChar = 0;
- try {
- numChars = stream.read(buf);
- } catch (IOException e) {
- throw new InputMismatchException();
- }
- if (numChars <= 0)
- return -1;
- }
- return buf[curChar++];
- }
- boolean isSpaceChar(int c) {
- return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
- }
- boolean isEndline(int c) {
- return c == '\n' || c == '\r' || c == -1;
- }
- int nextInt() {
- return Integer.parseInt(next());
- }
- long nextLong() {
- return Long.parseLong(next());
- }
- double nextDouble() {
- return Double.parseDouble(next());
- }
- String next() {
- int c = read();
- while (isSpaceChar(c))
- c = read();
- StringBuilder res = new StringBuilder();
- do {
- res.appendCodePoint(c);
- c = read();
- } while (!isSpaceChar(c));
- return res.toString();
- }
- String nextLine() {
- int c = read();
- while (isEndline(c))
- c = read();
- StringBuilder res = new StringBuilder();
- do {
- res.appendCodePoint(c);
- c = read();
- } while (!isEndline(c));
- return res.toString();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement