|
| 1 | +```java |
| 2 | +import java.io.*; |
| 3 | +import java.util.*; |
| 4 | + |
| 5 | +public class Main { |
| 6 | + static class Node { |
| 7 | + int to,cost; |
| 8 | + Node(int to, int cost) { |
| 9 | + this.to = to; |
| 10 | + this.cost = cost; |
| 11 | + } |
| 12 | + } |
| 13 | + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 14 | + static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 15 | + static StringTokenizer st; |
| 16 | + static int N,E; |
| 17 | + static List<Node>[] adjList; |
| 18 | + static int v1,v2; |
| 19 | + static int MAX = Integer.MAX_VALUE; |
| 20 | + static int min = -1; |
| 21 | + |
| 22 | + public static void main(String[] args) throws Exception { |
| 23 | + st = new StringTokenizer(br.readLine()); |
| 24 | + N = Integer.parseInt(st.nextToken()); |
| 25 | + E = Integer.parseInt(st.nextToken()); |
| 26 | + adjList = new ArrayList[N+1]; |
| 27 | + for (int i = 1; i <= N; i++) { |
| 28 | + adjList[i] = new ArrayList<>(); |
| 29 | + } |
| 30 | + for (int i = 0; i < E; i++) { |
| 31 | + st = new StringTokenizer(br.readLine()); |
| 32 | + int from = Integer.parseInt(st.nextToken()); |
| 33 | + int to = Integer.parseInt(st.nextToken()); |
| 34 | + int cost = Integer.parseInt(st.nextToken()); |
| 35 | + adjList[from].add(new Node(to, cost)); |
| 36 | + adjList[to].add(new Node(from, cost)); |
| 37 | + } |
| 38 | + st = new StringTokenizer(br.readLine()); |
| 39 | + v1 = Integer.parseInt(st.nextToken()); |
| 40 | + v2 = Integer.parseInt(st.nextToken()); |
| 41 | + int[][] dist = new int[3][N+1]; |
| 42 | + dijkstra(1,dist[0]); |
| 43 | + dijkstra(v1,dist[1]); |
| 44 | + dijkstra(v2,dist[2]); |
| 45 | + |
| 46 | + boolean flag1 = true; |
| 47 | + boolean flag2 = true; |
| 48 | + if(dist[0][v1] == MAX || dist[1][v2] == MAX || dist[2][N] == MAX) flag1 = false; |
| 49 | + if(dist[0][v2] == MAX || dist[2][v1] == MAX || dist[1][N] == MAX) flag2 = false; |
| 50 | + if(flag1 && flag2){ |
| 51 | + min = Math.min(dist[0][v1] + dist[1][v2] + dist[2][N],dist[0][v2] + dist[2][v1] + dist[1][N]); |
| 52 | + }else if(flag1){ |
| 53 | + min = dist[0][v1] + dist[1][v2] + dist[2][N]; |
| 54 | + }else if(flag2){ |
| 55 | + min = dist[0][v2] + dist[2][v1] + dist[1][N]; |
| 56 | + } |
| 57 | + bw.write(min + ""); |
| 58 | + bw.close(); |
| 59 | + } |
| 60 | + public static void dijkstra(int start , int[] dist) { |
| 61 | + PriorityQueue<Node> pq = new PriorityQueue<>((a,b) -> a.cost - b.cost); |
| 62 | + pq.add(new Node(start,0)); |
| 63 | + Arrays.fill(dist,MAX); |
| 64 | + dist[start] = 0; |
| 65 | + |
| 66 | + while (!pq.isEmpty()) { |
| 67 | + Node cur = pq.poll(); |
| 68 | + |
| 69 | + if(cur.cost > dist[cur.to]) { |
| 70 | + continue; |
| 71 | + } |
| 72 | + for(Node n : adjList[cur.to]){ |
| 73 | + int newDist = dist[cur.to] + n.cost; |
| 74 | + if(newDist < dist[n.to]) { |
| 75 | + dist[n.to] = newDist; |
| 76 | + pq.add(new Node(n.to,newDist)); |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | +} |
| 83 | +``` |
0 commit comments