목차

자취방 정하기

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()