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