[λ°±μ€€] 14938 μ„œκ°•κ·ΈλΌμš΄λ“œ (python 파이썬)

2022. 9. 10. 21:41·🧩 Problem Solving/[λ°±μ€€]

https://www.acmicpc.net/problem/14938

 

14938번: μ„œκ°•κ·ΈλΌμš΄λ“œ

μ˜ˆμ€μ΄λŠ” μš”μ¦˜ κ°€μž₯ 인기가 μžˆλŠ” κ²Œμž„ μ„œκ°•κ·ΈλΌμš΄λ“œλ₯Ό 즐기고 μžˆλ‹€. μ„œκ°•κ·ΈλΌμš΄λ“œλŠ” μ—¬λŸ¬ 지역쀑 ν•˜λ‚˜μ˜ 지역에 λ‚™ν•˜μ‚°μ„ 타고 λ‚™ν•˜ν•˜μ—¬, κ·Έ 지역에 λ–¨μ–΄μ Έ μžˆλŠ” μ•„μ΄ν…œλ“€μ„ μ΄μš©ν•΄ μ„œλ°”μ΄λ²Œμ„

www.acmicpc.net


쑰건만 잘 μƒκ°ν•˜λ©΄ λ˜λŠ” μ°©ν•œ 문제.


아이디어

1. λ‹€μ΅μŠ€νŠΈλΌ

  • μΆœλ°œμ§€λ‘œ λΆ€ν„° μ΅œλ‹¨κ±°λ¦¬λ₯Ό κ³„μ‚°ν•΄μ„œ μˆ˜μƒ‰ λ²”μœ„μ•ˆμ— λ“€μ–΄κ°€λŠ”μ§€ ν™•μΈν•˜λ©΄ λœλ‹€.
  • μˆ˜μƒ‰ λ²”μœ„μ•ˆμ— λ“€μ–΄μ˜€λ©΄ μ•„μ΄ν…œ 개수λ₯Ό 더해주면 λœλ‹€.
  • 1 ~ nκΉŒμ§€ for문으둜 μΆœλ°œμ§€λ₯Ό λ°”κΎΈλ©΄μ„œ νƒμƒ‰ν•˜λ©΄ λœλ‹€.

2. ν”Œλ‘œμ΄λ“œ μ›Œμ…œ

  • μ§€μ—­μ˜ 개수(100)κ°€ μ μœΌλ―€λ‘œ ν”Œλ‘œμ΄λ“œ μ›Œμ…œλ‘œ 풀어도 μ‹œκ°„μ΄ˆκ³Όμ— 걸리지 μ•ŠλŠ”λ‹€.

전체 μ½”λ“œ

λ‹€μ΅μŠ€νŠΈλΌ

import heapq
import sys
INF = int(1e9)
n, m, r = map(int, input().split())

items = [0] + [int(x) for x in sys.stdin.readline().split()]

graph = [[] for _ in range(n + 1)]

for _ in range(r):
    a, b, l = map(int, sys.stdin.readline().split())
    graph[a].append([b, l])
    graph[b].append([a, l])

def dijkstra(start):
    que = []
    heapq.heappush(que, (0, start))
    D[start] = 0

    while que:
        dist, now = heapq.heappop(que)
        if D[now] < dist:
            continue

        for i in graph[now]:
            cost = dist + i[1]
            if cost < D[i[0]]:
                D[i[0]] = cost
                heapq.heappush(que, (cost, i[0]))

ans = 0
for i in range(1, n + 1):
    D = [INF] * (n + 1)
    dijkstra(i)
    tmp = 0
    for d in range(1, n + 1):
        if m >= D[d]:
            tmp += items[d]

    ans = max(ans, tmp)

print(ans)

 

ν”Œλ‘œμ΄λ“œ μ›Œμ…œ

import sys
INF = int(1e9)
n, m, r = map(int, input().split())

items = [0] + [int(x) for x in sys.stdin.readline().split()]

graph = [[INF] * (n + 1) for _ in range(n + 1)]

for _ in range(r):
    a, b, l = map(int, sys.stdin.readline().split())
    graph[a][b] = l
    graph[b][a] = l

for i in range(n + 1):
    graph[i][i] = 0

for k in range(1, n + 1):
    for i in range(1, n + 1):
        for j in range(1, n + 1):
            graph[i][j] = min(graph[i][j], graph[i][k] + graph[k][j])

ans = 0
for i in range(1, n + 1):
    tmp = 0
    for j in range(1, n + 1):
        if graph[i][j] <= m:
            tmp += items[j]

    ans = max(ans, tmp)

print(ans)

 


μ½”λ“œ μ„€λͺ…

λ‹€μ΅μŠ€νŠΈλΌ

ans = 0
for i in range(1, n + 1):
    D = [INF] * (n + 1)
    dijkstra(i)
    tmp = 0
    for d in range(1, n + 1):
        if m >= D[d]:
            tmp += items[d]

    ans = max(ans, tmp)

print(ans)

for문을 μ‚¬μš©ν•΄μ„œ 각 μΆœλ°œμ§€ λ§ˆλ‹€ μ–Όλ§ˆλ‚˜ μ•„μ΄ν…œμ„ λͺ¨μ„ 수 μžˆλŠ”μ§€ κ³„μ‚°ν•œλ‹€.

 

μ €μž₯ν•œ μ΅œλŒ“κ°’μ„ 좜λ ₯ν•΄μ£Όλ©΄ λœλ‹€.

 

ν”Œλ‘œμ΄λ“œ μ›Œμ…œ

ans = 0
for i in range(1, n + 1):
    tmp = 0
    for j in range(1, n + 1):
        if graph[i][j] <= m:
            tmp += items[j]

    ans = max(ans, tmp)

μœ„μ™€ 방식은 λ˜‘κ°™λ‹€. 

μ €μž‘μžν‘œμ‹œ (μƒˆμ°½μ—΄λ¦Ό)

'🧩 Problem Solving > [λ°±μ€€]' μΉ΄ν…Œκ³ λ¦¬μ˜ λ‹€λ₯Έ κΈ€

[λ°±μ€€] 11559 puyopuyo (파이썬 python)  (0) 2023.03.26
[λ°±μ€€] 2583 μ˜μ—­ κ΅¬ν•˜κΈ°(python 파이썬)  (0) 2023.03.16
[λ°±μ€€]11660 ꡬ간 ν•© κ΅¬ν•˜κΈ° 5 (python 파이썬)  (0) 2022.08.19
[λ°±μ€€] 10942 νŒ°λ¦°λ“œλ‘¬? (python 파이썬)  (0) 2022.08.17
[λ°±μ€€] 1647 λ„μ‹œ λΆ„ν•  κ³„νš (python 파이썬)  (0) 2022.08.01
'🧩 Problem Solving/[λ°±μ€€]' μΉ΄ν…Œκ³ λ¦¬μ˜ λ‹€λ₯Έ κΈ€
  • [λ°±μ€€] 11559 puyopuyo (파이썬 python)
  • [λ°±μ€€] 2583 μ˜μ—­ κ΅¬ν•˜κΈ°(python 파이썬)
  • [λ°±μ€€]11660 ꡬ간 ν•© κ΅¬ν•˜κΈ° 5 (python 파이썬)
  • [λ°±μ€€] 10942 νŒ°λ¦°λ“œλ‘¬? (python 파이썬)
μ œλ΄‰μ•„
μ œλ΄‰μ•„
  • μ œλ΄‰μ•„
    Overthinking
    μ œλ΄‰μ•„
    fake it till you make it.
  • 전체
    였늘
    μ–΄μ œ
    • λΆ„λ₯˜ 전체보기 (106)
      • 🧩 Problem Solving (83)
        • [λ°±μ€€] (74)
        • [ν”„λ‘œκ·Έλž˜λ¨ΈμŠ€] (7)
        • [SW Expert Academy] (1)
        • [μ•Œκ³ λ¦¬μ¦˜ for PS] (1)
      • πŸ“¦ Data Structure (2)
      • πŸ“œ Language (14)
        • [python] (14)
      • πŸ–€ Git (1)
      • πŸŒ† 일상 (4)
        • πŸ’¬ 벽보고 λ§ν•˜κΈ° (4)
      • πŸ—„οΈ 기타 (2)
      • πŸ”΅ css (0)
  • λΈ”λ‘œκ·Έ 메뉴

    • ν™ˆ
    • νƒœκ·Έ
    • λ°©λͺ…둝
  • 링크

  • 곡지사항

  • 인기 κΈ€

  • νƒœκ·Έ

    κ΅¬ν˜„
    νˆ¬ν¬μΈν„°
    DP
    μŠ€νƒ
    λΆ€λΆ„ν•©
    λ‹€μ΅μŠ€νŠΈλΌ
    냅색
    μž¬κ·€
    파이썬
    ν”„λ‘œκ·Έλž˜λ¨ΈμŠ€
    브루트포슀
    SWEA
    μ •μ²˜κΈ°
    νŒ°λ¦°λ“œλ‘¬
    Bruteforce
    boj
    imos
    λΆ„ν•  정볡
    ν”Œλ‘œμ΄λ“œμ›Œμ…œ
    λˆ„μ ν•©
    λ°λΈŒμ½”μŠ€
    μœ„μƒμ •λ ¬
    그리디
    slicing
    Python
    λ°±νŠΈλž˜ν‚Ή
    λ°±μ€€
    DFS
    BFS
    ν”Œλ‘œμ΄λ“œ 와샬
  • 졜근 λŒ“κΈ€

  • 졜근 κΈ€

  • hELLOΒ· Designed Byμ •μƒμš°.v4.10.3
μ œλ΄‰μ•„
[λ°±μ€€] 14938 μ„œκ°•κ·ΈλΌμš΄λ“œ (python 파이썬)
μƒλ‹¨μœΌλ‘œ

ν‹°μŠ€ν† λ¦¬νˆ΄λ°”