|
| 1 | +package Graph.P5719; |
| 2 | + |
| 3 | +import java.io.*; |
| 4 | +import java.util.*; |
| 5 | + |
| 6 | +public class Main { |
| 7 | + |
| 8 | + static int N, M, S, D, U, V, P; |
| 9 | + static ArrayList<Node>[] graph, reverse; |
| 10 | + static int[] dist; |
| 11 | + |
| 12 | + public static void main(String[] args) throws Exception { |
| 13 | + System.setIn(new FileInputStream("src/Graph/P5719/input.txt")); |
| 14 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 15 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 16 | + |
| 17 | + while ((N = stoi(st.nextToken())) != 0 && (M = stoi(st.nextToken())) != 0) { |
| 18 | + st = new StringTokenizer(br.readLine()); |
| 19 | + S = stoi(st.nextToken()); D = stoi(st.nextToken()); |
| 20 | + |
| 21 | + graph = new ArrayList[N]; |
| 22 | + reverse = new ArrayList[N]; |
| 23 | + for (int i = 0; i < N; i++) { |
| 24 | + graph[i] = new ArrayList<>(); |
| 25 | + reverse[i] = new ArrayList<>(); |
| 26 | + } |
| 27 | + |
| 28 | + while (M-- > 0) { |
| 29 | + st = new StringTokenizer(br.readLine()); |
| 30 | + U = stoi(st.nextToken()); V = stoi(st.nextToken()); P = stoi(st.nextToken()); |
| 31 | + graph[U].add(new Node(V, P)); |
| 32 | + reverse[V].add(new Node(U, P)); |
| 33 | + } |
| 34 | + |
| 35 | + dijkstra(); |
| 36 | + delPath(); |
| 37 | + dijkstra(); |
| 38 | + System.out.println(dist[D]); |
| 39 | + |
| 40 | + st = new StringTokenizer(br.readLine()); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + static void dijkstra() { |
| 45 | + dist = new int[N]; |
| 46 | + Arrays.fill(dist, -1); |
| 47 | + |
| 48 | + PriorityQueue<Node> pq = new PriorityQueue<>(((o1, o2) -> o1.dist - o2.dist)); |
| 49 | + pq.offer(new Node(S, 0)); |
| 50 | + while (!pq.isEmpty()) { |
| 51 | + Node cur = pq.poll(); |
| 52 | + if (dist[cur.idx] != -1) continue; |
| 53 | + |
| 54 | + dist[cur.idx] = cur.dist; |
| 55 | + for (Node next : graph[cur.idx]) { |
| 56 | + if (dist[next.idx] == -1) |
| 57 | + pq.offer(new Node(next.idx, dist[cur.idx] + next.dist)); |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + static void delPath() { |
| 63 | + Queue<Integer> q = new LinkedList<>(); |
| 64 | + boolean[] visited = new boolean[N]; |
| 65 | + q.offer(D); visited[D] = true; |
| 66 | + while (!q.isEmpty()) { |
| 67 | + int cur = q.poll(); |
| 68 | + for (Node prev : reverse[cur]) { |
| 69 | + if (dist[prev.idx] + prev.dist == dist[cur]) { |
| 70 | + graph[prev.idx].remove(new Node(cur, prev.dist)); |
| 71 | + if (!visited[prev.idx]) { |
| 72 | + visited[prev.idx] = true; |
| 73 | + q.offer(prev.idx); |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + static int stoi(String s) { return Integer.parseInt(s); } |
| 81 | + |
| 82 | + static class Node { |
| 83 | + int idx, dist; |
| 84 | + |
| 85 | + public Node(int idx, int dist) { |
| 86 | + this.idx = idx; |
| 87 | + this.dist = dist; |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public boolean equals(Object o) { |
| 92 | + Node node = (Node) o; |
| 93 | + return idx == node.idx && dist == node.dist; |
| 94 | + } |
| 95 | + } |
| 96 | +} |
0 commit comments