Skip to content

Commit

Permalink
Merge pull request #160 from AlgoLeadMe/39-Munbin-Lee
Browse files Browse the repository at this point in the history
39-Munbin-Lee
  • Loading branch information
MunbinLee authored May 3, 2024
2 parents 124514a + a69d572 commit a6ee561
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Munbin-Lee/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,6 @@
| 35차시 | 2024.02.18 | 백트래킹 | <a href="https://www.acmicpc.net/problem/24891">단어 마방진</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/140 |
| 36차시 | 2024.02.21 | 문자열 | <a href="https://www.acmicpc.net/problem/15927">회문은 회문아니야!!</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/143 |
| 37차시 | 2024.03.05 | 백트래킹 | <a href="https://school.programmers.co.kr/learn/courses/30/lessons/250136">석유 시추</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/150 |
| 37차시 | 2024.03.08 | 트라이 | <a href="https://www.acmicpc.net/problem/5052">전화번호 목록</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/155 |
| 38차시 | 2024.03.08 | 트라이 | <a href="https://www.acmicpc.net/problem/5052">전화번호 목록</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/155 |
| 39차시 | 2024.03.14 | 문자열 | <a href="https://www.acmicpc.net/problem/1050">물약</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/160 |
---
45 changes: 45 additions & 0 deletions Munbin-Lee/문자열/39-물약.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
stdin = open(0)

def input():
return stdin.readline().rstrip()

N, M = map(int, input().split())

prices = {}

for _ in range(N):
item, price = input().split()
prices[item] = int(price)

recipes = []

for _ in range(M):
target, formula = input().split('=')
terms = formula.split('+')
recipes.append([target, terms])

def updatePrice(target, terms):
price = 0

for term in terms:
count = int(term[0])
item = term[1:]

if item not in prices:
return

price += count * prices[item]

if target not in prices or prices[target] > price:
prices[target] = price

for _ in range(M):
for recipe in recipes:
updatePrice(recipe[0], recipe[1])

if 'LOVE' not in prices:
print('-1')
exit()

answer = min(prices['LOVE'], 1000000001)
print(answer)

0 comments on commit a6ee561

Please sign in to comment.