Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions syheo/codingTest1/Baekjoon/1939.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#solved.ac
#골드4
#?
#중량제한
#1939

# 첫번쨰 아이디어
# 인접 행렬로 bfs만 돌음 -> 메모리초과
# 인접 리스트로 bfs만 돌음 -> 메모리 초과
# 중량 초과하면 간선 가중치만큼 길을 통과할 수 있다고 생각함.
# 중량 초과하면 아예 가질 못함
# 두번째 아이디어
# 중량을 이진탐색으로 선택, bfs 돌려서 갈 수 있는 경우와 못가는 경우로 left, right 조절

from collections import deque

INF = int(1e9)

def binary_search(A,B):
answer = 0
left = 0
right = INF
mid = (left+right)//2
while left<=right:
#방문 배열 초기화
visited = [False for _ in range(N+1)]
if bfs(A,B,mid,visited):
answer = max(mid,answer)
left = mid+1
else:
right = mid-1
mid = (left+right)//2
return answer

def bfs(A,B,mid,visited):
q = deque([A]) # 섬, 비용
visited[A]=True
while q:
land = q.popleft()
for info in graphs[land]:
dest = info[0]
destCost = info[1]
#문제를 잘못 이해함
#c = cost if destCost>=cost else destCost
#중량제한 통과
if mid<=destCost:
#방문했던 노드가 아니면
if not visited[dest]:
visited[dest]=True
if dest==B:
return True
else:
q.append(dest)

return False

#섬 개수, 도로 갯수
N, M = map(int,input().split())
#인접 리스트 초기화
graphs = [[] for _ in range(N+1)]
#인접 리스트 입력
for i in range(M):
a,b,c = map(int,input().split())
#a->b;c , b->a;c
graphs[a].append((b,c))
graphs[b].append((a,c))

# 공장이 있는 두 개의 섬
A,B = map(int,input().split())

# binarySearch and bfs
print(binary_search(A,B))
65 changes: 65 additions & 0 deletions syheo/codingTest1/Baekjoon/2469.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#solved.ac
#실버1
#?
#사다리 타기
#2469
# 10
# 5
# 첫번쨰 아이디어
# ????줄의 경우의 수를 bfs로 모두 구한 뒤 입력받은 결과와 비교 -> 시간초과
# 두번쨰 아이디어
# ????줄 기준 위 아래 결과를 구하고(first_sort,second_sort), ????줄에 의해 둘이 같아질 수 있는지 검사.

import sys
input=sys.stdin.readline

#??????줄이 나오기 전까지 sort, 나오면 해당 줄 리턴
def first_sort(n,k):
for i in range(n):
for j in range(k-1):
if ladders[i][j]=='?':
return i
elif ladders[i][j]=='-':
first_persons[j],first_persons[j+1]=first_persons[j+1],first_persons[j]

def second_sort(n,questionIdx):
for i in range(n-1,questionIdx,-1):
for j in range(k-1):
if ladders[i][j]=='-':
persons[j],persons[j+1]=persons[j+1],persons[j]


#사람 수 , 가로줄 수, 사다리 결과
k = int(input())
n = int(input())
persons = list(input())
#A~?(k 만큼 알파벳 솔팅)
first_persons = [chr(65+i) for i in range(k)]

ladders = []

for i in range(n):
ladders.append(list(input()))

questionIdx = first_sort(n,k)
second_sort(n,questionIdx)

isNoCase = False
res = ''
#정답 가능 줄 만들기
for i in range(k-1):
if first_persons[i]==persons[i]:
res += '*'
elif first_persons[i] == persons[i+1]:
res += '-'
elif i !=0 and first_persons[i] == persons[i-1] and res[i-1]=='-':
res += '*'
else:
isNoCase=True
break

if isNoCase:
for i in range(k-1):
print('x',end='')
else:
print(res)
40 changes: 40 additions & 0 deletions syheo/codingTest1/Baekjoon/5972-2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#solved.ac
#골드5
#?
#택배 배송
#5972

#다익스트라
import sys
import heapq
input = sys.stdin.readline

def bfs(N,dp):
q = []
heapq.heappush(q,(0,1)) #여물 수, 현재 위치
dp[1]=0
while q:
cost, node = heapq.heappop(q)
if dp[node]<cost:
continue
for info in graphs[node]:
dest = info[0]
c = info[1]
if dp[dest]>cost+c:
dp[dest]=cost+c
heapq.heappush(q,(cost+c,dest))


N,M = map(int,input().split())
INF = int(1e9)
graphs = [[] for _ in range(N+1)]
for i in range(M):
a,b,c = map(int,input().split())
graphs[a].append((b,c))
graphs[b].append((a,c))

dp = [INF for _ in range(N+1)]

bfs(N,dp)

print(dp[N])
40 changes: 40 additions & 0 deletions syheo/codingTest1/Baekjoon/5972.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#solved.ac
#골드5
#?
#택배 배송
#5972

#bfs+다익스트라 일부

from collections import deque

def bfs(N,dp):
q = deque([(1,0)]) #현재 위치, 여물 수
dp[1]=0
while q:
print(q)
node, cost = q.popleft()
if dp[node]<cost:
continue
for info in graphs[node]:
dest = info[0]
c = info[1]
if dp[dest]>cost+c:
dp[dest]=cost+c
q.append((dest,cost+c))


N,M = map(int,input().split())
INF = int(1e9)
graphs = [[] for _ in range(N+1)]
for i in range(M):
a,b,c = map(int,input().split())
graphs[a].append((b,c))
graphs[b].append((a,c))

dp = [INF for _ in range(N+1)]

bfs(N,dp)

print(dp[N])