Skip to content

Commit f2b8aef

Browse files
authored
승연 백준 숙제 (4/28/수 스터디) (#66)
* 11265 * 1446
1 parent b4ecf2d commit f2b8aef

File tree

2 files changed

+117
-0
lines changed

2 files changed

+117
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#solved.ac
2+
#실버1
3+
#플로이드 와샬
4+
#끝나지 않은 파티
5+
#11265
6+
7+
'''
8+
5 10
9+
0 4 4 8 7
10+
7 0 7 7 4
11+
1 4 0 5 4
12+
5 2 2 0 7
13+
1 4 1 6 0
14+
1 3 8
15+
2 4 1
16+
4 1 1
17+
1 5 5
18+
3 2 1
19+
3 2 5
20+
4 5 10
21+
5 3 2
22+
1 4 1
23+
1 4 11
24+
'''
25+
#플로이드 워셜로 풀이
26+
#플로이드 워셜은 a -> b로 갈떄
27+
#k라는 다른 경유지를 거쳤을 때를 모두 계산하는 방법임.
28+
#모든 a->b 경로에 대해 모든 k의 경우를 모두 계산
29+
#시간 복잡도 O(N^3)
30+
import sys
31+
input = sys.stdin.readline
32+
33+
N,M = map(int,input().split())
34+
#경로 맵 입력
35+
maps = []
36+
for i in range(N):
37+
maps.append(list(map(int,input().split())))
38+
39+
#플로이드 워셜 (경유지와 도착지, 경유지와 현위치, 도착지와 현위치가 같을 경우 배제)
40+
def floyd_warshall(N):
41+
for k in range(0,N):
42+
for a in range(0,N):
43+
if a==k: continue
44+
for b in range(0,N):
45+
if a==b or b==k: continue
46+
maps[a][b] = min(maps[a][b],maps[a][k]+maps[k][b])
47+
48+
floyd_warshall(N)
49+
50+
#정보 입력 (a->b,c:시간)
51+
for i in range(M):
52+
a,b,c = map(int,input().split())
53+
#도착 가능 여부 검사
54+
if maps[a-1][b-1]<=c:
55+
print('Enjoy other party')
56+
else:
57+
print('Stay here')

‎syheo/codingTest1/Baekjoon/1446.py‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#solved.ac
2+
#골드5
3+
#다익스트라
4+
#지름길
5+
#1446
6+
7+
#다익스트라
8+
9+
import sys, heapq
10+
11+
input = sys.stdin.readline
12+
INF = int(1e9)
13+
distance = [INF]*10001
14+
maps = [[] for _ in range(10001)]
15+
startNodes = set()
16+
N,D = map(int,input().split())
17+
18+
#그래프 정보 입력
19+
for i in range(N):
20+
a,b,c = map(int,input().split())
21+
startNodes.add(a)
22+
maps[a].append((b,c))
23+
#distance 초기화
24+
for i in range(10001):
25+
distance[i]=i
26+
27+
28+
#다익스트라
29+
def dijkstra(start,D):
30+
q = []
31+
heapq.heappush(q,(0,start))
32+
while q:
33+
cost,v = heapq.heappop(q)
34+
# #처리된 노드면 패스
35+
if distance[v] < cost:
36+
continue
37+
#v에서 갈 수 있는 지름길 체크
38+
for start in startNodes:
39+
#start가 이미 지난 지점이면 패스
40+
if start<v:
41+
continue
42+
for info in maps[start]:
43+
dest,c = info
44+
#목적지보다 길면 패스(역주행 안됨)
45+
if dest>D:
46+
continue
47+
#v에서 start까지 거리 계산해서 비용에 더해줌
48+
c += cost+start-v
49+
# 지름길 비용과 지름길로 안갔을 때를 비교
50+
if c < distance[v]+(dest-v):
51+
distance[dest]= min(distance[dest],c) #start->dest 가 여러개일 수 있음
52+
distance[D] = min(c+D-dest,distance[D]) # 0~v까지 비용 + 지름길 도착지 ~ D 까지 비용 과 비교
53+
heapq.heappush(q,(c,dest))
54+
55+
dijkstra(0,D)
56+
57+
print(distance[D])
58+
59+
60+

0 commit comments

Comments
 (0)