Skip to content

Commit 90fa6db

Browse files
authored
Merge pull request #96 from AlgorithmWithGod/khj20006
[20250212] BOJ / P5 / 두 가중치 / 권혁준
2 parents 2e79e75 + 380470d commit 90fa6db

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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;
12+
13+
static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());}
14+
static int nextInt() {return Integer.parseInt(st.nextToken());}
15+
static long nextLong() {return Long.parseLong(st.nextToken());}
16+
static void bwEnd() throws Exception {bw.flush();bw.close();}
17+
18+
// Additional field
19+
static int[][] D;
20+
static int[][] cost1;
21+
static int[][] cost2;
22+
23+
static int N;
24+
static final int INF = (int)1e9 + 5;
25+
26+
public static void main(String[] args) throws Exception {
27+
28+
ready();
29+
solve();
30+
31+
bwEnd();
32+
}
33+
34+
static void ready() throws Exception{
35+
36+
N = Integer.parseInt(br.readLine());
37+
D = new int[N][N*9+1];
38+
for(int i=0;i<N;i++) Arrays.fill(D[i], INF);
39+
40+
cost1 = new int[N][N];
41+
for(int i=0;i<N;i++) {
42+
char[] arr = br.readLine().toCharArray();
43+
for(int j=0;j<N;j++) {
44+
if(arr[j] == '.') continue;
45+
cost1[i][j] = arr[j]-'0';
46+
}
47+
}
48+
49+
cost2 = new int[N][N];
50+
for(int i=0;i<N;i++) {
51+
char[] arr = br.readLine().toCharArray();
52+
for(int j=0;j<N;j++) {
53+
if(arr[j] == '.') continue;
54+
cost2[i][j] = arr[j]-'0';
55+
}
56+
}
57+
58+
}
59+
60+
static void solve() throws Exception{
61+
62+
PriorityQueue<int[]> PQ = new PriorityQueue<>((a,b) -> a[0]-b[0]);
63+
PQ.offer(new int[] {0,0,0});
64+
D[0][0] = 0;
65+
while(!PQ.isEmpty()) {
66+
int[] now = PQ.poll();
67+
int w = now[0], s = now[1], n = now[2];
68+
if(w > D[n][s]) continue;
69+
for(int i=0;i<N;i++) {
70+
if(cost1[n][i] == 0) continue;
71+
if(s+cost1[n][i] <= N*9 && D[i][s+cost1[n][i]] > w + cost2[n][i]) {
72+
D[i][s+cost1[n][i]] = w + cost2[n][i];
73+
PQ.offer(new int[] {w+cost2[n][i],s+cost1[n][i],i});
74+
}
75+
}
76+
}
77+
78+
int ans = INF;
79+
for(int i=0;i<=N*9;i++) if(D[1][i] != INF) ans = Math.min(ans, D[1][i]*i);
80+
bw.write((ans==INF ? -1 : ans) + "\n");
81+
82+
}
83+
84+
85+
}
86+
87+
```

0 commit comments

Comments
 (0)