Skip to content

Commit 1fe2d57

Browse files
committed
[20250827] BOJ / G3 / ACM Craft / 김민진
1 parent 90417ce commit 1fe2d57

File tree

2 files changed

+93
-1
lines changed

2 files changed

+93
-1
lines changed

zinnnn37/202508/26 BOJ G1 동전 뒤집기.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,17 @@ public class BJ_1285_동전_뒤집기 {
3535
}
3636

3737
private static void sol() throws IOException {
38-
// 행 뒤집기
38+
// 뒤집는 경우의 수(열)
3939
for (int rowFillped = 0; rowFillped < (1 << N); rowFillped++) {
4040
// 뒷면
4141
int totalTails = 0;
4242

4343
for (int row = 0; row < N; row++) {
4444
// 0일 때 뒤집지 않고(0), 1일 때 뒤집을 때(1) 0이 되어야 해서 XOR 연산
45+
// 여기서 열 뒤집기
4546
int tailsInCol = Integer.bitCount(coins[row] ^ rowFillped);
47+
48+
// 행은 직접적으로 뒤집지 않고 뒷면의 개수를 비교해서 확인
4649
totalTails += Math.min(tailsInCol, N - tailsInCol);
4750
}
4851
ans = Math.min(ans, totalTails);
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
```java
2+
import java.io.*;
3+
import java.util.*;
4+
5+
public class BJ_1005_ACM_Craft {
6+
7+
private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
8+
private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
9+
private static final StringBuilder sb = new StringBuilder();
10+
private static StringTokenizer st;
11+
12+
private static int N;
13+
private static int K;
14+
private static int W;
15+
private static int[] dp;
16+
private static int[] time;
17+
private static int[] inDegree;
18+
19+
private static Queue<Integer> q;
20+
private static List<Integer>[] graph;
21+
22+
public static void main(String[] args) throws IOException {
23+
int T = Integer.parseInt(br.readLine());
24+
25+
while (T-- > 0) {
26+
init();
27+
sol();
28+
}
29+
bw.write(sb.toString());
30+
bw.flush();
31+
bw.close();
32+
br.close();
33+
}
34+
35+
private static void init() throws IOException {
36+
st = new StringTokenizer(br.readLine());
37+
N = Integer.parseInt(st.nextToken());
38+
K = Integer.parseInt(st.nextToken());
39+
40+
dp = new int[N + 1];
41+
time = new int[N + 1];
42+
st = new StringTokenizer(br.readLine());
43+
for (int i = 1; i <= N; i++) {
44+
time[i] = Integer.parseInt(st.nextToken());
45+
}
46+
47+
graph = new ArrayList[N + 1];
48+
for (int i = 1; i <= N; i++) {
49+
graph[i] = new ArrayList<>();
50+
}
51+
52+
inDegree = new int[N + 1];
53+
for (int i = 1; i <= K; i++) {
54+
st = new StringTokenizer(br.readLine());
55+
int u = Integer.parseInt(st.nextToken());
56+
int v = Integer.parseInt(st.nextToken());
57+
58+
graph[u].add(v);
59+
inDegree[v]++;
60+
}
61+
62+
W = Integer.parseInt(br.readLine());
63+
q = new ArrayDeque<>();
64+
}
65+
66+
private static void sol() throws IOException {
67+
for (int i = 1; i <= N; i++) {
68+
// 시작 건물
69+
if (inDegree[i] == 0) {
70+
dp[i] = time[i];
71+
q.offer(i);
72+
}
73+
}
74+
75+
while (!q.isEmpty()) {
76+
int cur = q.poll();
77+
78+
for (int nxt : graph[cur]) {
79+
dp[nxt] = Math.max(dp[nxt], dp[cur] + time[nxt]);
80+
if (--inDegree[nxt] == 0) {
81+
q.offer(nxt);
82+
}
83+
}
84+
}
85+
sb.append(dp[W]).append("\n");
86+
}
87+
88+
}
89+
```

0 commit comments

Comments
 (0)