File tree Expand file tree Collapse file tree 3 files changed +90
-0
lines changed
Expand file tree Collapse file tree 3 files changed +90
-0
lines changed Original file line number Diff line number Diff line change 1+ import sys
2+
3+
4+ input = sys .stdin .readline
5+
6+ n = int (input ()) # 도시 개수
7+ m = int (input ()) # 버스 개수
8+
9+ graph = [[float ('inf' )] * n for _ in range (n )]
10+ for _ in range (m ):
11+ start , end , cost = map (int , input ().split ())
12+
13+ graph [start - 1 ][end - 1 ] = min (cost , graph [start - 1 ][end - 1 ])
14+
15+ for k in range (n ):
16+ for i in range (n ):
17+ for j in range (n ):
18+ if i == j :
19+ graph [i ][j ] = 0
20+ continue
21+ if graph [i ][k ] != float ('inf' ) and graph [k ][j ] != float ('inf' ):
22+ graph [i ][j ] = min (graph [i ][j ], graph [i ][k ] + graph [k ][j ])
23+
24+ for row in graph :
25+ res = []
26+ for x in row :
27+ if x == float ('inf' ):
28+ res .append (0 )
29+ else :
30+ res .append (x )
31+ print (* res )
Original file line number Diff line number Diff line change 1+ import sys
2+ import heapq
3+
4+ input = sys .stdin .readline
5+
6+ n , m = map (int , input ().split ())
7+ graph = [[] for _ in range (n + 1 )] # 인접리스트 (인접 행렬로 하면 메모리 초과)
8+
9+ for _ in range (m ):
10+ a , b , c = map (int , input ().split ())
11+ graph [a ].append ((b , c ))
12+ graph [b ].append ((a , c ))
13+
14+ cost = [float ('inf' )] * (n + 1 )
15+ cost [1 ] = 0
16+ heap = [(0 , 1 )] # (최소비용, 시작지점)
17+
18+ # 가중치가 변동이 있다면 우선순위 큐 사용한 다익스트라
19+ while heap :
20+ cur_cost , x = heapq .heappop (heap )
21+ if cur_cost > cost [x ]:
22+ continue
23+ for nx , w in graph [x ]:
24+ total = cur_cost + w
25+ if total < cost [nx ]:
26+ cost [nx ] = total
27+ heapq .heappush (heap , (total , nx ))
28+
29+ print (cost [n ])
Original file line number Diff line number Diff line change 1+ def solution (m , n , puddles ):
2+ graph = [[- 1 ] * m for _ in range (n )]
3+
4+ graph [0 ][0 ] = 1
5+
6+ # 웅덩이 체크
7+ for x , y in puddles :
8+ graph [y - 1 ][x - 1 ] = 0
9+
10+ # 첫 열 초기화
11+ for i in range (1 , n ):
12+ if graph [i ][0 ] == 0 :
13+ graph [i ][0 ] = 0
14+ else :
15+ graph [i ][0 ] = graph [i - 1 ][0 ]
16+
17+ # 첫 행 초기화
18+ for j in range (1 , m ):
19+ if graph [0 ][j ] == 0 :
20+ graph [0 ][j ] = 0
21+ else :
22+ graph [0 ][j ] = graph [0 ][j - 1 ]
23+
24+ # 웅덩이 제외 경로 계산
25+ for i in range (1 , n ):
26+ for j in range (1 , m ):
27+ if graph [i ][j ] != 0 :
28+ graph [i ][j ] = graph [i - 1 ][j ] + graph [i ][j - 1 ]
29+
30+ return graph [n - 1 ][m - 1 ] % 1000000007
You can’t perform that action at this time.
0 commit comments