File tree 4 files changed +217
-0
lines changed
syheo/codingTest1/Baekjoon
4 files changed +217
-0
lines changed Original file line number Diff line number Diff line change
1
+ #solved.ac
2
+ #골드4
3
+ #?
4
+ #중량제한
5
+ #1939
6
+
7
+ # 첫번쨰 아이디어
8
+ # 인접 행렬로 bfs만 돌음 -> 메모리초과
9
+ # 인접 리스트로 bfs만 돌음 -> 메모리 초과
10
+ # 중량 초과하면 간선 가중치만큼 길을 통과할 수 있다고 생각함.
11
+ # 중량 초과하면 아예 가질 못함
12
+ # 두번째 아이디어
13
+ # 중량을 이진탐색으로 선택, bfs 돌려서 갈 수 있는 경우와 못가는 경우로 left, right 조절
14
+
15
+ from collections import deque
16
+
17
+ INF = int (1e9 )
18
+
19
+ def binary_search (A ,B ):
20
+ answer = 0
21
+ left = 0
22
+ right = INF
23
+ mid = (left + right )// 2
24
+ while left <= right :
25
+ #방문 배열 초기화
26
+ visited = [False for _ in range (N + 1 )]
27
+ if bfs (A ,B ,mid ,visited ):
28
+ answer = max (mid ,answer )
29
+ left = mid + 1
30
+ else :
31
+ right = mid - 1
32
+ mid = (left + right )// 2
33
+ return answer
34
+
35
+ def bfs (A ,B ,mid ,visited ):
36
+ q = deque ([A ]) # 섬, 비용
37
+ visited [A ]= True
38
+ while q :
39
+ land = q .popleft ()
40
+ for info in graphs [land ]:
41
+ dest = info [0 ]
42
+ destCost = info [1 ]
43
+ #문제를 잘못 이해함
44
+ #c = cost if destCost>=cost else destCost
45
+ #중량제한 통과
46
+ if mid <= destCost :
47
+ #방문했던 노드가 아니면
48
+ if not visited [dest ]:
49
+ visited [dest ]= True
50
+ if dest == B :
51
+ return True
52
+ else :
53
+ q .append (dest )
54
+
55
+ return False
56
+
57
+ #섬 개수, 도로 갯수
58
+ N , M = map (int ,input ().split ())
59
+ #인접 리스트 초기화
60
+ graphs = [[] for _ in range (N + 1 )]
61
+ #인접 리스트 입력
62
+ for i in range (M ):
63
+ a ,b ,c = map (int ,input ().split ())
64
+ #a->b;c , b->a;c
65
+ graphs [a ].append ((b ,c ))
66
+ graphs [b ].append ((a ,c ))
67
+
68
+ # 공장이 있는 두 개의 섬
69
+ A ,B = map (int ,input ().split ())
70
+
71
+ # binarySearch and bfs
72
+ print (binary_search (A ,B ))
Original file line number Diff line number Diff line change
1
+ #solved.ac
2
+ #실버1
3
+ #?
4
+ #사다리 타기
5
+ #2469
6
+ # 10
7
+ # 5
8
+ # 첫번쨰 아이디어
9
+ # ????줄의 경우의 수를 bfs로 모두 구한 뒤 입력받은 결과와 비교 -> 시간초과
10
+ # 두번쨰 아이디어
11
+ # ????줄 기준 위 아래 결과를 구하고(first_sort,second_sort), ????줄에 의해 둘이 같아질 수 있는지 검사.
12
+
13
+ import sys
14
+ input = sys .stdin .readline
15
+
16
+ #??????줄이 나오기 전까지 sort, 나오면 해당 줄 리턴
17
+ def first_sort (n ,k ):
18
+ for i in range (n ):
19
+ for j in range (k - 1 ):
20
+ if ladders [i ][j ]== '?' :
21
+ return i
22
+ elif ladders [i ][j ]== '-' :
23
+ first_persons [j ],first_persons [j + 1 ]= first_persons [j + 1 ],first_persons [j ]
24
+
25
+ def second_sort (n ,questionIdx ):
26
+ for i in range (n - 1 ,questionIdx ,- 1 ):
27
+ for j in range (k - 1 ):
28
+ if ladders [i ][j ]== '-' :
29
+ persons [j ],persons [j + 1 ]= persons [j + 1 ],persons [j ]
30
+
31
+
32
+ #사람 수 , 가로줄 수, 사다리 결과
33
+ k = int (input ())
34
+ n = int (input ())
35
+ persons = list (input ())
36
+ #A~?(k 만큼 알파벳 솔팅)
37
+ first_persons = [chr (65 + i ) for i in range (k )]
38
+
39
+ ladders = []
40
+
41
+ for i in range (n ):
42
+ ladders .append (list (input ()))
43
+
44
+ questionIdx = first_sort (n ,k )
45
+ second_sort (n ,questionIdx )
46
+
47
+ isNoCase = False
48
+ res = ''
49
+ #정답 가능 줄 만들기
50
+ for i in range (k - 1 ):
51
+ if first_persons [i ]== persons [i ]:
52
+ res += '*'
53
+ elif first_persons [i ] == persons [i + 1 ]:
54
+ res += '-'
55
+ elif i != 0 and first_persons [i ] == persons [i - 1 ] and res [i - 1 ]== '-' :
56
+ res += '*'
57
+ else :
58
+ isNoCase = True
59
+ break
60
+
61
+ if isNoCase :
62
+ for i in range (k - 1 ):
63
+ print ('x' ,end = '' )
64
+ else :
65
+ print (res )
Original file line number Diff line number Diff line change
1
+ #solved.ac
2
+ #골드5
3
+ #?
4
+ #택배 배송
5
+ #5972
6
+
7
+ #다익스트라
8
+ import sys
9
+ import heapq
10
+ input = sys .stdin .readline
11
+
12
+ def bfs (N ,dp ):
13
+ q = []
14
+ heapq .heappush (q ,(0 ,1 )) #여물 수, 현재 위치
15
+ dp [1 ]= 0
16
+ while q :
17
+ cost , node = heapq .heappop (q )
18
+ if dp [node ]< cost :
19
+ continue
20
+ for info in graphs [node ]:
21
+ dest = info [0 ]
22
+ c = info [1 ]
23
+ if dp [dest ]> cost + c :
24
+ dp [dest ]= cost + c
25
+ heapq .heappush (q ,(cost + c ,dest ))
26
+
27
+
28
+ N ,M = map (int ,input ().split ())
29
+ INF = int (1e9 )
30
+ graphs = [[] for _ in range (N + 1 )]
31
+ for i in range (M ):
32
+ a ,b ,c = map (int ,input ().split ())
33
+ graphs [a ].append ((b ,c ))
34
+ graphs [b ].append ((a ,c ))
35
+
36
+ dp = [INF for _ in range (N + 1 )]
37
+
38
+ bfs (N ,dp )
39
+
40
+ print (dp [N ])
Original file line number Diff line number Diff line change
1
+ #solved.ac
2
+ #골드5
3
+ #?
4
+ #택배 배송
5
+ #5972
6
+
7
+ #bfs+다익스트라 일부
8
+
9
+ from collections import deque
10
+
11
+ def bfs (N ,dp ):
12
+ q = deque ([(1 ,0 )]) #현재 위치, 여물 수
13
+ dp [1 ]= 0
14
+ while q :
15
+ print (q )
16
+ node , cost = q .popleft ()
17
+ if dp [node ]< cost :
18
+ continue
19
+ for info in graphs [node ]:
20
+ dest = info [0 ]
21
+ c = info [1 ]
22
+ if dp [dest ]> cost + c :
23
+ dp [dest ]= cost + c
24
+ q .append ((dest ,cost + c ))
25
+
26
+
27
+ N ,M = map (int ,input ().split ())
28
+ INF = int (1e9 )
29
+ graphs = [[] for _ in range (N + 1 )]
30
+ for i in range (M ):
31
+ a ,b ,c = map (int ,input ().split ())
32
+ graphs [a ].append ((b ,c ))
33
+ graphs [b ].append ((a ,c ))
34
+
35
+ dp = [INF for _ in range (N + 1 )]
36
+
37
+ bfs (N ,dp )
38
+
39
+ print (dp [N ])
40
+
You can’t perform that action at this time.
0 commit comments