Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.*;
- @SuppressWarnings("ALL")
- public class Main {
- Edge[] v;
- long[] dist;
- int n;
- public static void main(String[] args) throws IOException {
- new Main().run();
- }
- private void run() throws IOException {
- BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
- //Arrays.stream(reader.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
- int[] nmst = Arrays.stream(reader.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
- n = nmst[0];
- int m = nmst[1];
- nmst[2]--;
- nmst[3]--;
- v = new Edge[n];
- dist = new long[n];
- for (int i = 0; i < nmst[1]; i++) {
- int[] q = Arrays.stream(reader.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
- v[i] = new Edge(q[0] - 1, q[1] - 1, q[2]);
- }
- for (int i = 0; i < n; i++) {
- dist[i] = Long.MAX_VALUE;
- }
- dist[nmst[2]] = 0;
- for (int i = 0; i < n - 1; i++) {
- for (Edge current : v) {
- dist[current.b] = Long.min(dist[current.b], dist[current.a] + current.w);
- }
- }
- if (dist[nmst[3]] == Long.MAX_VALUE)
- System.out.println("Unreachable");
- else
- System.out.println(dist[nmst[3]]);
- }
- private class Edge {
- int a;
- int b;
- int w;
- public Edge(int a, int b, int w) {
- this.a = a;
- this.b = b;
- this.w = w;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement