diff --git "a/H0ngJu/DFS/\354\271\234\352\265\254\353\271\204.py" "b/H0ngJu/DFS/\354\271\234\352\265\254\353\271\204.py" new file mode 100644 index 00000000..4eaac63b --- /dev/null +++ "b/H0ngJu/DFS/\354\271\234\352\265\254\353\271\204.py" @@ -0,0 +1,38 @@ +import sys +from collections import deque, defaultdict + +def input() : return sys.stdin.readline().rstrip() + +N, M, k = map(int, input().split()) +friend_money = list(map(int, input().split())) +relation = defaultdict(set) +visited = [0] * N +total = 0 + +for _ in range(M): + a, b = map(int, input().split()) + relation[a-1].add(b-1) + relation[b-1].add(a-1) + +for i in range(N): + if not visited[i]: + visited[i] = True + friends = [] + q = deque([i]) + + while q: + f = q.popleft() + friends.append(f) + for p in relation[f]: + if not visited[p]: + visited[p] = True + q.append(p) + + if friends: + cost = min(friend_money[friend] for friend in friends) + total += cost + +if total <= k: + print(total) +else: + print("Oh no") diff --git a/H0ngJu/README.md b/H0ngJu/README.md index d0ad3845..957950e0 100644 --- a/H0ngJu/README.md +++ b/H0ngJu/README.md @@ -26,5 +26,7 @@ | 22차시 | 2024.08.06 | 해시 | [의상](https://school.programmers.co.kr/learn/courses/30/lessons/42578) | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/224 | | 23차시 | 2024.08.10 | 해시 | [베스트앨범](https://school.programmers.co.kr/learn/courses/30/lessons/42579) | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/227 | | 24차시 | 2024.08.17 | BFS | [아기상어](https://www.acmicpc.net/problem/16236) | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/233 | +| 25차시 | 2024.08.17 | DFS | [친구비](https://www.acmicpc.net/problem/16562) | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/234 | +| 26차시 | 2024.08.24 | 그리디 | [신입사원](https://www.acmicpc.net/problem/1946) | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/237 | --- diff --git "a/H0ngJu/\352\267\270\353\246\254\353\224\224/\354\213\240\354\236\205\354\202\254\354\233\220.py" "b/H0ngJu/\352\267\270\353\246\254\353\224\224/\354\213\240\354\236\205\354\202\254\354\233\220.py" new file mode 100644 index 00000000..21d9e280 --- /dev/null +++ "b/H0ngJu/\352\267\270\353\246\254\353\224\224/\354\213\240\354\236\205\354\202\254\354\233\220.py" @@ -0,0 +1,19 @@ +import sys + +def input(): return sys.stdin.readline().rstrip() + +T = int(input()) + +for _ in range(T): + N = int(input()) + apply = [list(map(int, input().split())) for _ in range(N)] + apply = sorted(apply) + top = 0 + cnt = 1 + + for i in range(1, N): + if apply[i][1] < apply[top][1]: + top = i + cnt += 1 + + print(cnt) \ No newline at end of file diff --git a/alstjr7437/BFS/DSLR.py b/alstjr7437/BFS/DSLR.py new file mode 100644 index 00000000..4a953c7d --- /dev/null +++ b/alstjr7437/BFS/DSLR.py @@ -0,0 +1,36 @@ +from collections import deque +import sys + +input = sys.stdin.readline + +T = int(input()) + +def command(register, cmd): + if cmd == 0: + return ((register * 2) % 10000, "D") + elif cmd == 1: + return (9999 if register == 0 else register - 1, "S") + elif cmd == 2: + return (register % 1000 * 10 + register // 1000, "L") + elif cmd == 3: + return (register // 10 + (register % 10) * 1000, "R") + +for _ in range(T): + A, B = map(int, input().split()) + + visited = [False] * 10000 + visited[A] = True + + queue = deque([(A, "")]) + while queue: + cur, result = queue.popleft() + + if cur == B: + print(result) + break + + for i in range(4): + register, cmd = command(cur, i) + if not visited[register]: + visited[register] = True + queue.append((register, result + cmd)) \ No newline at end of file diff --git a/alstjr7437/README.md b/alstjr7437/README.md index ecfd040c..bd7fb9fd 100644 --- a/alstjr7437/README.md +++ b/alstjr7437/README.md @@ -31,6 +31,7 @@ | 27차시 | 2024.05.26 | 우선순위 큐 | 이중 우선순위 큐 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/198 | | 28차시 | 2024.05.30 | 브루트 포스 | 카잉 달력 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/203 | | 29차시 | 2024.06.11 | 이분 탐색 | 나무 자르기 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/210 | -| 30차시 | 2024.06.19 | 방 번호 | 구현 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/211 | -| 31차시 | 2024.06.19 | 게임 맵 최단거리 | BFS | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/218 | -| 32차시 | 2024.08.15 | 과일 탕후루 | 투 포인터 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/230 | \ No newline at end of file +| 30차시 | 2024.06.19 | 구현 | 방 번호 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/211 | +| 31차시 | 2024.06.19 | BFS | 게임 맵 최단거리 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/218 | +| 32차시 | 2024.08.15 | 투 포인터 | 과일 탕후루 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/230 | +| 33차시 | 2024.08.21 | BFS | DSLR | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/236 | \ No newline at end of file diff --git a/tgyuuAn/README.md b/tgyuuAn/README.md index dfd14f36..aea5b0d5 100644 --- a/tgyuuAn/README.md +++ b/tgyuuAn/README.md @@ -77,5 +77,6 @@ | 68차시 | 2024.08.06 | 그리디 | 가희와 탑 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/226 | 69차시 | 2024.08.10 | 누적합, 수학 | 1의 개수 세기 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/228 | 70차시 | 2024.08.16 | 스택 | 탑 보기 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/232 +| 71차시 | 2024.08.20 | 다익스트라 | 다익스트라 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/235 | 72차시 | 2024.08.23 | DFS + 트리 | 등산 마니아 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/238 --- diff --git "a/tgyuuAn/\353\213\244\354\235\265\354\212\244\355\212\270\353\235\274/\355\232\241\353\213\250\353\263\264\353\217\204.py" "b/tgyuuAn/\353\213\244\354\235\265\354\212\244\355\212\270\353\235\274/\355\232\241\353\213\250\353\263\264\353\217\204.py" new file mode 100644 index 00000000..a7cec57d --- /dev/null +++ "b/tgyuuAn/\353\213\244\354\235\265\354\212\244\355\212\270\353\235\274/\355\232\241\353\213\250\353\263\264\353\217\204.py" @@ -0,0 +1,40 @@ +from collections import defaultdict +from heapq import * +import sys + +def input(): return sys.stdin.readline().rstrip() +MAX = 100_000 * 700_000 + 1 +N, M = map(int, input().split()) + +graph = defaultdict(lambda : defaultdict(list)) + +for idx in range(1,M+1): + start, destination = map(int, input().split()) + graph[start][destination].append(idx) + graph[destination][start].append(idx) + +costs = [MAX for _ in range(N+1)] +visited = set() +heap = [(0, 1)] +while heap: + now_cost, now_idx = heappop(heap) + + if now_idx in visited: continue + visited.add(now_idx) + + if costs[now_idx] <= now_cost: continue + costs[now_idx] = now_cost + + if now_idx == N: break + + for neighbor in graph[now_idx]: + if neighbor in visited: continue + + for neighbor_idx in graph[now_idx][neighbor]: + + real_idx = now_cost % M + if real_idx < neighbor_idx: + heappush(heap, (neighbor_idx - real_idx + now_cost, neighbor)) + else: + heappush(heap, (now_cost + M - real_idx + neighbor_idx, neighbor)) +print(costs[-1]) \ No newline at end of file