Problem:


Score : 400 points

Problem Statement

Kenkoooo is planning a trip in Republic of Snuke.
In this country, there are n cities and m trains running.
The cities are numbered 1 through n, and the i-th train connects City u_i and v_i bidirectionally.
Any city can be reached from any city by changing trains.

Two currencies are used in the country: yen and snuuk.
Any train fare can be paid by both yen and snuuk.
The fare of the i-th train is a_i yen if paid in yen, and b_i snuuk if paid in snuuk.

In a city with a money exchange office, you can change 1 yen into 1 snuuk.
However, when you do a money exchange, you have to change all your yen into snuuk.
That is, if Kenkoooo does a money exchange when he has X yen, he will then have X snuuk.
Currently, there is a money exchange office in every city, but the office in City i will shut down in i years and can never be used in and after that year.

Kenkoooo is planning to depart City s with 10^{15} yen in his pocket and head for City t, and change his yen into snuuk in some city while traveling.
It is acceptable to do the exchange in City s or City t.

Kenkoooo would like to have as much snuuk as possible when he reaches City t by making the optimal choices for the route to travel and the city to do the exchange.
For each i=0,...,n-1, find the maximum amount of snuuk that Kenkoooo has when he reaches City t if he goes on a trip from City s to City t after i years.
You can assume that the trip finishes within the year.

Constraints

2 \leq n \leq 10^5

1 \leq m \leq 10^5

1 \leq s,t \leq n

s \neq t

1 \leq u_i < v_i \leq n

1 \leq a_i,b_i \leq 10^9

If i\neq j, then u_i \neq u_j  or v_i \neq v_j.

Any city can be reached from any city by changing trains.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

n m s t
u_1 v_1 a_1 b_1
:
u_m v_m a_m b_m

Output

Print n lines.
In the i-th line, print the maximum amount of snuuk that Kenkoooo has when he reaches City t if he goes on a trip from City s to City t after i-1 years.

Sample Input 1

4 3 2 3
1 4 1 100
1 2 1 10
1 3 20 1

Sample Output 1

999999999999998
999999999999989
999999999999979
999999999999897

After 0 years, it is optimal to do the exchange in City 1.

After 1 years, it is optimal to do the exchange in City 2.

Note that City 1 can still be visited even after the exchange office is closed.
Also note that, if it was allowed to keep 1 yen when do the exchange in City 2 and change the remaining yen into snuuk, we could reach City 3 with 999999999999998 snuuk, but this is NOT allowed.

After 2 years, it is optimal to do the exchange in City 3.

After 3 years, it is optimal to do the exchange in City 4.
Note that the same train can be used multiple times.

Sample Input 2

8 12 3 8
2 8 685087149 857180777
6 7 298270585 209942236
2 4 346080035 234079976
2 5 131857300 22507157
4 8 30723332 173476334
2 6 480845267 448565596
1 4 181424400 548830121
4 5 57429995 195056405
7 8 160277628 479932440
1 6 475692952 203530153
3 5 336869679 160714712
2 7 389775999 199123879

Sample Output 2

999999574976994
999999574976994
999999574976994
999999574976994
999999574976994
999999574976994
999999574976994
999999574976994



Sample input:
4 3 2 3
1 4 1 100
1 2 1 10
1 3 20 1


Sample output:
999999999999998
999999999999989
999999999999979
999999999999897

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict
from heapq import heappush, heappop

def dijkstra(V, E, s):
    '''
    Input:
        V = list(range(N))
        E = defaultdict(dict)
        s (in V) : start vertex
    Output:
        dist: the list of the minimum cost from s to arbitrary vertex v in the given graph
    '''
    INF = float('inf')
    dist = {v: INF for v in V}
    heap = []
    heappush(heap, (0, s))
    while heap:
        d, v = heappop(heap)
        # don't need to check when the stored value is greater than provisional distance
        if d > dist[v]:
            continue
        dist[v] = d
        for u, c in E[v].items():
            temp = d + c
            if temp < dist[u]:
                heappush(heap, (temp, u))
    return dist

MON = 10**15
n, m, s, t = map(int, input().split())
V1, V2 = list(range(1, n+1)), list(range(1, n+1))
E1, E2 = defaultdict(dict), defaultdict(dict)
for _ in range(m):
    u, v, a, b = map(int, input().split())
    E1[u][v] = a
    E1[v][u] = a
    E2[u][v] = b
    E2[v][u] = b
ans = [MON]*n
dist_yen = dijkstra(V1, E1, s)
dist_snook = dijkstra(V2, E2, t)
temp = float('inf')
for i in reversed(range(n)):
    temp = min(temp, dist_yen[i+1] + dist_snook[i+1])
    ans[i] -= temp
print(*ans, sep='\n')

Submission status: Time Limit Exceeded

Solution code:
from itertools import product
from collections import defaultdict
from heapq import heappush, heappop

def dijkstra(V, E, s):
    '''
    Input:
        V = list(range(N))
        E = defaultdict(dict)
        s (in V) : start vertex
    Output:
        dist: the list of the minimum cost from s to arbitrary vertex v in the given graph
    '''
    INF = float('inf')
    dist = {v: INF for v in V}
    heap = []
    heappush(heap, (0, s))
    while heap:
        d, v = heappop(heap)
        # don't need to check when the stored value is greater than provisional distance
        if d > dist[v]:
            continue
        dist[v] = d
        for u, c in E[v].items():
            temp = d + c
            if temp < dist[u]:
                heappush(heap, (temp, u))
    return dist

MON = 10**15
n, m, s, t = map(int, input().split())
V = list(product([0], range(1, n+1))) + list(product([1], range(1, n+1)))
E = defaultdict(dict)
for _ in range(m):
    u, v, a, b = map(int, input().split())
    E[(0, u)][(0, v)] = a
    E[(0, v)][(0, u)] = a
    E[(1, u)][(1, v)] = b
    E[(1, v)][(1, u)] = b
ans = [MON]*n
for i in reversed(range(n)):
    E[(0, i+1)][(1, i+1)] = 0
    dist = dijkstra(V, E, (0, s))
    ans[i] -= dist[(1, t)]
print(*ans, sep='\n')

Submission status: Time Limit Exceeded

Solution code:
n,m,s,t = list(map(int,input().split()))

s,t=s-1,t-1
e_list1 = [[] for i in range(n)]
e_list2 = [[] for i in range(n)]
for i in range(m):
    u,v,a,b = list(map(int,input().split()))
    u,v=u-1,v-1
    e_list1[u].append([v,a])
    e_list1[v].append([u,a])
    e_list2[u].append([v,b])
    e_list2[v].append([u,b])

#dijkstra
#you need to prepare e_list = [[orient,dist]]
N=n
import heapq
vi = s
min_d_list1 = [10**27 for i in range(N)]
min_d_list1[vi] = 0
prev_list = [-1 for i in range(N)]
q = []
for i in range(N):
    heapq.heappush(q,[min_d_list1[i],i])

while len(q)>0:
    d,v = heapq.heappop(q)
    for e in e_list1[v]:
        v1,d1=e
        if min_d_list1[v1]>d+d1:
            min_d_list1[v1]=d+d1
            prev_list[v1]=v
            heapq.heappush(q,[d+d1,v1])

vi = t
min_d_list2 = [10**27 for i in range(N)]
min_d_list2[vi] = 0
prev_list = [-1 for i in range(N)]
q = []
for i in range(N):
    heapq.heappush(q,[min_d_list2[i],i])

while len(q)>0:
    d,v = heapq.heappop(q)
    for e in e_list2[v]:
        v1,d1=e
        if min_d_list2[v1]>d+d1:
            min_d_list2[v1]=d+d1
            prev_list[v1]=v
            heapq.heappush(q,[d+d1,v1])

min_list = []
min_=10**15
for i in range(n):
    min_=min(min_d_list1[n-i-1]+min_d_list2[n-i-1],min_)
    min_list.append(10**15-min_)

for i in range(n):
    print(min_list[n-i-1])

Submission status: Time Limit Exceeded

Solution code:
from heapq import heappop, heappush
INF = float('inf')

n, m, s, t = map(int, input().split())
edges = [list(map(int, input().split())) for _ in range(m)]

graph = [[] for _ in range(n + 1)]
for u, v, a, b in edges:
    graph[u].append((v, a, b))
    graph[v].append((u, a, b))

def dijkstra(s, pay):
    """
    s: start
    pay: 0 = yen, 1 = snuuk
    """
    d = [INF] * (n + 1)
    visited = [False] * (n + 1)
    pq = []

    heappush(pq, (0, s))
    d[s] = 0
    visited[s] = True

    while pq:
        cost, x = heappop(pq)
        visited[x] = True

        for v, *c in graph[x]:
            if visited[v]:
                continue

            if d[v] > d[x] + c[pay]:
                d[v] = d[x] + c[pay]
                heappush(pq, (d[v], v))

    return d

d1 = dijkstra(s, 0)
d2 = dijkstra(t, 1)

cost = [v + w for v, w in zip(d1[1:], d2[1:])]
for i in range(n):
    print(10 ** 15 - min(cost[i:]))

Submission status: Time Limit Exceeded

Solution code:
from heapq import heappop, heappush
INF = float('inf')

n, m, s, t = map(int, input().split())
edges = [list(map(int, input().split())) for _ in range(m)]

def dijkstra(s, pay):
    """
    s: start
    pay: 0 = yen, 1 = snuuk
    """
    d = [INF] * (n + 1)
    visited = [False] * (n + 1)
    pq = []

    heappush(pq, (0, s))
    d[s] = 0
    visited[s] = True

    while pq:
        cost, x = heappop(pq)
        visited[x] = True

        if d[x] < cost:
            continue

        for u, v, *c in edges:
            if u != x and v != x:
                continue

            
            node = u if v == x else v
            if visited[node]:
                continue

            if d[node] > d[x] + c[pay]:
                d[node] = d[x] + c[pay]
                heappush(pq, (d[node], node))

    return d

d1 = dijkstra(s, 0)
d2 = dijkstra(t, 1)
cost = [v + w for v, w in zip(d1[1:], d2[1:])]

for i in range(n):
    print(10 ** 15 - min(cost[i:]))

Submission status: Time Limit Exceeded

Solution code:
import heapq
from collections import defaultdict
WHITE = 0
GRAY = 1
BLACK = 2
INFTY = 10**16
def dijkstra(s, VC):
    color = [WHITE for i in range(N)]
    d = [INFTY for i in range(N)]
    d[s] = 0
    pq = []
    heapq.heappush(pq, (0, s))

    while len(pq) != 0:
        u = heapq.heappop(pq)[1]
        color[u] = BLACK

        for v, c in VC[u]:
            if color[v] != BLACK:
                if d[u] + c < d[v]:
                    d[v] = d[u] + c
                    color[v] = GRAY
                    heapq.heappush(pq, (d[v], v))

    return d

N, M, S, T = map(int, input().split())
# VA = [[] for i in range(N)]
# VB = [[] for i in range(N)]
VA = defaultdict(list)
VB = defaultdict(list)
for i in range(M):
    u, v, a, b = list(map(int, input().split()))
    VA[u-1].append([v-1, a,])
    VA[v-1].append([u-1, a])
    VB[u-1].append([v-1, b])
    VB[v-1].append([u-1, b])

da = dijkstra(S-1, VA)
db = dijkstra(T-1, VB)
tmp = 10**16
base = 10 ** 15
ans = [0 for i in range(N)]
for i in range(N-1, -1, -1):
    tmp = min(da[i] + db[i], tmp)
    ans[i] = tmp

for i in range(N):
    print(base - ans[i])

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict
import heapq
import sys
input = sys.stdin.readline


class Dijkstra:
    def __init__(self):
        self.edge = defaultdict(list)

    def add(self, u, v, d):
        self.edge[u].append([v, d])
        self.edge[v].append([u, d])

    def search(self, start):
        distance = defaultdict(lambda: float("inf"))
        distance[start] = 0
        queue = []
        heapq.heappush(queue, (0, start))
        seen = set()
        while queue:
            k, u = heapq.heappop(queue)
            if u in seen:
                continue
            seen.add(u)

            for v, d in self.edge[u]:
                if v in seen:
                    continue
                if distance[v] > k + d:
                    distance[v] = k + d
                    heapq.heappush(queue, (k + d, v))
        return distance


n, m, s, t = map(int, input().split())
uvab = [list(map(int, input().split())) for _ in range(m)]
graph1 = Dijkstra()
graph2 = Dijkstra()

for u, v, a, b in uvab:
    graph1.add(u, v, a)
    graph2.add(u, v, b)

result_a = graph1.search(s)
result_b = graph2.search(t)
ans_list = [float('inf')]

for i in range(n, 0, -1):
    cur = result_a[i] + result_b[i]
    if cur > ans_list[-1]:
        cur = ans_list[-1]
    ans_list.append(cur)

for ans in ans_list[n:0:-1]:
    print(10 ** 15 - ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict
import heapq


class Dijkstra:
    def __init__(self):
        self.edge = defaultdict(list)

    def add(self, u, v, d):
        self.edge[u].append([v, d])
        self.edge[v].append([u, d])

    def search(self, start):
        distance = defaultdict(lambda: float("inf"))
        distance[start] = 0
        queue = []
        heapq.heappush(queue, (0, start))
        seen = set()
        while queue:
            k, u = heapq.heappop(queue)
            if u in seen:
                continue
            seen.add(u)

            for v, d in self.edge[u]:
                if v in seen:
                    continue
                if distance[v] > k + d:
                    distance[v] = k + d
                    heapq.heappush(queue, (k + d, v))
        return distance


n, m, s, t = map(int, input().split())
uvab = [list(map(int, input().split())) for i in range(m)]
graph1 = Dijkstra()
graph2 = Dijkstra()

for u, v, a, b in uvab:
    graph1.add(u, v, a)
    graph2.add(u, v, b)

result_a = graph1.search(s)
result_b = graph2.search(t)
ans_list = [float('inf')]

for i in range(n, 0, -1):
    cur = result_a[i] + result_b[i]
    if cur > ans_list[-1]:
        cur = ans_list[-1]
    ans_list.append(cur)

for i in range(len(ans_list[1:])):
    print(10 ** 15 - ans_list[-(i + 1)])

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10**7)
n, m, s, t = map(int, input().split())
G1 = [[]for i in range(n+1)]
G2 = [[]for i in range(n+1)]
cost1 = [10**15 ]*(n+1)
cost2 = [10**15 ]*(n+1)
for i in range(m):
    u, v, a, b = map(int,input().split())
    G1[u] += (v,a),
    G1[v] += (u,a),
    G2[u] += (v,b),
    G2[v] += (u,b),
def f1(s, l, c):
    if cost1[s] > c:
        cost1[s] = c
    for v, a in G1[s]:
        if v not in l:
            f1(v, l+[v], c+a)
def f2(t, l, c):
    if cost2[t] > c:
        cost2[t] = c
    for v, b in G2[t]:
        if v not in l:
            f2(v, l+[v], c+b)
f1(s, [s], 0)
f2(t, [t], 0)
ansl = []
ans = 0
for i in range(1, n+1):
    cost = cost1[-i] + cost2[-i]
    m = 10**15 - cost
    if ans < m:
        ans = m
    ansl.append(ans)
for _ in range(n):
    print(ansl.pop())

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10**6)
n, m, s, t = map(int, input().split())
G1 = [[]for i in range(n+1)]
G2 = [[]for i in range(n+1)]
cost1 = [10**15 ]*(n+1)
cost2 = [10**15 ]*(n+1)
for i in range(m):
    u, v, a, b = map(int,input().split())
    G1[u] += (v,a),
    G1[v] += (u,a),
    G2[u] += (v,b),
    G2[v] += (u,b),
def f1(s, l, c):
    if cost1[s] > c:
        cost1[s] = c
    for v, a in G1[s]:
        if v not in l:
            f1(v, l+[v], c+a)
def f2(t, l, c):
    if cost2[t] > c:
        cost2[t] = c
    for v, b in G2[t]:
        if v not in l:
            f2(v, l+[v], c+b)
f1(s, [s], 0)
f2(t, [t], 0)
ansl = []
ans = 0
for i in range(1, n+1):
    cost = cost1[-i] + cost2[-i]
    m = 10**15 - cost
    if ans < m:
        ans = m
    ansl.append(ans)
for _ in range(n):
    print(ansl.pop())

Submission status: Time Limit Exceeded

Solution code:
from heapq import heappush, heappop
def dijkstra(graph:list, node:int, start:int) -> list:
    # graph[node] = [(cost, to)]
    inf = float('inf')
    dist = [inf]*node

    dist[start] = 0
    heap = [(0,start)]
    while heap:
        cost,thisNode = heappop(heap)
        for NextCost,NextNode in graph[thisNode]:
            dist_cand = dist[thisNode]+NextCost
            if dist_cand < dist[NextNode]:
                dist[NextNode] = dist_cand
                heappush(heap,(dist[NextNode],NextNode))
    return dist

n,m,s,t = map(int,input().split())

g_a = [[] for i in range(n)]
g_b = [[] for i in range(n)]

for i in range(m):
    u,v,a,b = map(int,input().split())
    g_a[u-1].append((a,v-1))
    g_a[v-1].append((a,u-1))
    g_b[u-1].append((b,v-1))
    g_b[v-1].append((b,u-1))

d_a = dijkstra(g_a,n,s-1)
d_b = dijkstra(g_b,n,t-1)

d = []
for i in range(n):
    d.append(d_a[i]+d_b[i])

d.reverse()

for i in range(n):
    print(10**15-min(d))
    d.pop()

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict
import heapq


class Dijkstra:
    def __init__(self):
        self.e = defaultdict(list)

    def add(self, u, v, d, directed=False):
        if directed is False:
            self.e[u].append([v, d])
            self.e[v].append([u, d])
        else:
            self.e[u].append([v, d])

    def delete(self, u, v):
        self.e[u] = [_ for _ in self.e[u] if _[0] != v]
        self.e[v] = [_ for _ in self.e[v] if _[0] != u]

    def search(self, s):
        d = defaultdict(lambda: float('inf'))
        d[s] = 0
        q = []
        heapq.heappush(q, (0, s))
        v = defaultdict(bool)
        while len(q):
            k, u = heapq.heappop(q)
            if v[u]:
                continue
            for uv, ud in self.e[u]:
                if v[uv]:
                    continue
                vd = k + ud
                if d[uv] > vd:
                    d[uv] = vd
                    heapq.heappush(q, (vd, uv))
        return d


N, M, S, T = map(int, input().split())
UVAB = [list(map(int, input().split())) for i in range(M)]
graph1 = Dijkstra()
graph2 = Dijkstra()

for u, v, a, b in UVAB:
    graph1.add(u, v, a)
    graph2.add(u, v, b)

result_a = graph1.search(S)
result_b = graph2.search(T)

ans_list = [float('inf')]

for i in range(N, 0, -1):
    cur = result_a[i] + result_b[i]
    if cur > ans_list[-1]:
        cur = ans_list[-1]
    ans_list.append(cur)

for i in range(len(ans_list[1:])):
    print(10 ** 15 - ans_list[-(i + 1)])

Submission status: Time Limit Exceeded

Solution code:
import sys
# import math, string, itertools, fractions, heapq, collections, re, array, bisect, copy, functools, random
from collections import deque, defaultdict, Counter; from heapq import heappush, heappop
# from itertools import permutations, combinations, product, accumulate, groupby
# from bisect import bisect_left, bisect_right, insort_left, insort_right
from operator import itemgetter as ig
sys.setrecursionlimit(10 ** 7)
# inf = 10 ** 20; INF = float("INF"); ans = 0; tmp = 0; ansli = []; tmpli = []; candili = []; mod = 10 ** 9 + 7
# dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]; ddn = dd + [(-1, 1), (1, 1), (1, -1), (-1, -1)]; ddn9 = ddn + [(0, 0)]
"""for dx, dy in dd:
        nx = j + dx; ny = i + dy
            if 0 <= nx < w and 0 <= ny < h:"""
def wi(): return list(map(int, sys.stdin.readline().split()))
def wip(): return [int(x) - 1 for x in sys.stdin.readline().split()]#WideIntPoint
def ws(): return sys.stdin.readline().split()
def i(): return int(sys.stdin.readline())
def s(): return input()
def hi(n): return [i() for _ in range(n)]
def hs(n): return [s() for _ in range(n)]#HeightString
def mi(n): return [wi() for _ in range(n)]#MatrixInt
def mip(n): return [wip() for _ in range(n)]
def ms(n): return [ws() for _ in range(n)]

n, m, s, t = wi()
uvab = mi(m)

dists = []
distt = []
for i in range(m):
    dists.append([uvab[i][0], uvab[i][1], uvab[i][2]])
    distt.append([uvab[i][0], uvab[i][1], uvab[i][3]])

class Graph(object):
    """
    隣接リストによる有向グラフ
    """

    def __init__(self):
        self.graph = defaultdict(list)

    def __len__(self):
        return len(self.graph)

    def add_edge(self, src, dst, weight=1):
        self.graph[src].append((dst, weight))

    def get_nodes(self):
        return self.graph.keys()


class Dijkstra(object):
    """
    ダイクストラ法（二分ヒープ）による最短経路探索
    計算量: O((E+V)logV)
    """

    def __init__(self, graph, start):
        self.g = graph.graph

        # startノードからの最短距離
        # startノードは0, それ以外は無限大で初期化
        self.dist = defaultdict(lambda: float('inf'))
        self.dist[start] = 0

        # 最短経路での1つ前のノード
        self.prev = defaultdict(lambda: None)

        # startノードをキューに入れる
        self.Q = []
        heappush(self.Q, (self.dist[start], start))

        while self.Q:
            # 優先度（距離）が最小であるキューを取り出す
            dist_u, u = heappop(self.Q)
            if self.dist[u] < dist_u:
                continue
            for v, weight in self.g[u]:
                alt = dist_u + weight
                if self.dist[v] > alt:
                    self.dist[v] = alt
                    self.prev[v] = u
                    heappush(self.Q, (alt, v))

    def shortest_distance(self, goal):
        """
        startノードからgoalノードまでの最短距離
        """
        return self.dist[goal]

    def shortest_path(self, goal):
        """
        startノードからgoalノードまでの最短経路
        """
        path = []
        node = goal
        while node is not None:
            path.append(node)
            node = self.prev[node]
        return path[::-1]
# (src, dst, weight)
inputs = dists
inputt = distt

gs = Graph()
gt = Graph()
for src, dst, weight in inputs:
    gs.add_edge(src, dst, weight)
    gs.add_edge(dst, src, weight)
for src, dst, weight in inputt:
    gt.add_edge(src, dst, weight)
    gt.add_edge(dst, src, weight)

spdi = {}
tpdi = {}
ds = Dijkstra(gs, s)
dt = Dijkstra(gt, t)
for i in range(1, n + 1):
    sp = format(ds.shortest_distance(i))
    spdi[i] = int(sp)
    tp = format(dt.shortest_distance(i))
    tpdi[i] = int(tp)
#print('最短経路: {}'.format(d.shortest_path(1)))
#print('最短距離: {}'.format(d.shortest_distance(1)))
route = []
for i in range(1, n + 1):
    route.append([10 ** 15 - (spdi[i] + tpdi[i]), i])
route.sort(key=ig(0), reverse=True)
j = 0
for i in range(n):
    while j < n:
        if i < route[j][1]:
            print(route[j][0])
            break
        else:
            j += 1
    if j == n:
        print(route[j][0])

Submission status: Time Limit Exceeded

Solution code:
import heapq


def dijkstra(adj, start):
    ans = [float("inf")] * len(adj)
    q = [(0, start)]
    while len(q):
        cost, node = heapq.heappop(q)
        if cost > ans[node]:
            continue
        ans[node] = cost
        for nxt in adj[node]:
            heapq.heappush(q, (cost + adj[node][nxt], nxt))
    return ans


def main():
    n, m, s, t = map(int, input().split())
    adj1 = [{} for _ in range(n)]
    adj2 = [{} for _ in range(n)]
    for _ in range(m):
        u, v, a, b = map(int, input().split())
        adj1[u-1][v-1] = a
        adj1[v-1][u-1] = a
        adj2[u-1][v-1] = b
        adj2[v-1][u-1] = b
    d1 = dijkstra(adj1, s-1)
    d2 = dijkstra(adj2, t-1)
    ans = [float("inf")] * n
    ans[n-1] = d1[n-1] + d2[n-1]
    for i in range(n-2, -1, -1):
        ans[i] = min(d1[i] + d2[i], ans[i+1])
    for i in range(n):
        print(10**15 - ans[i])


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
INF=10**20
N,M,S,T=[int(a) for a in input().split(" ")]
UVAB=[[int(a) for a in input().split(" ")] for i in range(M)]

def dijkstra(S, N, paths):
    from heapq import heappush,heappop
    q=[(0,S)]
    check=[False]*N
    shortest=[INF]*N
    shortest[S]=0
    while len(q)>0:
        d0, src=heappop(q)
        if d0>shortest[src]:
            continue
        check[src]=True
        for dst, d1 in paths[src]:
            if check[dst] is False and d0+d1<shortest[dst]:
                shortest[dst]=d0+d1
                heappush(q, (d0+d1, dst))
        if all(check):
            break
    return np.array(shortest)

paths_yn=[[] for _ in range(N)]
paths_sn=[[] for _ in range(N)]
for u,v,a,b in UVAB:
    paths_yn[u-1].append((v-1,a))
    paths_yn[v-1].append((u-1,a))
    paths_sn[u-1].append((v-1,b))
    paths_sn[v-1].append((u-1,b))
shortest_yn = dijkstra(S-1, N, paths_yn)
shortest_sn = dijkstra(T-1, N, paths_sn)

totals = 10**15-(shortest_yn+shortest_sn)
for a in np.maximum.accumulate(totals[::-1])[::-1]:
    print(int(a))

Submission status: Time Limit Exceeded

Solution code:
import heapq
n, m, s, t = map(int, input().split())
INF = 10**20
data = []
for _ in range(m):
    data.append(list(map(int, input().split())))

check = [False] * (n+1)
graph = [[] for i in range(n+1)]

for i in range(m):
    graph[data[i][0]].append([data[i][1], data[i][2], data[i][3]])
    graph[data[i][1]].append([data[i][0], data[i][2], data[i][3]])
city_yen_cost = [INF] * (n+1)
city_yen_cost[s] = 0
queue = []
#[cost, vertex]
heapq.heappush(queue, [0, s])
count = 0
while count != n:
    now = heapq.heappop(queue)
    if check[now[1]]:
        continue
    else:
        count += 1
        check[now[1]] = True
        for path in graph[now[1]]:
            if not check[path[0]]:
                city_yen_cost[path[0]] = min(city_yen_cost[now[1]]+path[1], city_yen_cost[path[0]])
                heapq.heappush(queue, [city_yen_cost[now[1]]+path[1], path[0]])




check = [False] * (n+1)
city_snuke_cost = [INF] * (n+1)
city_snuke_cost[t] = 0
queue = []
heapq.heappush(queue, [0, t])
count = 0
while count != n:
    now = heapq.heappop(queue)
    if check[now[1]]:
        continue
    else:
        count += 1
        check[now[1]] = True
        for path in graph[now[1]]:
            if not check[path[0]]:
                city_snuke_cost[path[0]] = min(city_snuke_cost[now[1]]+path[2], city_snuke_cost[path[0]])
                heapq.heappush(queue, [city_snuke_cost[now[1]]+path[2], path[0]])


for i in range(n+1):
    city_yen_cost[i] += city_snuke_cost[i]
for i in range(n):
    city_yen_cost[-i-2] = min(city_yen_cost[-i-2], city_yen_cost[-i-1])
for i in range(n):
    print(10**15-city_yen_cost[i+1])

Submission status: Time Limit Exceeded

Solution code:
import heapq
n, m, s, t = map(int, input().split())
INF = 10**20
data = []
for _ in range(m):
    data.append(list(map(int, input().split())))

check = [False] * (n+1)
graph = [[] for i in range(n+1)]

for i in range(m):
    graph[data[i][0]].append([data[i][1], data[i][2], data[i][3]])
    graph[data[i][1]].append([data[i][0], data[i][2], data[i][3]])
city_yen_cost = [INF] * (n+1)
city_yen_cost[s] = 0
queue = []
#[cost, vertex]
heapq.heappush(queue, [0, s])
count = 0
while count != n:
    now = heapq.heappop(queue)
    if check[now[1]]:
        continue
    else:
        count += 1
        check[now[1]] = True
        for path in graph[now[1]]:
            city_yen_cost[path[0]] = min(city_yen_cost[now[1]]+path[1], city_yen_cost[path[0]])
            if not check[path[0]]:
                heapq.heappush(queue, [city_yen_cost[now[1]]+path[1], path[0]])




check = [False] * (n+1)
city_snuke_cost = [INF] * (n+1)
city_snuke_cost[t] = 0
queue = []
heapq.heappush(queue, [0, t])
count = 0
while count != n:
    now = heapq.heappop(queue)
    if check[now[1]]:
        continue
    else:
        count += 1
        check[now[1]] = True
        for path in graph[now[1]]:
            city_snuke_cost[path[0]] = min(city_snuke_cost[now[1]]+path[2], city_snuke_cost[path[0]])
            if not check[path[0]]:
                heapq.heappush(queue, [city_snuke_cost[now[1]]+path[2], path[0]])


for i in range(n+1):
    city_yen_cost[i] += city_snuke_cost[i]
for i in range(n):
    city_yen_cost[-i-2] = min(city_yen_cost[-i-2], city_yen_cost[-i-1])
for i in range(n):
    print(10**15-city_yen_cost[i+1])

Submission status: Time Limit Exceeded

Solution code:
import heapq
n, m, s, t = map(int, input().split())
INF = 10**20
data = []
for _ in range(m):
    data.append(list(map(int, input().split())))

check = [False] * (n+1)
graph = [[] for i in range(n+1)]

for i in range(m):
    graph[data[i][0]].append([data[i][1], data[i][2], data[i][3]])
    graph[data[i][1]].append([data[i][0], data[i][2], data[i][3]])
city_yen_cost = [INF] * (n+1)
city_yen_cost[s] = 0
queue = []
#[cost, vertex]
heapq.heappush(queue, [0, s])
count = 0
while count != n:
    now = heapq.heappop(queue)
    if check[now[1]]:
        continue
    else:
        count += 1
        check[now[1]] = True
        for path in graph[now[1]]:
            city_yen_cost[path[0]] = min(city_yen_cost[now[1]]+path[1], city_yen_cost[path[0]])
            heapq.heappush(queue, [city_yen_cost[now[1]]+path[1], path[0]])




check = [False] * (n+1)
city_snuke_cost = [INF] * (n+1)
city_snuke_cost[t] = 0
queue = []
heapq.heappush(queue, [0, t])
count = 0
while count != n:
    now = heapq.heappop(queue)
    if check[now[1]]:
        continue
    else:
        count += 1
        check[now[1]] = True
        for path in graph[now[1]]:
            city_snuke_cost[path[0]] = min(city_snuke_cost[now[1]]+path[2], city_snuke_cost[path[0]])
            heapq.heappush(queue, [city_snuke_cost[now[1]]+path[2], path[0]])


for i in range(n+1):
    city_yen_cost[i] += city_snuke_cost[i]
for i in range(n):
    city_yen_cost[-i-2] = min(city_yen_cost[-i-2], city_yen_cost[-i-1])
for i in range(n):
    print(10**15-city_yen_cost[i+1])

Submission status: Time Limit Exceeded

Solution code:
import heapq
import sys
n, m, s, t = map(int, input().split())
INF = 10**20
data = []
for _ in range(m):
    data.append(list(map(int, sys.stdin.readline().split())))

check = [False] * (n+1)
graph = [[] for i in range(n+1)]

for i in range(m):
    graph[data[i][0]].append([data[i][1], data[i][2], data[i][3]])
    graph[data[i][1]].append([data[i][0], data[i][2], data[i][3]])
city_yen_cost = [INF] * (n+1)
city_yen_cost[s] = 0
queue = []
#[cost, vertex]
heapq.heappush(queue, [0, s])
count = 0
while count != n:
    now = heapq.heappop(queue)
    if check[now[1]]:
        continue
    else:
        count += 1
        check[now[1]] = True
        for path in graph[now[1]]:
            city_yen_cost[path[0]] = min(city_yen_cost[now[1]]+path[1], city_yen_cost[path[0]])
            heapq.heappush(queue, [city_yen_cost[now[1]]+path[1], path[0]])




check = [False] * (n+1)
city_snuke_cost = [INF] * (n+1)
city_snuke_cost[t] = 0
queue = []
heapq.heappush(queue, [0, t])
count = 0
while count != n:
    now = heapq.heappop(queue)
    if check[now[1]]:
        continue
    else:
        count += 1
        check[now[1]] = True
        for path in graph[now[1]]:
            city_snuke_cost[path[0]] = min(city_snuke_cost[now[1]]+path[2], city_snuke_cost[path[0]])
            heapq.heappush(queue, [city_snuke_cost[now[1]]+path[2], path[0]])


for i in range(n+1):
    city_yen_cost[i] += city_snuke_cost[i]
for i in range(n):
    city_yen_cost[-i-2] = min(city_yen_cost[-i-2], city_yen_cost[-i-1])
for i in range(n):
    print(10**15-city_yen_cost[i+1])