diff --git a/khj20006/202502/06 BOJ G2 The Other Way.md b/khj20006/202502/06 BOJ G2 The Other Way.md new file mode 100644 index 00000000..c6ee6413 --- /dev/null +++ b/khj20006/202502/06 BOJ G2 The Other Way.md @@ -0,0 +1,112 @@ +```java + +import java.util.*; +import java.io.*; + +class Edge{ + int x; + long c; + Edge(int x, long c){ + this.x = x; + this.c = c; + } +} + +class Element { + long dist, cnt; + int node; + Element(long dist, long cnt, int node){ + this.dist = dist; + this.cnt = cnt; + this.node = node; + } +} + +class Main { + + // IO field + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); + static StringTokenizer st; + + static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());} + static int nextInt() {return Integer.parseInt(st.nextToken());} + static long nextLong() {return Long.parseLong(st.nextToken());} + static void bwEnd() throws Exception {bw.flush();bw.close();} + + // Additional field + static List[] V; + static long[] D; + static long[] C; + static int N, M, S, E; + static long mod = (long)1e9 + 9, INF = 1234567890123456789L; + + public static void main(String[] args) throws Exception { + + ready(); + solve(); + + bwEnd(); + } + + static void ready() throws Exception{ + + nextLine(); + N = nextInt(); + M = nextInt(); + S = nextInt(); + E = nextInt(); + V = new ArrayList[N+1]; + D = new long[N+1]; + + Arrays.fill(D, INF); + C = new long[N+1]; + for(int i=1;i<=N;i++) V[i] = new ArrayList<>(); + + for(int i=0;i PQ = new PriorityQueue<>((a,b) -> { + if(a.dist == b.dist) return 0; + if(a.dist < b.dist) return -1; + return 1; + }); + PQ.offer(new Element(0,1,start)); + while(!PQ.isEmpty()) { + Element now = PQ.poll(); + if(now.dist > D[now.node]) continue; + for(Edge e : V[now.node]) { + if(D[e.x] > now.dist + e.c) { + D[e.x] = now.dist + e.c; + C[e.x] = C[now.node]; + PQ.offer(new Element(D[e.x], C[e.x], e.x)); + } + else if(D[e.x] == now.dist + e.c) { + C[e.x] = (C[e.x] + C[now.node]) % mod; + } + } + } + + } + +} + +```