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:
import heapq, sys
def input():
    return sys.stdin.readline()[:-1]

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

yen_edge = [[] for _ in range(n)]
snuke_edge = [[] for _ in range(n)]

for _ in range(m):
	u, v, a, b = map(int, input().split())
	yen_edge[u - 1].append([v - 1, a])
	yen_edge[v - 1].append([u - 1, a])
	snuke_edge[u - 1].append([v - 1, b])
	snuke_edge[v - 1].append([u - 1, b])

class DijkstraList():
    #隣接リスト版
    #同一頂点の複数回探索を防ぐため訪問した頂点数を変数cntで持つ
    def __init__(self, adj, start):
        self.list = adj
        self.start = start
        self.size = len(adj)

    def solve(self):
        self.dist = [10**30 for _ in range(self.size)]
        self.dist[self.start] = 0
        #self.prev = [-1 for _ in range(self.size)]
        self.q = []
        self.cnt = 0

        heapq.heappush(self.q, (0, self.start))

        while self.q and self.cnt < self.size:
            u_dist, u = heapq.heappop(self.q)
            if self.dist[u] < u_dist:
                continue
            for v, w in self.list[u]:
                if self.dist[v] > u_dist + w:
                    self.dist[v] = u_dist + w
                    #self.prev[v] = u
                    heapq.heappush(self.q, (self.dist[v], v))

            self.cnt += 1

        return

    def distance(self, goal):
        return self.dist[goal]

yen = DijkstraList(yen_edge, s - 1)
snuke = DijkstraList(snuke_edge, t - 1)
yen.solve()
snuke.solve()
ans_list = []

for i in range(n):
	ans_list.append(yen.distance(i) + snuke.distance(i))

ans = []

ans.append(10 ** 15 - ans_list[n - 1])

for i in range(1, n):
	if ans_list[n - i - 1] > ans_list[n - i]:
		ans_list[n - i - 1] = ans_list[n - i]
	ans.append(10 ** 15 - ans_list[n - i - 1])

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

Submission status: Time Limit Exceeded

Solution code:
from heapq import heappush, heappop, heapify
from collections import defaultdict
N,M,S,T=[int(a) for a in input().split(" ")]
S,T=S-1,T-1
UVAB = [[int(a) for a in input().split(" ")] for i in range(M)]
dic = defaultdict(list)
INF = 10**20

for u, v, a, b in UVAB:
    dic[u-1].append((v-1,a))
    dic[v-1].append((u-1,a))
    dic[u+N-1].append((v+N-1,b))
    dic[v+N-1].append((u+N-1,b))

ans = []
for i in list(range(N))[::-1]:
    dic[i].append((i+N,0))
    dic_dist = defaultdict(lambda: INF)
    dic_dist[S] = 0
    q = [(0, S)]
    while(q):
        d0, node = heappop(q)
        if dic_dist[node] < d0:
            continue
        for dst, d1 in dic[node]:
            if dic_dist[dst] > d0+d1:
                dic_dist[dst] = d0+d1
                heappush(q, (d0+d1, dst))
    ans.append(10**15-dic_dist[T+N])

for line in ans[::-1]:
    print(line)

Submission status: Time Limit Exceeded

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

check = [False for i in range(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 for i in range(n+1)]
city_yen_cost[s] = 0
queue = []
#[cost, vertex]
heapq.heappush(queue, [0, s])
while len(queue) > 0:
    now = heapq.heappop(queue)
    if check[now[1]]:
        continue
    else:
        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 for i in range(n+1)]
city_snuke_cost = [INF for i in range(n+1)]
city_snuke_cost[t] = 0
queue = []
heapq.heappush(queue, [0, t])
while len(queue) > 0:
    now = heapq.heappop(queue)
    if check[now[1]]:
        continue
    else:
        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]])


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

Submission status: Time Limit Exceeded

Solution code:
import sys

import numpy as np
from scipy.sparse import coo_matrix
from scipy.sparse.csgraph import dijkstra

sys.setrecursionlimit(10000)
INF = float('inf')

N, M, S, T = map(int, next(sys.stdin).split())
U = []
V = []
A = []
B = []
for _ in range(M):
    u, v, a, b = map(int, next(sys.stdin).split())
    U.append(u)
    V.append(v)
    A.append(a)
    B.append(b)
graph_yen = coo_matrix((A, (U, V)), shape=(N + 1, N + 1))
graph_snuke = coo_matrix((B, (U, V)), shape=(N + 1, N + 1))

# yen_mins[i]: S から i まで行くときの最小金額
yen_mins = dijkstra(graph_yen.tocsr(), indices=S, directed=False, unweighted=False)
# snuke_mins[i]: T から i まで行くときの最小金額
snuke_mins = dijkstra(graph_snuke.tocsr(), indices=T, directed=False, unweighted=False)

# totals[i]: i で両替して S から T まで行くときの最大金額
totals = 10 ** 15 - (yen_mins + snuke_mins)[1:]
# 後ろから累積最大値とる
for a in np.maximum.accumulate(totals[::-1])[::-1]:
    print(int(a))

Submission status: Time Limit Exceeded

Solution code:
import sys

import numpy as np
from scipy.sparse import lil_matrix
from scipy.sparse.csgraph import dijkstra

sys.setrecursionlimit(10000)
INF = float('inf')

N, M, S, T = list(map(int, input().split()))
graph_yen = lil_matrix((N + 1, N + 1))
graph_snuke = lil_matrix((N + 1, N + 1))
for _ in range(M):
    u, v, a, b = list(map(int, input().split()))
    graph_yen[u, v] = a
    graph_snuke[u, v] = b

# yen_mins[i]: S から i まで行くときの最小金額
yen_mins = dijkstra(graph_yen.tocsr(), indices=S, directed=False, unweighted=False)
# snuke_mins[i]: T から i まで行くときの最小金額
snuke_mins = dijkstra(graph_snuke.tocsr(), indices=T, directed=False, unweighted=False)

# total_mins[i]: i で両替して S から T まで行くときの最小金額
total_mins = 10 ** 15 - (yen_mins + snuke_mins)[1:]

# 後ろから累積最大値とる
for a in np.maximum.accumulate(total_mins[::-1])[::-1]:
    print(int(a))

Submission status: Time Limit Exceeded

Solution code:
import sys

import numpy as np
from scipy.sparse import coo_matrix
from scipy.sparse.csgraph import dijkstra

sys.setrecursionlimit(10000)
INF = float('inf')

N, M, S, T = list(map(int, input().split()))
U = []
V = []
A = []
B = []
for _ in range(M):
    u, v, a, b = list(map(int, input().split()))
    U.append(u)
    V.append(v)
    A.append(a)
    B.append(b)

graph_yen = coo_matrix((A, (U, V)), shape=(N + 1, N + 1))
graph_snuke = coo_matrix((B, (U, V)), shape=(N + 1, N + 1))

# yen_mins[i]: S から i まで行くときの最小金額
yen_mins = dijkstra(graph_yen.tocsr(), indices=S, directed=False, unweighted=False)
# snuke_mins[i]: T から i まで行くときの最小金額
snuke_mins = dijkstra(graph_snuke.tocsr(), indices=T, directed=False, unweighted=False)

# total_mins[i]: i で両替して S から T まで行くときの最小金額
total_mins = 10 ** 15 - (yen_mins + snuke_mins)[1:]

# 後ろから累積最大値とる
for a in np.maximum.accumulate(total_mins[::-1])[::-1]:
    print(int(a))

Submission status: Time Limit Exceeded

Solution code:
import sys

import numpy as np
from scipy.sparse import coo_matrix
from scipy.sparse.csgraph import dijkstra

sys.setrecursionlimit(10000)
INF = float('inf')

N, M, S, T = list(map(int, input().split()))
U = []
V = []
A = []
B = []
for _ in range(M):
    u, v, a, b = list(map(int, input().split()))
    U.append(u)
    V.append(v)
    A.append(a)
    B.append(b)

graph_yen = coo_matrix((A, (U, V)), shape=(N + 1, N + 1))
graph_snuke = coo_matrix((B, (U, V)), shape=(N + 1, N + 1))

# yen_mins[i]: S から i まで行くときの最小金額
yen_mins = dijkstra(graph_yen, indices=S, directed=False, unweighted=False)
# snuke_mins[i]: T から i まで行くときの最小金額
snuke_mins = dijkstra(graph_snuke, indices=T, directed=False, unweighted=False)

# total_mins[i]: i で両替して S から T まで行くときの最小金額
total_mins = 10 ** 15 - (yen_mins + snuke_mins)[1:]

# 後ろから累積最大値とる
for a in np.maximum.accumulate(total_mins[::-1])[::-1]:
    print(int(a))

Submission status: Time Limit Exceeded

Solution code:
#import sys
#import numpy as np
from scipy.sparse import csr_matrix, coo_matrix
from scipy.sparse.csgraph import dijkstra, johnson

#sys.setrecursionlimit(10000)
money = 10 ** 15

N, M, S, T = [int(i) for i in input().split()]

rows = []
cols = []
weights_yen = []
weights_snuke = []

for _ in range(M):
    # u v 間のコスト
    u, v, a, b = [int(i) for i in input().split()]
    rows.append(u-1)
    cols.append(v-1)
    weights_yen.append(a)
    weights_snuke.append(b)

graph_yen = coo_matrix((weights_yen, (rows, cols)), shape=(N, N)).tocsr()
graph_snuke = coo_matrix((weights_snuke, (rows, cols)), shape=(N, N)).tocsr()

yen_d = johnson(graph_yen, indices=S-1, directed=False, unweighted=False)
snuke_d = johnson(graph_snuke, indices=T-1, directed=False, unweighted=False)

cost = [int(money - (i + j)) for i, j in zip(yen_d, snuke_d)]
ans = [money] * N
ans[N-1] = cost[N-1]

for i in range(N - 2, -1, -1):
    ans[i] = max(ans[i+1], cost[i])

for a in ans:
    print(a)

Submission status: Time Limit Exceeded

Solution code:
#import sys
#import numpy as np
from scipy.sparse import csr_matrix, coo_matrix
from scipy.sparse.csgraph import dijkstra

#sys.setrecursionlimit(10000)
money = 10 ** 15

N, M, S, T = [int(i) for i in input().split()]

rows = []
cols = []
weights_yen = []
weights_snuke = []

for _ in range(M):
    # u v 間のコスト
    u, v, a, b = [int(i) for i in input().split()]
    rows.append(u-1)
    cols.append(v-1)
    weights_yen.append(a)
    weights_snuke.append(b)

graph_yen = coo_matrix((weights_yen, (rows, cols)), shape=(N, N)).tocsr()
graph_snuke = coo_matrix((weights_snuke, (rows, cols)), shape=(N, N)).tocsr()

yen_d = dijkstra(graph_yen, indices=S-1, directed=False, unweighted=False)
snuke_d = dijkstra(graph_snuke, indices=T-1, directed=False, unweighted=False)

#cost = [int(money - (yen_d[i] + snuke_d[i])) for i in range(N)]
cost = [int(money - (i + j)) for i, j in zip(yen_d, snuke_d)]
ans = [money] * N
ans[N-1] = cost[N-1]

for i in range(N - 2, -1, -1):
    ans[i] = max(ans[i+1], cost[i])

for a in ans:
    print(a)

Submission status: Time Limit Exceeded

Solution code:
#import sys
#import numpy as np
from scipy.sparse import csr_matrix, coo_matrix
from scipy.sparse.csgraph import dijkstra

#sys.setrecursionlimit(10000)
money = 10 ** 15

N, M, S, T = [int(i) for i in input().split()]

rows = []
cols = []
weights_yen = []
weights_snuke = []

for _ in range(M):
    # u v 間のコスト
    u, v, a, b = [int(i) for i in input().split()]
    rows.append(u-1)
    cols.append(v-1)
    weights_yen.append(a)
    weights_snuke.append(b)

graph_yen = coo_matrix((weights_yen, (rows, cols)), shape=(N, N)).tocsr()
graph_snuke = coo_matrix((weights_snuke, (rows, cols)), shape=(N, N)).tocsr()

yen_d = dijkstra(graph_yen, indices=S-1, directed=False, unweighted=False)
snuke_d = dijkstra(graph_snuke, indices=T-1, directed=False, unweighted=False)

cost = [int(money - (yen_d[i] + snuke_d[i])) for i in range(N)]
ans = [money] * N
ans[N-1] = cost[N-1]

for i in range(N - 2, -1, -1):
    ans[i] = max(ans[i+1], cost[i])

for a in ans:
    print(a)

#cost.pop()
#for i in range(N):

Submission status: Time Limit Exceeded

Solution code:
import sys
import numpy as np
from scipy.sparse import csr_matrix, coo_matrix
from scipy.sparse.csgraph import dijkstra

sys.setrecursionlimit(10000)
money = 10 ** 15

N, M, S, T = [int(i) for i in input().split()]

rows = []
cols = []
weights_yen = []
weights_snuke = []

for _ in range(M):
    # u v 間のコスト
    u, v, a, b = [int(i) for i in input().split()]
    rows.append(u-1)
    cols.append(v-1)
    weights_yen.append(a)
    weights_snuke.append(b)

graph_yen = coo_matrix((weights_yen, (rows, cols)), shape=(N, N)).tocsr()
graph_snuke = coo_matrix((weights_snuke, (rows, cols)), shape=(N, N)).tocsr()

yen_d = dijkstra(graph_yen, indices=S-1, directed=False, unweighted=False)
snuke_d = dijkstra(graph_snuke, indices=T-1, directed=False, unweighted=False)

cost = [int(money - (yen_d[i] + snuke_d[i])+ 0.1) for i in range(N)]
ans = [money] * N
ans[N-1] = cost[N-1]

for i in range(N - 2, -1, -1):
    ans[i] = max(ans[i+1], cost[i])

for a in ans:
    print(a)

Submission status: Time Limit Exceeded

Solution code:
import sys
import numpy as np
from scipy.sparse import csr_matrix, coo_matrix
from scipy.sparse.csgraph import dijkstra

sys.setrecursionlimit(10000)
money = 10 ** 15

N, M, S, T = [int(i) for i in input().split()]

rows = []
cols = []
weights_yen = []
weights_snuke = []

for _ in range(M):
    # u v 間のコスト
    u, v, a, b = [int(i) for i in input().split()]
    rows.append(u-1)
    cols.append(v-1)
    weights_yen.append(a)
    weights_snuke.append(b)

graph_yen = coo_matrix((weights_yen, (rows, cols)), shape=(N, N)).tocsr()
graph_snuke = coo_matrix((weights_snuke, (rows, cols)), shape=(N, N)).tocsr()

yen_d = dijkstra(graph_yen, indices=S-1, directed=False, unweighted=False)
snuke_d = dijkstra(graph_snuke, indices=T-1, directed=False, unweighted=False)

cost = [0] * N
for i in range(N):
    cost[i] = int(money - (yen_d[i] + snuke_d[i])+ 0.1)

ans = [money] * N
ans[N-1] = cost[N-1]

for i in range(N - 2, -1, -1):
    ans[i] = max(ans[i+1], cost[i])

for a in ans:
    print(a)

Submission status: Time Limit Exceeded

Solution code:
import sys
import numpy as np
from scipy.sparse import csr_matrix, coo_matrix
from scipy.sparse.csgraph import dijkstra

sys.setrecursionlimit(10000)
money = 10 ** 15

N, M, S, T = [int(i) for i in input().split()]

rows = []
cols = []
weights_yen = []
weights_snuke = []

for _ in range(M):
    # u v 間のコスト
    u, v, a, b = [int(i) for i in input().split()]
    rows.append(u-1)
    cols.append(v-1)
    weights_yen.append(a)
    weights_snuke.append(b)

graph_yen = coo_matrix((weights_yen, (rows, cols)), shape=(N, N))
graph_snuke = coo_matrix((weights_snuke, (rows, cols)), shape=(N, N))

yen_d = dijkstra(graph_yen, indices=S-1, directed=False, unweighted=False)
snuke_d = dijkstra(graph_snuke, indices=T-1, directed=False, unweighted=False)

cost = [0] * N
for i in range(N):
    cost[i] = int(money - (yen_d[i] + snuke_d[i])+ 0.1)

ans = [money] * N
ans[N-1] = cost[N-1]

for i in range(N - 2, -1, -1):
    ans[i] = max(ans[i+1], cost[i])

for a in ans:
    print(a)

Submission status: Time Limit Exceeded

Solution code:
import heapq
from itertools import accumulate
inf = float('inf')

def dijkstra(ad_lst, n, s):
    costs = [inf for i in range(n+1)]
    costs[s], Q = 0, []
    heapq.heappush(Q, [costs[s], s])

    while(Q != []):
        c, u = heapq.heappop(Q)
        for v, c in ad_lst[u]:
            if costs[u] + c < costs[v]:
                heapq.heappush(Q, [costs[u] + c, v])
                costs[v] = costs[u] + c

    return costs

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

route_y = {i: [] for i in range(n+1)}
route_s = {i: [] for i in range(n+1)}
for i in range(m):
    u, v, a, b = map(int, input().split())
    route_y[u].append([v,a])
    route_y[v].append([u,a])
    route_s[u].append([v,b])
    route_s[v].append([u,b])

cost_y = dijkstra(route_y, n, s)
cost_s = dijkstra(route_s, n, t)
result = list(accumulate([y+s for y,s in zip(cost_y, cost_s)][::-1], min))
yen = 10**15
[print(yen-result[i]) for i in range(n-1, -1, -1)]

Submission status: Time Limit Exceeded

Solution code:
import heapq
from itertools import accumulate
inf = float('inf')

def dijkstra(ad_lst, n, s):
    costs = [inf for i in range(n+1)]
    costs[s] = 0
    Q = []
    heapq.heappush(Q, [costs[s], s])

    while(Q != []):
        c, u = heapq.heappop(Q)
        for v, c in ad_lst[u]:
            if costs[u] + c < costs[v]:
                alt = costs[u] + c
                heapq.heappush(Q, [alt, v])
                costs[v] = alt

    return costs

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

route_y = {i: [] for i in range(n+1)}
route_s = {i: [] for i in range(n+1)}
for i in range(m):
    u, v, a, b = map(int, input().split())
    route_y[u].append([v,a])
    route_y[v].append([u,a])
    route_s[u].append([v,b])
    route_s[v].append([u,b])

cost_y = dijkstra(route_y, n, s)
cost_s = dijkstra(route_s, n, t)
result = list(accumulate([y+s for y,s in zip(cost_y, cost_s)][::-1], min))
yen = 10**15
[print(yen-result[i]) for i in range(n-1, -1, -1)]

Submission status: Time Limit Exceeded

Solution code:
import heapq
from itertools import accumulate

def dijkstra(ad_lst, n, s, t):
    inf = float('inf')
    costs = [inf for i in range(n+1)]
    costs[s] = 0
    Q = []
    heapq.heappush(Q, [costs[s], s])

    while(Q != []):
        c, u = heapq.heappop(Q)
        for v, c in ad_lst[u]:
            if costs[u] + c < costs[v]:
                alt = costs[u] + c
                heapq.heappush(Q, [alt, v])
                costs[v] = alt

    return costs

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

route_y = {i: [] for i in range(n+1)}
route_s = {i: [] for i in range(n+1)}
for i in range(m):
    u, v, a, b = map(int, input().split())
    route_y[u].append([v,a])
    route_y[v].append([u,a])
    route_s[u].append([v,b])
    route_s[v].append([u,b])

cost_y = dijkstra(route_y, n, s, t)
cost_s = dijkstra(route_s, n, t, s)
result=list(accumulate([y+s for y,s in zip(cost_y, cost_s)][::-1],min))
yen = 10**15
[print(yen-result[i]) for i in range(n-1, -1, -1)]

Submission status: Time Limit Exceeded

Solution code:
import heapq

def dijkstra(ad_lst, n, s, t):
    inf = float('inf')
    costs = [inf for i in range(n+1)]
    costs[s] = 0
    Q = []
    heapq.heappush(Q, [costs[s], s])

    while(Q != []):
        c, u = heapq.heappop(Q)
        for v, c in ad_lst[u]:
            if costs[u] + c < costs[v]:
                alt = costs[u] + c
                heapq.heappush(Q, [alt, v])
                costs[v] = alt

    return costs

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

route_y = {i: [] for i in range(n+1)}
route_s = {i: [] for i in range(n+1)}
for i in range(m):
    u, v, a, b = map(int, input().split())
    route_y[u].append([v,a])
    route_y[v].append([u,a])
    route_s[u].append([v,b])
    route_s[v].append([u,b])

cost_y = dijkstra(route_y, n, s, t)
cost_s = dijkstra(route_s, n, t, s)
cost_ys = [y+s for y,s in zip(cost_y, cost_s)]

inf = float('inf')
result = [inf for _ in range(n+1)]
min_cost = result[-1]
for i in range(n, 0, -1):
    if min_cost > cost_ys[i]:
        min_cost = cost_ys[i]
    result[i] = min_cost
yen = 10**15
[print(yen-result[i]) for i in range(1, n+1)]

Submission status: Time Limit Exceeded

Solution code:
import heapq

def dijkstra(ad_lst, n, s, t):
    inf = float('inf')
    costs = [inf for i in range(n+1)]
    costs[s] = 0
    Q = []
    heapq.heappush(Q, [costs[s], s])

    while(Q != []):
        c, u = heapq.heappop(Q)
        for v, c in ad_lst[u]:
            if costs[u] + c < costs[v]:
                alt = costs[u] + c
                heapq.heappush(Q, [alt, v])
                costs[v] = alt

    return costs

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

route_y = {i: [] for i in range(n+1)}
route_s = {i: [] for i in range(n+1)}
for i in range(m):
    u, v, a, b = map(int, input().split())
    route_y[u].append([v,a])
    route_y[v].append([u,a])
    route_s[u].append([v,b])
    route_s[v].append([u,b])

cost_y = dijkstra(route_y, n, s, t)
cost_s = dijkstra(route_s, n, t, s)
cost_ys = [y+s for y,s in zip(cost_y, cost_s)]

inf = float('inf')
result = [inf for _ in range(n+1)]
min_cost = result[-1]
for i in range(n, 0, -1):
    if min_cost > cost_ys[i]:
        min_cost = cost_ys[i]
    result[i] = min_cost
yen = 10**15
for i in range(1, n+1):
    print(yen - result[i])

Submission status: Time Limit Exceeded

Solution code:
import heapq

def dijkstra(ad_lst, n, s, t):
    costs = [float('inf') for i in range(n+1)]
    costs[s] = 0
    Q = []
    heapq.heappush(Q, [costs[s], s])

    while(Q != []):
        c, u = heapq.heappop(Q)
        for v, c in ad_lst[u]:
            if costs[u] + c < costs[v]:
                alt = costs[u] + c
                heapq.heappush(Q, [alt, v])
                costs[v] = alt

    return costs

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

route_y = {i: [] for i in range(n+1)}
route_s = {i: [] for i in range(n+1)}
for i in range(m):
    u, v, a, b = map(int, input().split())
    route_y[u].append([v,a])
    route_y[v].append([u,a])
    route_s[u].append([v,b])
    route_s[v].append([u,b])

cost_y = dijkstra(route_y, n, s, t)
cost_s = dijkstra(route_s, n, t, s)
cost_ys = [y+s for y,s in zip(cost_y, cost_s)]

inf = float('inf')
result = [inf for _ in range(n+1)]
min_cost = result[-1]
for i in range(n, 0, -1):
    if min_cost > cost_ys[i]:
        min_cost = cost_ys[i]
    result[i] = min_cost
yen = 10**15
for i in range(1, n+1):
    print(yen - result[i])

Submission status: Time Limit Exceeded

Solution code:
import heapq

def dijkstra(ad_lst, n, s, t):
    costs = [float('inf') for i in range(n+1)]
    costs[s] = 0
    Q = []
    heapq.heappush(Q, [costs[s], s])

    while(Q != []):
        c, u = heapq.heappop(Q)
        for v, c in ad_lst[u]:
            if costs[u] + c < costs[v]:
                alt = costs[u] + c
                heapq.heappush(Q, [alt, v])
                costs[v] = alt

    return costs

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

route_y = {i: [] for i in range(n+1)}
route_s = {i: [] for i in range(n+1)}
for i in range(m):
    u, v, a, b = map(int, input().split())
    route_y[u].append([v,a])
    route_y[v].append([u,a])
    route_s[u].append([v,b])
    route_s[v].append([u,b])

cost_y = dijkstra(route_y, n, s, t)
cost_s = dijkstra(route_s, n, t, s)
cost_ys = [cost_y[i]+cost_s[i] for i in range(n+1)]

inf = float('inf')
result = [inf for _ in range(n+1)]
min_cost = result[-1]
for i in range(n, 0, -1):
    if min_cost > cost_ys[i]:
        min_cost = cost_ys[i]
    result[i] = min_cost
yen = 10**15
for i in range(1, n+1):
    print(yen - result[i])

Submission status: Time Limit Exceeded

Solution code:
import heapq

def dijkstra(ad_lst, n, s, t):
    costs = [float('inf') for i in range(n+1)]
    costs[s] = 0
    Q = []
    heapq.heappush(Q, [costs[s], s])

    while(Q != []):
        c, u = heapq.heappop(Q)
        for v, c in ad_lst[u]:
            alt = costs[u] + c
            if alt < costs[v]:
                heapq.heappush(Q, [alt, v])
                costs[v] = alt

    return costs

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

route_y = {i: [] for i in range(n+1)}
route_s = {i: [] for i in range(n+1)}
for i in range(m):
    u, v, a, b = map(int, input().split())
    route_y[u].append([v,a])
    route_y[v].append([u,a])
    route_s[u].append([v,b])
    route_s[v].append([u,b])

cost_y = dijkstra(route_y, n, s, t)
cost_s = dijkstra(route_s, n, t, s)
cost_ys = [[cost_y[i]+cost_s[i], i] for i in range(1,n+1)]

cost_ys.sort()
yen = 10**15
k = 0
for i in range(n):
    while cost_ys[k][1]<=i: k+=1
    print(yen-cost_ys[k][0])
# inf = float('inf')
# result = [inf for _ in range(n+1)]
# min_cost = result[-1]
# for i in range(n, 0, -1):
#     if min_cost > cost_ys[i]:
#         min_cost = cost_ys[i]
#     result[i] = min_cost
# yen = 10**15
# for i in range(1, n+1):
#     print(yen - result[i])

Submission status: Time Limit Exceeded

Solution code:
import heapq

def dijkstra(ad_lst, n, s, t):
    costs = [float('inf') for i in range(n+1)]
    costs[s] = 0
    prevs = [-1 for _ in range(n+1)]
    Q = []
    heapq.heappush(Q, [costs[s], s])

    while(Q != []):
        c, u = heapq.heappop(Q)
        for v, c in ad_lst[u]:
            alt = costs[u] + c
            if alt < costs[v]:
                heapq.heappush(Q, [alt, v])
                costs[v] = alt

    return costs

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

route_y = {i: [] for i in range(n+1)}
route_s = {i: [] for i in range(n+1)}
for i in range(m):
    u, v, a, b = map(int, input().split())
    route_y[u].append([v,a])
    route_y[v].append([u,a])
    route_s[u].append([v,b])
    route_s[v].append([u,b])

cost_y = dijkstra(route_y, n, s, t)
cost_s = dijkstra(route_s, n, t, s)
cost_ys = [y+s for y, s in zip(cost_y, cost_s)]
inf = float('inf')
result = [inf for _ in range(n+1)]
min_cost = result[-1]
for i in range(n, 0, -1):
    if min_cost > cost_ys[i]:
        min_cost = cost_ys[i]
    result[i] = min_cost
yen = 10**15
for i in range(1, n+1):
    print(yen - result[i])

Submission status: Time Limit Exceeded

Solution code:
import heapq

def dijkstra(ad_lst, n, s, t):
    costs = [float('inf') for i in range(n+1)]
    costs[s] = 0
    Q = []
    heapq.heappush(Q, [costs[s], s])

    while(Q != []):
        c, u = heapq.heappop(Q)
        for v, c in ad_lst[u]:
            alt = costs[u] + c
            if alt < costs[v]:
                heapq.heappush(Q, [alt, v])
                costs[v] = alt

    return costs

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

route_y = {i: [] for i in range(n+1)}
route_s = {i: [] for i in range(n+1)}
for i in range(m):
    u, v, a, b = map(int, input().split())
    route_y[u].append([v,a])
    route_y[v].append([u,a])
    route_s[u].append([v,b])
    route_s[v].append([u,b])

cost_y = dijkstra(route_y, n, s, t)
cost_s = dijkstra(route_s, n, t, s)
cost_ys = [y+s for y, s in zip(cost_y, cost_s)]
result = [float('inf') for _ in range(n+1)]
min_cost = result[-1]
for i in range(n, 0, -1):
    if min_cost > cost_ys[i]:
        min_cost = cost_ys[i]
    result[i] = min_cost
yen = 10**15
for i in range(1, n+1):
    print(yen - result[i])

Submission status: Time Limit Exceeded

Solution code:
import heapq

def dijkstra(ad_lst, n, s, t):
    costs = [float('inf') for i in range(n+1)]
    costs[s] = 0
    prevs = [-1 for _ in range(n+1)]
    Q = []
    heapq.heappush(Q, [costs[s], s])

    while(Q != []):
        c, u = heapq.heappop(Q)
        for v, c in ad_lst[u]:
            alt = costs[u] + c
            if alt < costs[v]:
                prevs[v] = u
                heapq.heappush(Q, [alt, v])
                costs[v] = alt

    return costs

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

route_y = {i: [] for i in range(n+1)}
route_s = {i: [] for i in range(n+1)}
for i in range(m):
    u, v, a, b = map(int, input().split())
    route_y[u].append([v,a])
    route_y[v].append([u,a])
    route_s[u].append([v,b])
    route_s[v].append([u,b])

cost_y = dijkstra(route_y, n, s, t)
cost_s = dijkstra(route_s, n, t, s)
cost_ys = [y+s for y, s in zip(cost_y, cost_s)]
result = [float('inf') for _ in range(n+1)]
min_cost = result[-1]
for i in range(n, 0, -1):
    if min_cost > cost_ys[i]:
        min_cost = cost_ys[i]
    result[i] = min_cost
yen = 10**15
for i in range(1, n+1):
    print(yen - result[i])

Submission status: Time Limit Exceeded

Solution code:
import collections
import heapq


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

    def add(self, u, v, d):
        self.e[u].append([v, d])
        self.e[v].append([u, 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):
        """
        :param s: 始点
        :return: 始点から各点までの最短経路
        """
        d = collections.defaultdict(lambda: float('inf'))
        d[s] = 0
        q = []
        heapq.heappush(q, (0, s))
        v = collections.defaultdict(bool)
        while len(q):
            k, u = heapq.heappop(q)
            if v[u]:
                continue
            v[u] = True

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