목차

오민식의 고민

ps
링크acmicpc.net/…
출처BOJ
문제 번호1219
문제명오민식의 고민
레벨골드 2
분류

SPFA

시간복잡도O(VE)
인풋사이즈V<=100, E<=100
사용한 언어Python
제출기록35088KB / 172ms
최고기록64ms
해결날짜2021/09/23

풀이

코드

"""Solution code for "BOJ 1219. 오민식의 고민".

- Problem link: https://www.acmicpc.net/problem/1219
- Solution link: http://www.teferi.net/ps/problems/boj/1219

Tags: [SPFA]
"""

import sys
from teflib import tgraph

INF = float('inf')


def main():
    N, start, dest, M = [int(x) for x in sys.stdin.readline().split()]
    wgraph = [{} for _ in range(N)]
    for _ in range(M):
        u, v, w = [int(x) for x in sys.stdin.readline().split()]
        try:
            cost = min(wgraph[u][v], w)
        except KeyError:
            cost = w
        wgraph[u][v] = cost
    gain = [int(x) for x in sys.stdin.readline().split()]
    for cost_to in wgraph:
        for v in cost_to:
            cost_to[v] -= gain[v]

    cost = tgraph.spfa(wgraph, start)[dest]
    if cost == INF:
        print('gg')
    elif cost == -INF:
        print('Gee')
    else:
        print(gain[start] - cost)


if __name__ == '__main__':
    main()