Advertisement
ilyasiluyanov

Untitled

Jan 16th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.66 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.*;
  5.  
  6.  
  7. @SuppressWarnings("ALL")
  8. public class Main {
  9.     Edge[] v;
  10.     long[] dist;
  11.     int n;
  12.  
  13.     public static void main(String[] args) throws IOException {
  14.         new Main().run();
  15.     }
  16.  
  17.     private void run() throws IOException {
  18.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  19.         //Arrays.stream(reader.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
  20.         int[] nmst = Arrays.stream(reader.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
  21.         n = nmst[0];
  22.         int m = nmst[1];
  23.         nmst[2]--;
  24.         nmst[3]--;
  25.         v = new Edge[n];
  26.         dist = new long[n];
  27.         for (int i = 0; i < nmst[1]; i++) {
  28.             int[] q = Arrays.stream(reader.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
  29.             v[i] = new Edge(q[0] - 1, q[1] - 1, q[2]);
  30.         }
  31.         for (int i = 0; i < n; i++) {
  32.             dist[i] = Long.MAX_VALUE;
  33.         }
  34.         dist[nmst[2]] = 0;
  35.  
  36.         for (int i = 0; i < n - 1; i++) {
  37.             for (Edge current : v) {
  38.                 dist[current.b] = Long.min(dist[current.b], dist[current.a] + current.w);
  39.             }
  40.         }
  41.         if (dist[nmst[3]] == Long.MAX_VALUE)
  42.             System.out.println("Unreachable");
  43.         else
  44.             System.out.println(dist[nmst[3]]);
  45.     }
  46.  
  47.     private class Edge {
  48.         int a;
  49.         int b;
  50.         int w;
  51.  
  52.         public Edge(int a, int b, int w) {
  53.             this.a = a;
  54.             this.b = b;
  55.             this.w = w;
  56.         }
  57.     }
  58.  
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement