Skip to content

Commit fb87766

Browse files
authored
[20250211] BOJ / 플래5 / 물약 / 권혁준
1 parent 19d7f0d commit fb87766

File tree

1 file changed

+122
-0
lines changed

1 file changed

+122
-0
lines changed
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
```java
2+
3+
import java.util.*;
4+
import java.io.*;
5+
6+
class Node {
7+
int cnt;
8+
String ingredient;
9+
Node(int cnt, String ingredient){
10+
this.cnt = cnt;
11+
this.ingredient = ingredient;
12+
}
13+
}
14+
15+
class Main {
16+
17+
// IO field
18+
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
19+
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
20+
static StringTokenizer st;
21+
22+
static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());}
23+
static int nextInt() {return Integer.parseInt(st.nextToken());}
24+
static long nextLong() {return Long.parseLong(st.nextToken());}
25+
static void bwEnd() throws Exception {bw.flush();bw.close();}
26+
27+
// Additional field
28+
static final int INF = 1000000001;
29+
30+
static int N, M;
31+
static HashMap<String, Integer> price = new HashMap<>();
32+
static String[] newIngredients;
33+
static List<Node>[] expressions;
34+
static boolean[] clear;
35+
36+
public static void main(String[] args) throws Exception {
37+
38+
ready();
39+
solve();
40+
41+
bwEnd();
42+
}
43+
44+
static void ready() throws Exception{
45+
46+
nextLine();
47+
N = nextInt();
48+
M = nextInt();
49+
for(int i=0;i<N;i++) {
50+
nextLine();
51+
price.put(st.nextToken(), nextInt());
52+
}
53+
54+
newIngredients = new String[M];
55+
expressions = new List[M];
56+
clear = new boolean[M];
57+
for(int i=0;i<M;i++) {
58+
String[] temp1 = br.readLine().split("=");
59+
newIngredients[i] = temp1[0];
60+
61+
String[] temp2 = temp1[1].split("\\+");
62+
expressions[i] = new ArrayList<>();
63+
for(int j=0;j<temp2.length;j++) {
64+
int cnt = Integer.parseInt(temp2[j].substring(0,1));
65+
String ingredient = temp2[j].substring(1,temp2[j].length());
66+
expressions[i].add(new Node(cnt, ingredient));
67+
}
68+
}
69+
70+
}
71+
72+
static void solve() throws Exception{
73+
74+
boolean change;
75+
76+
do {
77+
78+
change = false;
79+
for(int i=0;i<M;i++) {
80+
boolean allIngredientExists = true;
81+
for(Node node : expressions[i]) {
82+
allIngredientExists &= price.containsKey(node.ingredient);
83+
}
84+
if(!allIngredientExists) continue;
85+
int res = 0;
86+
for(Node node : expressions[i]) {
87+
if(res >= INF) break;
88+
int value = price.get(node.ingredient);
89+
for(int k=0;k<node.cnt;k++) {
90+
if(res >= INF) {
91+
res = INF;
92+
break;
93+
}
94+
res += value;
95+
}
96+
}
97+
if(price.containsKey(newIngredients[i])) {
98+
if(res < price.get(newIngredients[i])) {
99+
price.put(newIngredients[i], res);
100+
change = true;
101+
102+
}
103+
}
104+
else {
105+
price.put(newIngredients[i], res);
106+
change = true;
107+
}
108+
109+
110+
}
111+
112+
}while(change);
113+
114+
if(price.containsKey("LOVE")) bw.write(price.get("LOVE")+"\n");
115+
else bw.write("-1");
116+
117+
}
118+
119+
120+
}
121+
122+
```

0 commit comments

Comments
 (0)