Skip to content

Commit c3c9a9d

Browse files
authored
[20250822] BOJ / P4 / 세금 / 이강현
1 parent dab09dd commit c3c9a9d

File tree

1 file changed

+108
-0
lines changed

1 file changed

+108
-0
lines changed

lkhyun/202508/22 BOJ P4 세금.md

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
```java
2+
import java.io.*;
3+
import java.util.*;
4+
5+
public class Main {
6+
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
7+
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
8+
static StringTokenizer st;
9+
10+
static class Edge{
11+
int to;
12+
int cnt;
13+
int cost;
14+
15+
Edge(int to, int cnt, int cost){
16+
this.to = to;
17+
this.cnt = cnt;
18+
this.cost = cost;
19+
}
20+
}
21+
static int N,M,K;
22+
static int S,D;
23+
static List<Edge>[] adjLists;
24+
static int[][] dist;
25+
26+
public static void main(String[] args) throws IOException {
27+
st = new StringTokenizer(br.readLine());
28+
N = Integer.parseInt(st.nextToken());
29+
M = Integer.parseInt(st.nextToken());
30+
K = Integer.parseInt(st.nextToken());
31+
32+
adjLists = new List[N+1];
33+
dist = new int[N+1][N+1];
34+
35+
for (int i = 1; i <= N; i++) {
36+
adjLists[i] = new ArrayList<>();
37+
Arrays.fill(dist[i], Integer.MAX_VALUE);
38+
}
39+
40+
st = new StringTokenizer(br.readLine());
41+
S = Integer.parseInt(st.nextToken());
42+
D = Integer.parseInt(st.nextToken());
43+
44+
for (int i = 0; i < M; i++) {
45+
st = new StringTokenizer(br.readLine());
46+
int from = Integer.parseInt(st.nextToken());
47+
int to = Integer.parseInt(st.nextToken());
48+
int cost = Integer.parseInt(st.nextToken());
49+
adjLists[from].add(new Edge(to, 0, cost));
50+
adjLists[to].add(new Edge(from, 0, cost));
51+
}
52+
53+
dijkstra(S);
54+
55+
int optIdx = 0;
56+
int ans = Integer.MAX_VALUE;
57+
for (int i = 1; i <= N; i++) {
58+
if(dist[D][i] < ans){
59+
ans = dist[D][i];
60+
optIdx = i;
61+
}
62+
}
63+
StringBuilder sb = new StringBuilder();
64+
sb.append(ans).append("\n");
65+
66+
int tax = 0;
67+
for (int i = 0; i < K; i++) {
68+
tax += Integer.parseInt(br.readLine());
69+
ans = Integer.MAX_VALUE;
70+
for (int j = 1; j <= N; j++) {
71+
if(dist[D][j] == Integer.MAX_VALUE) continue;
72+
int cur = dist[D][j] + (tax*j);
73+
if(cur < ans){
74+
ans = cur;
75+
optIdx = j;
76+
}
77+
}
78+
sb.append(ans).append("\n");
79+
}
80+
bw.write(sb.toString());
81+
bw.close();
82+
}
83+
84+
public static void dijkstra(int start){
85+
PriorityQueue<Edge> pq = new PriorityQueue<>((a,b) -> a.cost - b.cost);
86+
87+
dist[start][0] = 0;
88+
pq.add(new Edge(start,0,0));
89+
90+
while(!pq.isEmpty()){
91+
Edge cur = pq.poll();
92+
93+
if(dist[cur.to][cur.cnt] < cur.cost) continue;
94+
95+
for(Edge next : adjLists[cur.to]){
96+
int newCost = cur.cost + next.cost;
97+
int nextCnt = cur.cnt + 1;
98+
99+
if(nextCnt <= N && dist[next.to][nextCnt] > newCost){
100+
dist[next.to][nextCnt] = newCost;
101+
pq.offer(new Edge(next.to, nextCnt, newCost));
102+
}
103+
}
104+
}
105+
}
106+
}
107+
108+
```

0 commit comments

Comments
 (0)