|
| 1 | +```java |
| 2 | + |
| 3 | +import java.util.*; |
| 4 | +import java.io.*; |
| 5 | + |
| 6 | +class Main { |
| 7 | + |
| 8 | + // IO field |
| 9 | + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 10 | + static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 11 | + static StringTokenizer st = new StringTokenizer(""); |
| 12 | + |
| 13 | + static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());} |
| 14 | + static String nextToken() throws Exception { |
| 15 | + while(!st.hasMoreTokens()) nextLine(); |
| 16 | + return st.nextToken(); |
| 17 | + } |
| 18 | + static int nextInt() throws Exception { return Integer.parseInt(nextToken()); } |
| 19 | + static long nextLong() throws Exception { return Long.parseLong(nextToken()); } |
| 20 | + static double nextDouble() throws Exception { return Double.parseDouble(nextToken()); } |
| 21 | + static void bwEnd() throws Exception {bw.flush();bw.close();} |
| 22 | + |
| 23 | + // Additional field |
| 24 | + |
| 25 | + static int N, M, S, E; |
| 26 | + static List<int[]>[] V; |
| 27 | + static boolean[] vis; |
| 28 | + |
| 29 | + static final int INF = (int)1e9 + 5; |
| 30 | + |
| 31 | + public static void main(String[] args) throws Exception { |
| 32 | + |
| 33 | + ready(); |
| 34 | + solve(); |
| 35 | + |
| 36 | + bwEnd(); |
| 37 | + |
| 38 | + } |
| 39 | + |
| 40 | + static void ready() throws Exception{ |
| 41 | + |
| 42 | + N = nextInt(); |
| 43 | + M = nextInt(); |
| 44 | + V = new List[N+1]; |
| 45 | + for(int i=1;i<=N;i++) V[i] = new ArrayList<>(); |
| 46 | + for(int i=0;i<M;i++) { |
| 47 | + int a = nextInt(), b = nextInt(), c = nextInt(); |
| 48 | + V[a].add(new int[] {b,c}); |
| 49 | + V[b].add(new int[] {a,c}); |
| 50 | + } |
| 51 | + for(int i=1;i<=N;i++) Collections.sort(V[i], (a,b) -> a[0]-b[0]); |
| 52 | + S = nextInt(); |
| 53 | + E = nextInt(); |
| 54 | + vis = new boolean[N+1]; |
| 55 | + |
| 56 | + } |
| 57 | + |
| 58 | + static void solve() throws Exception{ |
| 59 | + |
| 60 | + int ans = 0; |
| 61 | + { |
| 62 | + PriorityQueue<int[]> Q = new PriorityQueue<>((a,b) -> { |
| 63 | + if(a[0] == b[0]) return a[1]-b[1]; |
| 64 | + return a[0]-b[0]; |
| 65 | + }); |
| 66 | + int[] D = new int[N+1]; |
| 67 | + Arrays.fill(D, INF); |
| 68 | + Q.offer(new int[] {0,E}); |
| 69 | + D[E] = 0; |
| 70 | + while(!Q.isEmpty()) { |
| 71 | + int[] now = Q.poll(); |
| 72 | + int d = now[0], n = now[1]; |
| 73 | + if(d > D[n]) continue; |
| 74 | + for(int[] next:V[n]) { |
| 75 | + int i = next[0], c = next[1]; |
| 76 | + if(D[i] > d+c) { |
| 77 | + D[i] = d+c; |
| 78 | + Q.offer(new int[] {D[i],i}); |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + ans += D[S]; |
| 83 | + int g = S; |
| 84 | + while(g != E) { |
| 85 | + for(int[] i:V[g]) if(D[g] == i[1] + D[i[0]]) { |
| 86 | + g = i[0]; |
| 87 | + break; |
| 88 | + } |
| 89 | + if(g != E) vis[g] = true; |
| 90 | + } |
| 91 | + |
| 92 | + } |
| 93 | + |
| 94 | + |
| 95 | + PriorityQueue<int[]> Q = new PriorityQueue<>((a,b) -> { |
| 96 | + if(a[0] == b[0]) return a[1]-b[1]; |
| 97 | + return a[0]-b[0]; |
| 98 | + }); |
| 99 | + int[] D = new int[N+1]; |
| 100 | + Arrays.fill(D, INF); |
| 101 | + Q.offer(new int[] {0,E}); |
| 102 | + D[E] = 0; |
| 103 | + while(!Q.isEmpty()) { |
| 104 | + int[] now = Q.poll(); |
| 105 | + int d = now[0], n = now[1]; |
| 106 | + if(d > D[n]) continue; |
| 107 | + for(int[] next:V[n]) if(!vis[next[0]]) { |
| 108 | + int i = next[0], c = next[1]; |
| 109 | + if(D[i] > d+c) { |
| 110 | + D[i] = d+c; |
| 111 | + Q.offer(new int[] {D[i],i}); |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + bw.write((ans + D[S]) + "\n"); |
| 116 | + |
| 117 | + } |
| 118 | + |
| 119 | +} |
| 120 | + |
| 121 | +``` |
0 commit comments