Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 | 29 |
30 | 31 |
Tags
- 프로그래머스 #정수삼각형 #동적계획법
- 디스코드 봇
- 파이썬
- supabase
- 2156
- 갓생
- 캠스터디
- C언어로 쉽게 풀어쓴 자료구조
- discord.js
- react
- content script
- 포도주시식
- 자료구조
- popup
- 백준
- TypeScript
- Message Passing
- webpack
- Chrome Extension
- nodejs
- 백준 7579
- 공부시간측정어플
- X
- 동적계획법
- 백준 #7568번 #파이썬 #동적계획법
- 크롬 익스텐션
- 크롬 확장자
- background script
Archives
- Today
- Total
히치키치
[백준] 1939번 : 중량제한 - Python(파이썬) 본문
1939번: 중량제한
첫째 줄에 N, M(1≤M≤100,000)이 주어진다. 다음 M개의 줄에는 다리에 대한 정보를 나타내는 세 정수 A, B(1≤A, B≤N), C(1≤C≤1,000,000,000)가 주어진다. 이는 A번 섬과 B번 섬 사이에 중량제한이 C인 다리
www.acmicpc.net
풀이
- BFS : dequeue 사용 : 가중치 제한, 도착, 끝 & visit 리스트 : 경로 기록, 한번의 이동 체크
- 이진탐색 : 이동 가능한 최대 중량
코드
import sys
from collections import deque
input=sys.stdin.readline
n,m=map(int, input().split())
adj=[[] for _ in range(n+1)]
for _ in range(m):
a,b,w=map(int,input().split())
adj[a].append((b,w))
adj[b].append((a,w))
start,end=map(int,input().split())
min_=1
max_=1000000000
def bfs(c):
queue=deque()
queue.append(start)
visit=[False]*(n+1)
visit[start]=True
while queue:
x=queue.popleft()
for y,weight in adj[x]:
if not visit[y] and c<=weight:
queue.append(y)
visit[y]=True
return visit[end]
while min_<=max_:
mid=(min_+max_)//2
if bfs(mid):
result=mid
min_=mid+1
else:
max_=mid-1
print(result)
햇갈렸던 점
#(1) 양방향으로 오고 갈 수 있으므로 간선을 두번 저장
for _ in range(m):
a,b,w=map(int,input().split())
adj[a].append((b,w))
adj[b].append((a,w))
#(2) list는 zero-indexing이므로 섬 번호로 깔끔하게 인덱스를 매기고 싶으면 n+1해줘야함
#(3) BFS 알고리즘 -> 시작점부터 끝 점까지 중량 제한을 모두 확인
#(4) deqeue -> 양쪽으로 효율적으로 추가/삭제, append : 오른쪽 추가, leftpop : 왼쪽 삭제
#(5) visit -> 방문:True, 방문 X : False
def bfs(c):
queue=deque()
queue.append(start)
visit=[False]*(n+1)
visit[start]=True
while queue:
x=queue.popleft()
for y,weight in adj[x]:
if not visit[y] and c<=weight:
queue.append(y)
visit[y]=True
return visit[end]
'알고리즘 스터디' 카테고리의 다른 글
[백준] 11399번 : ATM - Python(파이썬) (0) | 2021.03.30 |
---|---|
[백준] 16234번 : 인구이동 - Python(파이썬) (0) | 2021.03.29 |
[백준] 1339번 : 단어수학 - Python(파이썬) (0) | 2021.03.22 |
[백준] 2630번 : 색종이 만들기 - Python(파이썬) (0) | 2021.03.22 |
[백준] 17829번 : 222-풀링 - Python(파이썬) (0) | 2021.03.22 |
Comments