File tree Expand file tree Collapse file tree 6 files changed +199
-5
lines changed Expand file tree Collapse file tree 6 files changed +199
-5
lines changed Original file line number Diff line number Diff line change 1+ n , m = map (int , input ().split ())
2+
3+ arr = [[] for _ in range (n + 1 )]
4+
5+ for i in range (m ):
6+ a , b , c = map (int , input ().split ())
7+ arr [a ].append ([b , c ])
8+ arr [b ].append ([a , b ])
9+
10+ for i in range (1 , n + 1 ):
11+ print (arr [i ])
12+
13+ start , end = map (int , input ().split ())
14+
15+
16+ # 플로이드 워셜 반대로?
17+ # for m in range(1, n+1):
18+ # for s in range(1, n+1):
19+ # for e in range(1, n+1):
20+ # if d[s][e] > d[s][m] + d[m][e]:
21+ # d[s][e] = d[s][m] + d[m][e]
22+
23+
24+ #print(d)
Original file line number Diff line number Diff line change 1+ import sys
2+ import heapq
3+ INF = 1e9
4+ n , m = map (int , input ().split ())
5+ arr = [[] * (n + 1 ) for _ in range (n + 1 )]
6+ dis = [INF ] * (n + 1 )
7+
8+ for _ in range (m ):
9+ a , b , c = map (int , input ().split ())
10+ arr [a ].append ((b , c ))
11+ arr [b ].append ((a , c ))
12+
13+ def dijkstra (start ):
14+ q = []
15+ heapq .heappush (q , (0 , start )) #비용, 출발점
16+ dis [start ] = 0
17+ while q :
18+ d , now = heapq .heappop (q )
19+ if dis [now ] < d :
20+ continue
21+ for v , w in arr [now ]: # 가려는 곳에대한 위치, 비용
22+ cost = d + w
23+ if cost < dis [v ]:
24+ dis [v ] = cost
25+ heapq .heappush (q , (cost , v ))
26+
27+ dijkstra (1 )
28+ print (dis [n ])
Original file line number Diff line number Diff line change 11def solution (bridge_length , weight , truck_weights ):
2+ time = 0
3+ on_bridge = []
4+ on_time = []
25
3- answer = 0
6+ while (truck_weights or on_bridge ) :
7+ time += 1
48
9+ if (on_time ) :
10+ if (on_time [0 ] + bridge_length == time ) :
11+ on_bridge .pop (0 )
12+ on_time .pop (0 )
13+ if (truck_weights ) :
14+ if (sum (on_bridge ) + truck_weights [0 ] <= weight ) :
15+ on_bridge .append (truck_weights .pop (0 ))
16+ on_time .append (time )
517
6-
7- return answer
8-
18+ return time
919'''
1020트럭 무게를 정렬할 필요는 없고
1121그냥 순서대로
22+ 아 무조건 정해진 순서대로 가야한다..
1223'''
13-
Original file line number Diff line number Diff line change 1+ n = int (input ())
2+ k = int (input ())
3+
4+ arr = [[0 ]* n for _ in range (n + 1 )]
5+ order = []
6+ count = 0
7+ dir = 0
8+
9+ for i in range (k ):
10+ a , b = map (int , input ().split ())
11+ arr [a ][b ] = 1
12+
13+ l = int (input ())
14+
15+ for i in range (l ):
16+ # x = int(input().split())
17+ # c = input()
18+ x , c = input ().split ()
19+ order .append ((int (x ), c ))
20+
21+ # for i in range(n):
22+ # print(arr[i])
23+ # print(order)
24+ # order.pop()
25+ #print(order)
26+
27+ #오른쪽 볼때 좌, 우 회전 순서는 각각 우 상 좌 하 / 우 하 좌 상
28+ #왼쪽 '' 좌 하 우 상 / 좌 상 우 하
29+
30+ dx = [0 , - 1 , 0 , 1 ] #우 상 좌 하
31+ dy = [1 , 0 , - 1 , 0 ]
32+
33+ x , y = 1 , 1
34+ q = [(x , y )]
35+
36+ while True :
37+ count += 1
38+
39+ arr [x ][y ] = 2
40+
41+
42+
43+ nx = x + dx [dir ]
44+ ny = y + dy [dir ]
45+
46+ if 1 <= nx <= n and 1 <= ny <= n and arr [nx ][ny ] != 2 :
47+ if arr [nx ][ny ] == 0 :
48+ arr [nx ][ny ] = 2
49+ q .append ((nx , ny ))
50+ ex , ey = q .pop (0 )
51+ arr [ex ][ey ] = 0
52+
53+ elif arr [nx ][ny ] == 1 :
54+ arr [nx ][ny ] = 2
55+ q .append ((nx , ny ))
56+
57+ x , y = nx , ny
58+
59+ else :
60+ break
61+
62+ if count == order [0 ][0 ]:
63+ if order [0 ][1 ] == 'L' : #왼쪽회전
64+ dir = (dir + 1 ) % 4
65+ else : #오른쪽회전
66+ dir = (dir + 3 ) % 4
67+
68+ order .pop ()
69+
70+ print (count )
71+
72+
73+
74+
75+
76+
77+
78+
79+
80+
81+ '''
82+ 6
83+ 3
84+ 3 4
85+ 2 5
86+ 5 3
87+ 3
88+ 3 D
89+ 15 L
90+ 17 D
91+ '''
Original file line number Diff line number Diff line change 1+ from itertools import combinations
2+ n , m = map (int , input ().split ())
3+
4+ arr = []
5+ chicken = []
6+ home = []
7+
8+ for i in range (n ):
9+ temp = list (map (int , input ().split ()))
10+ for j in range (n ):
11+ if temp [j ] == 1 :
12+ home .append ((i , j ))
13+ elif temp [j ] == 2 :
14+ chicken .append ((i , j ))
15+
16+ m_chicken = list (combinations (chicken , m ))
17+
18+ def get_sum (candidate ):
19+ result = 0
20+ for hx , hy in home :
21+ temp = 1e9
22+ for cx , cy in candidate :
23+ temp = min (temp , abs (hx - cx ) + abs (hy - cy ))
24+ result += temp
25+ return result
26+
27+ result = 1e9
28+ for i in m_chicken :
29+ result = min (result , get_sum (i ))
30+
31+
32+ print (result )
33+
34+ '''
35+ 5 3
36+ 0 0 1 0 0
37+ 0 0 2 0 1
38+ 0 1 2 0 0
39+ 0 0 1 0 0
40+ 0 0 0 0 2
41+ '''
You can’t perform that action at this time.
0 commit comments