Problem p02703 (python)

Fixed submission:
import heapq
import sys
input = sys.stdin.readline
 
INF = 10 ** 18
 
class Edge():
    def __init__(self, end, cost, time):
        self.end = end
        self.cost = cost
        self.time = time
 
dp = []
h = []
 
def push(t, v, x):
    if x < 0:
        return
    if dp[v][x] <= t:
        return
    dp[v][x] = t
    heapq.heappush(h, (t, v, x))
 
def dijkstra(n, G, cd, start, s, max_s):
    global dp
    dp = [[INF for j in range(max_s+1)] for i in range(n)] 
    push(0, start, s)
 
    while h:
        # 使っていない頂点のうち、現時点で最も到達短時間が短いものを選びvとする
        t, v, x = heapq.heappop(h)
        if dp[v][x] < t:
            continue
        # 両替
        if x < max_s:
            push(t+cd[v][1], v, min(x+cd[v][0], max_s))
        # vから到達可能な頂点について、到達時間が短くなれば更新
        for e in G[v]:
            push(t+e.time, e.end, x-e.cost)
 
 
n, m, s = map(int, input().split())
uvab = [list(map(int, input().split())) for _ in range(m)]
cd = [list(map(int, input().split())) for _ in range(n)]
 
es = [[] for i in range(n)]
max_s = 50 * (n-1)
 
for u, v, a, b in uvab:
    es[u-1].append(Edge(v-1,a,b))
    es[v-1].append(Edge(u-1,a,b))
 
dijkstra(n, es, cd, 0, min(max_s,s), max_s)
 
for i in range(1, n):
    print(min(dp[i]))

Buggy submission (verdict: Runtime Error):
import heapq


class Dijkstra(object):

    """ 使用例
    dik = Dijkstra()
    edge = defaultdict(list)
    edge[(1, 1)].append([1, (1, 2)])
    edge[(1, 2)].append([1, (1, 1)])
    edge[(1, 2)].append([2, (2, 1)])
    edge[(2, 1)].append([2, (1, 2)])
    edge[(2, 1)].append([5, (2, 2)])
    edge[(2, 2)].append([5, (2, 1)])
    edge[(2, 2)].append([3, (1, 2)])
    edge[(1, 2)].append([3, (2, 2)])

    s0 = (1, 1)
    ans = dik(s0, edge)

    """

    INF = 10**9

    def __init(self):
        pass

    def __call__(self, s0, edge):

        # s0:始点 (,,,)
        # edge[(,,,)] = [コスト、(,,,)], [コスト、(,,,)]

        d = dict()
        used = dict()

        edgelist = edge[s0]
        heapq.heapify(edgelist)

        d[s0] = 0
        used[s0] = False

        while len(edgelist):
            minedge = heapq.heappop(edgelist)
            if not used.get(minedge[1], True):
                continue
            v = minedge[1]
            d[v] = minedge[0]
            used[v] = False

            for e in edge[v]:
                if used.get(e[1], True):
                    if d.get(e[1], self.INF) <= d[v] + e[0]:
                        continue
                    heapq.heappush(edgelist, [d[v] + e[0], e[1]])

        return d


def solve():
    N, M, S = map(int, input().split())

    s_max = 2500
    S = min(S, s_max)

    dik = Dijkstra()
    edge = dict()
    s0 = (1, S)

    for m in range(1, M + 1):
        U, V, A, B = map(int, input().split())
        for s in range(A, s_max + 1):
            if not (U, s) in edge:
                edge[(U, s)] = []
            if not (V, s) in edge:
                edge[(V, s)] = []
            edge[(U, s)].append([B, (V, s - A)])
            edge[(V, s)].append([B, (U, s - A)])

    for n in range(1, N + 1):
        C, D = map(int, input().split())
        for s in range(s_max + 1):
            s1 = min(s + C, s_max)
            if not (n, s) in edge:
                edge[(n, s)] = []
            edge[(n, s)].append([D, (n, s1)])

    d = dik(s0, edge)

    for n in range(2, N + 1):
        ans = Dijkstra.INF
        for s in range(s_max + 1):
            ans = min(ans, d.get((n, s)), Dijkstra.INF)
        print(ans)


if __name__ == '__main__':
    solve()

Buggy submission (verdict: Runtime Error):
import heapq

INF = 10**7


class Dijkstra(object):

    """ 使用例
    dik = Dijkstra()
    edge = defaultdict(list)
    edge[(1, 1)].append([1, (1, 2)])
    edge[(1, 2)].append([1, (1, 1)])
    edge[(1, 2)].append([2, (2, 1)])
    edge[(2, 1)].append([2, (1, 2)])
    edge[(2, 1)].append([5, (2, 2)])
    edge[(2, 2)].append([5, (2, 1)])
    edge[(2, 2)].append([3, (1, 2)])
    edge[(1, 2)].append([3, (2, 2)])

    s0 = (1, 1)
    ans = dik(s0, edge)

    """

    def __init(self):
        pass

    def __call__(self, s0, edge):

        # s0:始点 (,,,)
        # edge[(,,,)] = [コスト、(,,,)], [コスト、(,,,)]

        d = dict()
        used = dict()

        edgelist = edge[s0]
        heapq.heapify(edgelist)

        d[s0] = 0
        used[s0] = False

        while len(edgelist):
            minedge = heapq.heappop(edgelist)
            if not used.get(minedge[1], True):
                continue
            v = minedge[1]
            d[v] = minedge[0]
            used[v] = False

            for e in edge[v]:
                if used.get(e[1], True):
                    if d.get(e[1], INF) <= d[v] + e[0]:
                        continue
                    heapq.heappush(edgelist, [d[v] + e[0], e[1]])

        return d


N, M, S = map(int, input().split())

s_max = 2500
S = min(S, s_max)

dik = Dijkstra()
edge = dict()
s0 = (1, S)

for m in range(1, M + 1):
    U, V, A, B = map(int, input().split())
    for s in range(A, s_max + 1):
        if not (U, s) in edge:
            edge[(U, s)] = []
        if not (V, s) in edge:
            edge[(V, s)] = []
        edge[(U, s)].append([B, (V, s - A)])
        edge[(V, s)].append([B, (U, s - A)])

for n in range(1, N + 1):
    C, D = map(int, input().split())
    for s in range(s_max + 1):
        s1 = min(s + C, s_max)
        if not (n, s) in edge:
            edge[(n, s)] = []
        edge[(n, s)].append([D, (n, s1)])

d = dik(s0, edge)

for n in range(2, N + 1):
    ans = INF
    for s in range(s_max + 1):
        ans = min(ans, d.get((n, s)), INF)
    print(ans)

Buggy submission (verdict: Runtime Error):
import heapq

INF = 10 ** 9


class Dijkstra(object):

    """ 使用例
    dik = Dijkstra()
    edge = defaultdict(list)
    edge[(1, 1)].append([1, (1, 2)])
    edge[(1, 2)].append([1, (1, 1)])
    edge[(1, 2)].append([2, (2, 1)])
    edge[(2, 1)].append([2, (1, 2)])
    edge[(2, 1)].append([5, (2, 2)])
    edge[(2, 2)].append([5, (2, 1)])
    edge[(2, 2)].append([3, (1, 2)])
    edge[(1, 2)].append([3, (2, 2)])

    s0 = (1, 1)
    ans = dik(s0, edge)

    """

    def __init(self):
        pass

    def __call__(self, s0, edge):

        # s0:始点 (,,,)
        # edge[(,,,)] = [コスト、(,,,)], [コスト、(,,,)]

        d = dict()
        used = dict()

        edgelist = edge[s0]
        heapq.heapify(edgelist)

        d[s0] = 0
        used[s0] = False

        while len(edgelist):
            minedge = heapq.heappop(edgelist)
            if not used.get(minedge[1], True):
                continue
            v = minedge[1]
            d[v] = minedge[0]
            used[v] = False

            for e in edge[v]:
                if used.get(e[1], True):
                    if d.get(e[1], INF) <= d[v] + e[0]:
                        continue
                    heapq.heappush(edgelist, [d[v] + e[0], e[1]])

        return d


N, M, S = map(int, input().split())

s_max = 2500
S = min(S, s_max)

dik = Dijkstra()
edge = dict()
s0 = (1, S)

for m in range(1, M + 1):
    U, V, A, B = map(int, input().split())
    for s in range(A, s_max + 1):
        if not (U, s) in edge:
            edge[(U, s)] = []
        if not (V, s) in edge:
            edge[(V, s)] = []
        edge[(U, s)].append([B, (V, s - A)])
        edge[(V, s)].append([B, (U, s - A)])

for n in range(1, N + 1):
    C, D = map(int, input().split())
    for s in range(s_max + 1):
        s1 = min(s + C, s_max)
        if not (n, s) in edge:
            edge[(n, s)] = []
        edge[(n, s)].append([D, (n, s1)])

d = dik(s0, edge)

for n in range(2, N + 1):
    ans = INF
    for s in range(s_max + 1):
        ans = min(ans, d.get((n, s)), INF)
    print(ans)

Buggy submission (verdict: Runtime Error):
import heapq

INF = 10 ** 9


class Dijkstra(object):

    """ 使用例
    dik = Dijkstra()
    edge = defaultdict(list)
    edge[(1, 1)].append([1, (1, 2)])
    edge[(1, 2)].append([1, (1, 1)])
    edge[(1, 2)].append([2, (2, 1)])
    edge[(2, 1)].append([2, (1, 2)])
    edge[(2, 1)].append([5, (2, 2)])
    edge[(2, 2)].append([5, (2, 1)])
    edge[(2, 2)].append([3, (1, 2)])
    edge[(1, 2)].append([3, (2, 2)])

    s0 = (1, 1)
    ans = dik(s0, edge)

    """

    def __init(self):
        pass

    def __call__(self, s0, edge):

        # s0:始点 (,,,)
        # edge[(,,,)] = [コスト、(,,,)], [コスト、(,,,)]

        d = dict()
        used = dict()

        edgelist = edge[s0]
        heapq.heapify(edgelist)

        d[s0] = 0
        used[s0] = False

        while len(edgelist):
            minedge = heapq.heappop(edgelist)
            if not used.get(minedge[1], True):
                continue
            v = minedge[1]
            d[v] = minedge[0]
            used[v] = False

            for e in edge[v]:
                if used.get(e[1], True):
                    if d.get(e[1], INF) <= d[v] + e[0]:
                        continue
                    heapq.heappush(edgelist, [d[v] + e[0], e[1]])

        return d


def solve():
    N, M, S = map(int, input().split())

    s_max = 2500
    S = min(S, s_max)

    dik = Dijkstra()
    edge = dict()
    s0 = (1, S)

    for m in range(1, M + 1):
        U, V, A, B = map(int, input().split())
        for s in range(A, s_max + 1):
            if not (U, s) in edge:
                edge[(U, s)] = []
            if not (V, s) in edge:
                edge[(V, s)] = []
            edge[(U, s)].append([B, (V, s - A)])
            edge[(V, s)].append([B, (U, s - A)])

    for n in range(1, N + 1):
        C, D = map(int, input().split())
        for s in range(s_max + 1):
            s1 = min(s + C, s_max)
            if not (n, s) in edge:
                edge[(n, s)] = []
            edge[(n, s)].append([D, (n, s1)])

    d = dik(s0, edge)

    for n in range(2, N + 1):
        ans = INF
        for s in range(s_max + 1):
            ans = min(ans, d.get((n, s)), INF)
        print(ans)


if __name__ == '__main__':
    solve()

Buggy submission (verdict: Runtime Error):
import heapq

INF = 10**9


class Dijkstra(object):

    """ 使用例
    dik = Dijkstra()
    edge = defaultdict(list)
    edge[(1, 1)].append([1, (1, 2)])
    edge[(1, 2)].append([1, (1, 1)])
    edge[(1, 2)].append([2, (2, 1)])
    edge[(2, 1)].append([2, (1, 2)])
    edge[(2, 1)].append([5, (2, 2)])
    edge[(2, 2)].append([5, (2, 1)])
    edge[(2, 2)].append([3, (1, 2)])
    edge[(1, 2)].append([3, (2, 2)])

    s0 = (1, 1)
    ans = dik(s0, edge)

    """

    def __init(self):
        pass

    def __call__(self, s0, edge):

        # s0:始点 (,,,)
        # edge[(,,,)] = [コスト、(,,,)], [コスト、(,,,)]

        d = dict()
        used = dict()

        edgelist = edge[s0]
        heapq.heapify(edgelist)

        d[s0] = 0
        used[s0] = False

        while len(edgelist):
            minedge = heapq.heappop(edgelist)
            if not used.get(minedge[1], True):
                continue
            v = minedge[1]
            d[v] = minedge[0]
            used[v] = False

            for e in edge[v]:
                if used.get(e[1], True):
                    if d.get(e[1], INF) <= d[v] + e[0]:
                        continue
                    heapq.heappush(edgelist, [d[v] + e[0], e[1]])

        return d


N, M, S = map(int, input().split())

s_max = 2500
S = min(S, s_max)

dik = Dijkstra()
edge = dict()
s0 = (1, S)

for m in range(1, M + 1):
    U, V, A, B = map(int, input().split())
    for s in range(A, s_max + 1):
        if not (U, s) in edge:
            edge[(U, s)] = []
        if not (V, s) in edge:
            edge[(V, s)] = []
        edge[(U, s)].append([B, (V, s - A)])
        edge[(V, s)].append([B, (U, s - A)])

for n in range(1, N + 1):
    C, D = map(int, input().split())
    for s in range(s_max + 1):
        s1 = min(s + C, s_max)
        if not (n, s) in edge:
            edge[(n, s)] = []
        edge[(n, s)].append([D, (n, s1)])

d = dik(s0, edge)

for n in range(2, N + 1):
    ans = INF
    for s in range(s_max + 1):
        ans = min(ans, d.get((n, s)), INF)
    print(ans)

Buggy submission (verdict: Runtime Error):
import heapq

INF = 10**9


class Dijkstra(object):

    """ 使用例
    dik = Dijkstra()
    edge = defaultdict(list)
    edge[(1, 1)].append([1, (1, 2)])
    edge[(1, 2)].append([1, (1, 1)])
    edge[(1, 2)].append([2, (2, 1)])
    edge[(2, 1)].append([2, (1, 2)])
    edge[(2, 1)].append([5, (2, 2)])
    edge[(2, 2)].append([5, (2, 1)])
    edge[(2, 2)].append([3, (1, 2)])
    edge[(1, 2)].append([3, (2, 2)])

    s0 = (1, 1)
    ans = dik(s0, edge)

    """

    def __init(self):
        pass

    def __call__(self, s0, edge):

        # s0:始点 (,,,)
        # edge[(,,,)] = [コスト、(,,,)], [コスト、(,,,)]

        d = dict()
        used = dict()

        edgelist = edge[s0]
        heapq.heapify(edgelist)

        d[s0] = 0
        used[s0] = False

        while len(edgelist):
            minedge = heapq.heappop(edgelist)
            if not used.get(minedge[1], True):
                continue
            v = minedge[1]
            d[v] = minedge[0]
            used[v] = False

            for e in edge[v]:
                if used.get(e[1], True):
                    if d.get(e[1], INF) <= d[v] + e[0]:
                        continue
                    heapq.heappush(edgelist, [d[v] + e[0], e[1]])

        return d


def main():
    N, M, S = map(int, input().split())

    s_max = 2500
    S = min(S, s_max)

    dik = Dijkstra()
    edge = dict()
    s0 = (1, S)

    for m in range(1, M + 1):
        U, V, A, B = map(int, input().split())
        for s in range(A, s_max + 1):
            if not (U, s) in edge:
                edge[(U, s)] = []
            if not (V, s) in edge:
                edge[(V, s)] = []
            edge[(U, s)].append([B, (V, s - A)])
            edge[(V, s)].append([B, (U, s - A)])

    for n in range(1, N + 1):
        C, D = map(int, input().split())
        for s in range(s_max + 1):
            s1 = min(s + C, s_max)
            if not (n, s) in edge:
                edge[(n, s)] = []
            edge[(n, s)].append([D, (n, s1)])

    d = dik(s0, edge)

    for n in range(2, N + 1):
        ans = INF
        for s in range(s_max + 1):
            ans = min(ans, d.get((n, s)), INF)
        print(ans)


if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
import heapq

INF = 10**9


class Dijkstra(object):

    """ 使用例
    dik = Dijkstra()
    edge = defaultdict(list)
    edge[(1, 1)].append([1, (1, 2)])
    edge[(1, 2)].append([1, (1, 1)])
    edge[(1, 2)].append([2, (2, 1)])
    edge[(2, 1)].append([2, (1, 2)])
    edge[(2, 1)].append([5, (2, 2)])
    edge[(2, 2)].append([5, (2, 1)])
    edge[(2, 2)].append([3, (1, 2)])
    edge[(1, 2)].append([3, (2, 2)])

    s0 = (1, 1)
    ans = dik(s0, edge)

    """

    def __init(self):
        pass

    def __call__(self, s0, edge):

        # s0:始点 (,,,)
        # edge[(,,,)] = [コスト、(,,,)], [コスト、(,,,)]

        d = dict()
        used = dict()

        edgelist = edge[s0]
        heapq.heapify(edgelist)

        d[s0] = 0
        used[s0] = False

        while len(edgelist):
            minedge = heapq.heappop(edgelist)
            if not used.get(minedge[1], True):
                continue
            v = minedge[1]
            d[v] = minedge[0]
            used[v] = False

            for e in edge[v]:
                if used.get(e[1], True):
                    if d.get(e[1], INF) <= d[v] + e[0]:
                        continue
                    heapq.heappush(edgelist, [d[v] + e[0], e[1]])

        return d


def main():
    N, M, S = map(int, input().split())

    s_max = 2500
    S = min(S, s_max)

    dik = Dijkstra()
    edge = dict()
    s0 = (1, S)

    for m in range(1, M + 1):
        U, V, A, B = map(int, input().split())
        for s in range(A, s_max + 1):
            if not (U, s) in edge:
                edge[(U, s)] = []
            if not (V, s) in edge:
                edge[(V, s)] = []
            edge[(U, s)].append([B, (V, s - A)])
            edge[(V, s)].append([B, (U, s - A)])

    for n in range(1, N + 1):
        C, D = map(int, input().split())
        for s in range(s_max + 1):
            s1 = min(s + C, s_max)
            if not (n, s) in edge:
                edge[(n, s)] = []
            edge[(n, s)].append([D, (n, s1)])

    d = dik(s0, edge)

    for n in range(2, N + 1):
        ans = INF
        for s in range(s_max + 1):
            ans = min(ans, d.get((n, s)), INF)
        print(ans)


if __name__ == '__main__':
    main()