ps | |
---|---|
링크 | acmicpc.net/… |
출처 | BOJ |
문제 번호 | 25619 |
문제명 | 자취방 정하기 |
레벨 | 골드 2 |
분류 |
다익스트라 |
시간복잡도 | O(ElogV) |
인풋사이즈 | V<=200,000, E<=200,000 |
사용한 언어 | Python |
제출기록 | 132648KB / 1268ms |
최고기록 | 1268ms |
해결날짜 | 2022/11/26 |
"""Solution code for "BOJ 25619. 자취방 정하기".
- Problem link: https://www.acmicpc.net/problem/25619
- Solution link: http://www.teferi.net/ps/problems/boj/25619
Tags: [Dijkstra]
"""
import sys
from teflib import graph as tgraph
from teflib import search
SOURCE = 0
def main():
N, M = [int(x) for x in sys.stdin.readline().split()]
wgraph = [{} for _ in range(N)]
neg_nodes = set()
for _ in range(M):
u, v, a, b = [int(x) for x in sys.stdin.readline().split()]
wgraph[u - 1][v - 1] = wgraph[v - 1][u - 1] = a + b
if a + b < 0:
neg_nodes.update((u - 1, v - 1))
T = int(sys.stdin.readline())
connected_nodes = set(search.bfs_iter(lambda u: wgraph[u].keys(), 0))
if connected_nodes & neg_nodes:
answer = connected_nodes
else:
target_dist = T * 2
dists = tgraph.dijkstra(wgraph, SOURCE)
answer = {u for u, dist_u in enumerate(dists) if dist_u <= target_dist}
answer.discard(SOURCE)
print(len(answer))
print(*(u + 1 for u in sorted(answer)))
if __name__ == '__main__':
main()