Skip to content

Commit

Permalink
Merge branch 'main' into 81-tgyuuAn
Browse files Browse the repository at this point in the history
  • Loading branch information
tgyuuAn authored Nov 20, 2024
2 parents 6a86310 + 3a46632 commit a22fd08
Show file tree
Hide file tree
Showing 5 changed files with 189 additions and 2 deletions.
6 changes: 4 additions & 2 deletions H0ngJu/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
| 26차시 | 2024.08.24 | 그리디 | [신입사원](https://www.acmicpc.net/problem/1946) | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/237 |
| 27차시 | 2024.08.27 | DFS | [트리의 지름](https://www.acmicpc.net/problem/1967) | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/240 |
| 28차시 | 2024.09.04 | 벨만포드 | [타임머신](https://www.acmicpc.net/problem/11657) | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/244 |
| 29차시 | 2024.09.06 | 구현 |[톱니바퀴](https://www.acmicpc.net/problem/14891) | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/245 |
| 29차시 | 2024.09.06 | 구현 | [톱니바퀴](https://www.acmicpc.net/problem/14891) | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/245 |
| 30차시 | 2024.09.27 | 수학 | [Fly me to the Alpha Centauri](https://www.acmicpc.net/problem/1011) | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/248 |
| 31차시 | 2024.10.05 | 정렬 | [선 긋기](https://www.acmicpc.net/problem/2170) | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/249 |
| 31차시 | 2024.10.05 | 정렬 | [선 긋기](https://www.acmicpc.net/problem/2170) | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/249 |
| 32차시 | 2024.10.14 | 이분 탐색 | [기타 레슨](https://www.acmicpc.net/problem/2343) | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/252 |
| 33차시 | 2024.11.08 | 백트래킹 | [병원 거리 최소화](https://www.codetree.ai/training-field/frequent-problems/problems/min-of-hospital-distance/submissions?page=11&pageSize=5) | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/253 |
112 changes: 112 additions & 0 deletions H0ngJu/백트래킹/병원 거리 최소화.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import sys

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

n, m = map(int, input().split())
hospital = []
person = []
info = [[] for _ in range(n)]

# 사람, 병원 정보 받기
for i in range(n):
info[i].extend(list(map(int, input().split())))
for j in range(n):
if info[i][j] == 2:
hospital.append((i,j))
if info[i][j] == 1:
person.append((i,j))

distance = [[0 for _ in range(len(hospital))] for _ in range(len(person))]

for i in range(len(person)):
px, py = person[i]
for j in range(len(hospital)):
hx, hy = hospital[j]
distance[i][j] = abs(px-hx) + abs(py-hy)

# hospital 위치 중에 m개를 선택해서 최솟값 구하기
hospital_arr = [i for i in range(len(hospital))]
all_combinations = []

def combination(start, depth, combination_arr):
if depth == m: # m만큼 찾으면 return
all_combinations.append(combination_arr[:])
return
for i in range(start, len(hospital_arr)):
combination_arr.append(hospital_arr[i]) # 원소 일단 넣고
combination(i + 1, depth + 1, combination_arr) # 재귀 돌림
combination_arr.pop() # 다시 뽑은거 복귀 -> [1,2,3]을 구하면, 3빼고 다시 다음 반복문 넣어서 [1,2,4] 가능

combination(0, 0, [])

min_distance_sum = 1e9
for hospitals_combination in all_combinations:
cur_distance = 0

for i in range(len(person)):
dist = 1e9
for j in hospitals_combination:
dist = min(dist, distance[i][j])
cur_distance += dist

min_distance_sum = min(min_distance_sum, cur_distance)

print(min_distance_sum)

# --- combination -----

# import sys
# import itertools

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

# n, m = map(int, input().split())
# hospital = []
# person = []
# info = [[] for _ in range(n)]

# # 사람, 병원 정보 받기
# for i in range(n):
# info[i].extend(list(map(int, input().split())))
# for j in range(n):
# if info[i][j] == 2:
# hospital.append((i,j))
# if info[i][j] == 1:
# person.append((i,j))

# distance = [[0 for _ in range(len(hospital))] for _ in range(len(person))]

# for i in range(len(person)):
# px, py = person[i]
# for j in range(len(hospital)):
# hx, hy = hospital[j]
# distance[i][j] = abs(px-hx) + abs(py-hy)

# # hospital 위치 중에 m개를 선택해서 최솟값 구하기

# min_distance_sum = 1e9
# combbination_arr = [i for i in range(len(hospital))]

# for hospitals_combination in itertools.combinations(combbination_arr, m):
# cur_distance = 0

# for i in range(len(person)):
# dist = 1e9
# for j in hospitals_combination:
# dist = min(dist, distance[i][j])
# cur_distance += dist

# min_distance_sum = min(min_distance_sum, cur_distance)

# print(min_distance_sum)

# # 빈자리면 0, 사람이면 1, 병원이면 2
# # 출력은 최소 거리

# # 병원의 수 만큼 배열을 생성해서, 각 사람마다의 최소 거리를 계산한다.
# # 각 병원의 거리 총 합을 계산해서 정렬한다. -> 문제 : 사람마다 다른 병원이 최소 거리일 수 있지만, 해당 가능성을 무시하게 됨

# # 각 사람마다 각 병원의 거리를 계산한다.
# # [[1,2,4,5], [1,2,4,5]]의 형태
# # 병원 중 m개를 고르는 조합
# # 조합에서 최소거리 갱신하여 찾기
28 changes: 28 additions & 0 deletions H0ngJu/이분탐색/기타 레슨.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import sys

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

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

left = max(lecture)
right = sum(lecture)

while left <= right:
mid = (left+right)//2
record_cnt = 1
time = 0 # 레코드 하나에 들어가는 시간

for i in range(N):
if time + lecture[i] > mid: # 시간 초과되면 다음 레코드로
record_cnt += 1
time = lecture[i]
else:
time += lecture[i]

if record_cnt <= M: # 깅의가 남아있으면 크기 줄이기
right = mid - 1
else:
left = mid + 1

print(left)
1 change: 1 addition & 0 deletions tgyuuAn/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,5 +86,6 @@
| 77차시 | 2024.09.27 | 구현 | <a href="https://school.programmers.co.kr/learn/courses/30/lessons/150366">표 병합</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/247
| 78차시 | 2024.10.06 | 그리디 | <a href="https://school.programmers.co.kr/learn/courses/30/lessons/68646">풍선 터뜨리기</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/250
| 79차시 | 2024.10.12 | 이분 매칭 | <a href="https://www.acmicpc.net/problem/9576">책 나눠주기</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/251
| 80차시 | 2024.11.11 | 다익스트라 | <a href="https://www.acmicpc.net/problem/11779">최소비용 구하기 2</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/254
| 81차시 | 2024.11.15 | 이분 탐색 | <a href="https://www.acmicpc.net/problem/1701">Cubeeditor</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/255
---
44 changes: 44 additions & 0 deletions tgyuuAn/다익스트라/최소비용 구하기 2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from heapq import *
from collections import defaultdict
import sys

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

N = int(input())
M = int(input())
bus_info = defaultdict(lambda : defaultdict(lambda : int(1e9)))

for _ in range(M):
n1, n2, cost = map(int,input().split())

if bus_info[n1][n2] > cost:
bus_info[n1][n2] = cost

start, destination = map(int, input().split())
heap = []
heappush(heap, [0, 0, start])

previous = [1 for _ in range(N+1)]
cost = [int(1e9) for _ in range(N+1)]
while heap:
now_cost, previous_node, now_node = heappop(heap)

if cost[now_node] <= now_cost: continue
previous[now_node] = previous_node
cost[now_node] = now_cost

for node in bus_info[now_node]:
temp = now_cost + bus_info[now_node][node]
if cost[node] > temp:
heappush(heap, [temp, now_node, node])

print(cost[destination])

history = []
now_history = destination
while now_history != 0:
history.append(now_history)
now_history = previous[now_history]

print(len(history))
print(" ".join(map(str, history[-1::-1])))

0 comments on commit a22fd08

Please sign in to comment.