1+ # n = 3
2+ # start = 1
3+ # end = 3
4+ # roads = [[1, 2, 2], [3, 2, 3]]
5+ # traps = [2]
6+ n = 4
7+ start = 1
8+ end = 4
9+ roads = [[1 , 2 , 1 ], [3 , 2 , 1 ], [2 , 4 , 1 ]]
10+ traps = [2 , 3 ]
11+
12+ INF = int (1e9 )
13+
14+ from collections import deque
15+ import heapq
16+
17+ def change_direction2 (target ,graph ,n ):
18+ changed_graph = graph [:]
19+ appendList = []
20+ for i in range (len (changed_graph )):
21+ info = changed_graph [i ]
22+ if info [0 ]== target or info [1 ]== target :
23+ changed_graph [i ]= (info [1 ],info [0 ],info [2 ])
24+ return changed_graph
25+
26+ def change_direction (target ,graph ,n ):
27+ changed_graph = graph [:]
28+ changedList = []
29+ for i in range (len (changed_graph [target ])):
30+ info = changed_graph [target ][i ]
31+ if info [2 ]== 1 :
32+ changed_graph [target ][i ]= (info [0 ],info [1 ],- 1 )
33+ elif info [2 ]== - 1 :
34+ changed_graph [target ][i ]= (info [0 ],info [1 ],1 )
35+ changedList .append (info [0 ])
36+
37+ for i in changedList :
38+ for j in range (len (changed_graph [i ])):
39+ info = changed_graph [i ][j ]
40+ if info [0 ]== target :
41+ if info [2 ]== 1 :
42+ changed_graph [i ][j ]= (info [0 ],info [1 ],- 1 )
43+ elif info [2 ]== - 1 :
44+ changed_graph [i ][j ]= (info [0 ],info [1 ],1 )
45+ changedList .append (info [0 ])
46+
47+ return changed_graph
48+
49+ def dijkstra (start ,end ,traps ,graph ,n ,distances ):
50+ q = []
51+ heapq .heappush (q ,(0 ,start ,graph [:]))
52+ distances [start ].append (0 )
53+ while q :
54+ dist , now ,changed_graph = heapq .heappop (q )
55+ if min (distances [now ]) < dist :
56+ continue
57+ for i in changed_graph [now ]:
58+ if i [2 ]== 1 :
59+ cost = dist + i [1 ]
60+ tmp_dst = distances [i [0 ]][now ]
61+ if cost < tmp_dst :
62+ distances [i [0 ]][now ]= cost
63+ if i [0 ] in traps :
64+ heapq .heappush (q ,(cost ,i [0 ],change_direction (i [0 ],changed_graph ,n )))
65+ else :
66+ heapq .heappush (q ,(cost ,i [0 ],change_direction (i [0 ],changed_graph ,n )))
67+
68+
69+ def bfs (start ,end ,traps ,graph ,n ):
70+ q = deque ()
71+ cntList = []
72+ #start,end,cost
73+ q .append ((start ,end ,0 ,graph [:],'' ))
74+ while q :
75+ a ,b ,cost ,change_map ,visited = q .popleft ()
76+ for info in change_map [a ]:
77+ #print(a,b,cost,change_map,visited)
78+ if str (a )+ str (info [0 ]) not in visited and info [2 ]== 1 :
79+ v = info [0 ]
80+ c = info [1 ]
81+ if v == end :
82+ return cost + c
83+ else :
84+ if v in traps :
85+ q .append ((v ,end ,cost + c ,change_direction (v ,change_map ,n ),visited + str (v )))
86+ else :
87+ q .append ((v ,end ,cost + c ,change_map ,visited + str (v )))
88+
89+ return min (cntList )
90+
91+
92+
93+ def solution (n , start , end , roads , traps ):
94+ graph = [[] for i in range (n + 1 )]
95+ distance = [[INF for _ in range (n + 1 )] for i in range (n + 1 )]
96+ distances = [[ INF for i in range (n + 1 )] for _ in range (n + 1 )]
97+
98+ #visited = [[[False for j in range(n+1)] for i in range(n+1)] for _ in range(n+1)]
99+ for road in roads :
100+ #1-> , -1<-
101+ if distance [road [0 ]][road [1 ]]> road [2 ]:
102+ graph [road [0 ]].append ((road [1 ],road [2 ],1 ))
103+ distance [road [0 ]][road [1 ]]= road [2 ]
104+ graph [road [1 ]].append ((road [0 ],road [2 ],- 1 ))
105+ distance [road [1 ]][road [0 ]]= road [2 ]
106+ #다익스트라 알고리즘 수행
107+ dijkstra (start ,end ,traps ,graph ,n ,distances )
108+ print (distances )
109+ return min (distances [end ])
110+
111+
112+ print (solution (n ,start ,end ,roads ,traps ))
0 commit comments