-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #185 from AlgoLeadMe/54-tgyuuAn
- Loading branch information
Showing
2 changed files
with
68 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
from collections import defaultdict | ||
from heapq import * | ||
import sys | ||
|
||
INF = 50_000_001 | ||
|
||
def input(): return sys.stdin.readline().rstrip() | ||
|
||
T = int(input()) | ||
|
||
for _ in range(T): | ||
cnt_node, cnt_edge, cnt_dedication = map(int, input().split()) | ||
start_node, g, h = map(int, input().split()) | ||
|
||
graph = defaultdict(lambda : defaultdict(int)) | ||
for _ in range(cnt_edge): | ||
start, destination, cost = map(int,input().split()) | ||
graph[start][destination] = cost | ||
graph[destination][start] = cost | ||
|
||
node_dedications = set() | ||
for _ in range(cnt_dedication): | ||
node_dedications.add(int(input())) | ||
|
||
table = [INF for _ in range(cnt_node+1)] | ||
table[start_node] = 0 | ||
|
||
visited = set() | ||
heap = [] | ||
heappush(heap, [0, start_node, 1]) | ||
flag = [1 for _ in range(cnt_node+1)] | ||
|
||
while heap: | ||
now_cost, now_node, now_flag = heappop(heap) | ||
|
||
if now_node in visited: continue | ||
visited.add(now_node) | ||
|
||
if flag[now_node] == 1 and now_flag == 0: flag[now_node] = 0 | ||
|
||
for next_node in graph[now_node]: | ||
cost = graph[now_node][next_node] | ||
|
||
if (now_cost + cost) > table[next_node]: continue | ||
|
||
table[next_node] = now_cost + cost | ||
|
||
new_flag = now_flag | ||
# ๋ง์ฝ, ์ง๊ธ ์์ง์ด๋ ค๋ ๋๋ก๊ฐ ๋์๊ฐ ๋๋ ๋๋ก์์ ๊ฒฝ์ฐ flag๋ฅผ 0๋ก ๋ฐ๊ฟ | ||
if (now_node, next_node) in {(g, h), (h, g)}: new_flag = 0 | ||
|
||
heappush(heap, [now_cost + cost, next_node, new_flag]) | ||
|
||
answer = [] | ||
for dedication in node_dedications: | ||
if flag[dedication] == 0: answer.append(dedication) | ||
|
||
print(*sorted(answer)) |