목록2024/01 (30)
파란색가운의 개발 블로그
import sys # sys.stdin.readline() from collections import deque sys.setrecursionlimit(100000000) import heapq import copy import itertools from itertools import combinations from itertools import permutations INF = 1e9 def findMin(): dx = [-1,0,1,0] dy = [0,1,0,-1] # 상하좌우 움직일 수 있으므로!! q = [] heapq.heappush(q,(graph[0][0],0,0)) # 비용, row, col (start) while q: dist,row,col = heapq.heappop(q) # 힙에서..
import sys # sys.stdin.readline() from collections import deque sys.setrecursionlimit(100000000) import heapq import copy import itertools from itertools import combinations from itertools import permutations INF = 1e9 N,M = map(int,sys.stdin.readline().split()) graph = [[INF] *(N+1) for _ in range(N+1)] for _ in range(M): a,b = map(int,sys.stdin.readline().split()) graph[a][b] = 1 # 정순이면 a < b ..
https://www.acmicpc.net/problem/11404 11404번: 플로이드 첫째 줄에 도시의 개수 n이 주어지고 둘째 줄에는 버스의 개수 m이 주어진다. 그리고 셋째 줄부터 m+2줄까지 다음과 같은 버스의 정보가 주어진다. 먼저 처음에는 그 버스의 출발 도시의 번호가 www.acmicpc.net 문제 n(2 ≤ n ≤ 100)개의 도시가 있다. 그리고 한 도시에서 출발하여 다른 도시에 도착하는 m(1 ≤ m ≤ 100,000)개의 버스가 있다. 각 버스는 한 번 사용할 때 필요한 비용이 있다. 모든 도시의 쌍 (A, B)에 대해서 도시 A에서 B로 가는데 필요한 비용의 최솟값을 구하는 프로그램을 작성하시오. 입력 첫째 줄에 도시의 개수 n이 주어지고 둘째 줄에는 버스의 개수 m이 주어진다..
import sys # sys.stdin.readline() from collections import deque sys.setrecursionlimit(100000000) import heapq import copy import itertools from itertools import combinations from itertools import permutations INF = 1e9 def djikstra(start): q = [] distance[start] = 0 heapq.heappush(q,(0,start)) # 0은 거리 start는 노드 번호 while q: dist,now = heapq.heappop(q) if distance[now] < dist: # 갱신할 필요 없음 continue..
import sys # sys.stdin.readline() from collections import deque sys.setrecursionlimit(100000000) import heapq import copy import itertools from itertools import combinations from itertools import permutations INF = 1e9 N,M = map(int,sys.stdin.readline().split()) graph = [[INF] *(N+1) for _ in range(N+1)] for _ in range(M): a,b = map(int,sys.stdin.readline().split()) graph[a][b] = 1 graph[b][a] =..
사용되는 경우: 모든 지점에서 다른 모든 지점까지의 최단 경로 다익스트라 알고리즘과의 차이점: 다익스트라 : 시작점이 정해져 있다 . 그리디 알고리즘 사용 / 시간복잡도 O(ElogV) 플로이드 워셜 알고리즘: 시작점 정해지지 않음. 다이나믹 프로그래밍과 유사 / 시간복잡도 O(N^3) import sys # sys.stdin.readline() from collections import deque sys.setrecursionlimit(100000000) import heapq import copy import itertools from itertools import combinations from itertools import permutations INF = 1e9 n = int(input()) m..
한번 개념 공부하고 문제도 풀었었지만 여전히 까먹는다 까먹지 않도록 장기기억으로 가져가도록 하자 기본 코드는 정말정말 기본이 되어야 하므로 이걸 올리고도 한번 더 해봐야겠다는 생각이 들었다 한줄 한줄 이게 왜 존재해야 하는 코드인지 다시 생각해봐야겠다. 시간복잡도 : O(ElogV) import sys # sys.stdin.readline() from collections import deque sys.setrecursionlimit(100000000) import heapq import copy import itertools from itertools import combinations from itertools import permutations INF = 1e9 def djikstra(start):..
https://www.acmicpc.net/problem/14501 14501번: 퇴사 첫째 줄에 백준이가 얻을 수 있는 최대 이익을 출력한다. www.acmicpc.net 문제 상담원으로 일하고 있는 백준이는 퇴사를 하려고 한다. 오늘부터 N+1일째 되는 날 퇴사를 하기 위해서, 남은 N일 동안 최대한 많은 상담을 하려고 한다. 백준이는 비서에게 최대한 많은 상담을 잡으라고 부탁을 했고, 비서는 하루에 하나씩 서로 다른 사람의 상담을 잡아놓았다. 각각의 상담은 상담을 완료하는데 걸리는 기간 Ti와 상담을 했을 때 받을 수 있는 금액 Pi로 이루어져 있다. N = 7인 경우에 다음과 같은 상담 일정표를 보자. 1일2일3일4일5일6일7일TiPi 3 5 1 1 2 4 2 10 20 10 20 15 40 20..
https://www.acmicpc.net/problem/1932 문제 1932번: 정수 삼각형 첫째 줄에 삼각형의 크기 n(1 ≤ n ≤ 500)이 주어지고, 둘째 줄부터 n+1번째 줄까지 정수 삼각형이 주어진다. www.acmicpc.net 7 3 8 8 1 0 2 7 4 4 4 5 2 6 5 위 그림은 크기가 5인 정수 삼각형의 한 모습이다. 맨 위층 7부터 시작해서 아래에 있는 수 중 하나를 선택하여 아래층으로 내려올 때, 이제까지 선택된 수의 합이 최대가 되는 경로를 구하는 프로그램을 작성하라. 아래층에 있는 수는 현재 층에서 선택된 수의 대각선 왼쪽 또는 대각선 오른쪽에 있는 것 중에서만 선택할 수 있다. 삼각형의 크기는 1 이상 500 이하이다. 삼각형을 이루고 있는 각 수는 모두 정수이며,..
import sys # sys.stdin.readline() from collections import deque sys.setrecursionlimit(100000000) import heapq import copy import itertools from itertools import combinations from itertools import permutations INF = 1e9 def findMax(element): arr = [[0 for _ in range(m)] for _ in range(n)] index = 0 maxarray =[] for i in range(n): for j in range(m): arr[i][j] = (element[index]) index +=1 print(arr..