전체 글
-
[BFS 알고리즘] 단지번호붙이기Algorithms/Breadth First Search 2021. 8. 14. 11:59
기출 : 백준 #2667 n = int(input()) board = list() for _ in range(n): board.append(input()) visited = [[False for __ in range(n)] for _ in range(n)] dx, dy = [1,-1,0,0], [0,0,1,-1] answer = [] def bfs(x,y): total = 1 visited[x][y] = True queue = list() queue.append([x,y]) while queue: cur = queue.pop(0) for dir in range(4): mx = cur[0] + dx[dir] my = cur[1] + dy[dir] if mx=n: continue if visited[mx][..
-
[다익스트라 알고리즘] 특정한 최단 경로Algorithms/Shortest Path 2021. 8. 13. 16:29
기출 : 백준 #1504 1 -> v1 -> v2 -> n 1 -> v2 -> v1 -> n 위의 두 가지 경로를 고려하여 최솟값을 출력하면 된다 from heapq import heappop, heappush INF = int(1e9) n, e = map(int,input().split()) graph = [[]for _ in range(n+1)] queue = list() for _ in range(e): x,y,w = map(int,input().split()) graph[x].append((w, y)) graph[y].append((w, x)) v1, v2 = map(int,input().split()) def dijkstra(start, end): distance = [INF for _ in r..
-
[다익스트라 알고리즘] 최소비용 구하기Algorithms/Shortest Path 2021. 8. 13. 16:07
기출 : 백준 #1916 일반적인 다익스트라 문제이다 from heapq import heappush, heappop INF = int(1e9) n = int(input()) m = int(input()) distance = [INF for _ in range(n+1)] graph = [[] for _ in range(n+1)] for _ in range(m): x, y, w = map(int,input().split()) graph[x].append([w,y]) start, end = map(int,input().split()) def djikstra(start): queue = list() distance[start] = 0 heappush(queue, (0,start)) while queue: cu..
-
[다익스트라 알고리즘] 최단경로Algorithms/Shortest Path 2021. 8. 13. 15:35
기출 : 백준 1753 일반적인 다익스트라 문제다 두 정점 사이에 여러 간선이 있을 수 있다는 점을 고려해야 한다 from heapq import heappush, heappop INF = int(1e9) v, e = map(int, input().split()) k = int(input()) distance = [INF for _ in range(v+1)] minVal = dict() graph = [[] for _ in range(v+1)] queue = list() for _ in range(e): x, y, w = map(int,input().split()) if x not in minVal: minVal[x] = dict() if y not in minVal[x]: minVal[x][y] = w..
-
[그리디 알고리즘] 신입 사원Algorithms/Greedy Algorithm 2021. 8. 8. 14:21
기출: 백준 1946 서류 등수를 기준으로 정렬한 다음, 면접 등수의 최솟값을 갱신하면서 하나씩 세면 된다 tc = int(input()) for t in range(tc): n = int(input()) ans = 1 cand = list() for i in range(n): cand.append(list(map(int,input().split()))) cand.sort(key = lambda x: x[0]) minVal = cand[0][1] for i in range(1,n): if minVal>cand[i][1]: ans += 1 minVal=cand[i][1] print(ans)
-
[그리디 알고리즘] 동전 0Algorithms/Greedy Algorithm 2021. 8. 8. 13:29
기출: 백준 11047 동전의 개수가 무한하고 큰 동전의 가치는 작은 동전 가치의 배수이다 n, k = map(int,input().split()) coins = list() for _ in range(n): coins.append(int(input())) ans = 0 coins.sort(reverse=True) for coin in coins: if k>0: ans += (k//coin) k -= (k//coin)*coin print(ans)
-
[그리디 알고리즘] ATMAlgorithms/Greedy Algorithm 2021. 8. 8. 13:26
기출 : 백준 11399 n번째 사람이 기다리는 시간을 D[n], 인출하는 시간을 t[n]이라고 하면 D[n] = D[n-1] + t[n]이다 D[0] + D[1] + D[2] + ... + D[n-1] = D[0] * n + D[1] * n-1 + ... 이다 n = int(input()) arr = list(map(int,input().split())) arr.sort() ans = 0 for i in range(len(arr)): ans += (arr[i]*(len(arr)-i)) print(ans)