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():
    #隣接リスト版
    def __init__(self, adj, start):
        self.list = adj
        self.start = start
        self.size = len(adj)

    def solve(self):
        self.dist = [float("inf") for _ in range(self.size)]
        self.dist[self.start] = 0
        self.q = []

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

        while self.q:
            u = heapq.heappop(self.q)
            u_dist = self.dist[u]
            for v, w in self.list[u]:
                if self.dist[v] > u_dist + w:
                    self.dist[v] = u_dist + w
                    heapq.heappush(self.q, v)

        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:
def dijkstra_heap(n,s,edge):
    # 始点sから各頂点への最短距離
    d = [float("inf")] * n
    used = [True] * n  # True:未確定
    d[s] = 0
    used[s] = False
    edgelist = []
    for e in edge[s]:
        heappush(edgelist, e)
    while edgelist:
        minedge = heappop(edgelist)
        # まだ使われてない頂点の中から最小の距離のものを探す
        if not used[minedge[1]]:
            continue
        v = minedge[1]
        d[v] = minedge[0]
        used[v] = False
        for e in edge[v]:
            if used[e[1]]:
                heappush(edgelist, [e[0] + d[v], e[1]])
    return d

def examD():
    N, M, s, t = LI()
    edge = defaultdict(list)
    edge2 = defaultdict(list)
    # edge[i] : iから出る道の[重み,行先]の配列
    for i in range(M):
        x, y, z1, z2 = map(int, input().split())
        x -=1; y -=1
        edge[x].append([z1, y])
        edge[y].append([z1, x])
        edge2[x].append([z2, y])
        edge2[y].append([z2, x])
    start1 = s-1
    start2 = t-1
    res1 = dijkstra_heap(N,start1, edge)
    res2 = dijkstra_heap(N,start2, edge2)
    ans = []
    snuke = 10**15
    cur = res1[N-1] + res2[N-1]
    ans.append(snuke-cur)
    for i in range(N-2,-1,-1):
        if res1[i] + res2[i]<cur:
            cur = res1[i] + res2[i]
            ans.append(snuke-cur)
        else:
            ans.append(snuke-cur)
    for v in ans[::-1]:
        print(v)

import sys,copy,bisect,itertools,heapq,math
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
mod = 10**9 + 7
inf = float('inf')

examD()

Submission status: Time Limit Exceeded

Solution code:

def dijkstra_heap(n,s,edge):
    # 始点sから各頂点への最短距離
    d = [float("inf")] * n
    used = [True] * n  # True:未確定
    d[s] = 0
    used[s] = False
    edgelist = []
    for e in edge[s]:
        heappush(edgelist, e)
    while len(edgelist):
        minedge = heappop(edgelist)
        # まだ使われてない頂点の中から最小の距離のものを探す
        if not used[minedge[1]]:
            continue
        v = minedge[1]
        d[v] = minedge[0]
        used[v] = False
        for e in edge[v]:
            if used[e[1]]:
                heappush(edgelist, [e[0] + d[v], e[1]])
    return d

def examD():
    N, M, s, t = LI()
    edge = defaultdict(list)
    edge2 = defaultdict(list)
    # edge[i] : iから出る道の[重み,行先]の配列
    for i in range(M):
        x, y, z1, z2 = map(int, input().split())
        x -=1; y -=1
        edge[x].append([z1, y])
        edge[y].append([z1, x])
        edge2[x].append([z2, y])
        edge2[y].append([z2, x])
    start1 = s-1
    start2 = t-1
    res1 = dijkstra_heap(N,start1, edge)
    res2 = dijkstra_heap(N,start2, edge2)
    ans = []
    cur = res1[N-1] + res2[N-1]
    ans.append(cur)
    for i in range(N-2,-1,-1):
        if res1[i] + res2[i]<cur:
            cur = res1[i] + res2[i]
            ans.append(cur)
        else:
            ans.append(cur)
    ans.reverse()
    snuke = 10**15
    for v in ans:
        print(snuke-v)

import sys,copy,bisect,itertools,heapq,math
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
mod = 10**9 + 7
inf = float('inf')

examD()

Submission status: Time Limit Exceeded

Solution code:
def dijkstra_heap(n,s,edge):
    # 始点sから各頂点への最短距離
    d = [float("inf")] * n
    used = [True] * n  # True:未確定
    d[s] = 0
    used[s] = False
    edgelist = []
    for e in edge[s]:
        heappush(edgelist, e)
    while len(edgelist):
        minedge = heappop(edgelist)
        # まだ使われてない頂点の中から最小の距離のものを探す
        if not used[minedge[1]]:
            continue
        v = minedge[1]
        d[v] = minedge[0]
        used[v] = False
        for e in edge[v]:
            if used[e[1]]:
                heappush(edgelist, [e[0] + d[v], e[1]])
    return d

def examD():
    N, M, s, t = LI()
    edge = defaultdict(list)
    edge2 = defaultdict(list)
    # edge[i] : iから出る道の[重み,行先]の配列
    for i in range(M):
        x, y, z1, z2 = map(int, input().split())
        x -=1; y -=1
        edge[x].append([z1, y])
        edge[y].append([z1, x])
        edge2[x].append([z2, y])
        edge2[y].append([z2, x])
    start1 = s-1
    start2 = t-1
    res1 = dijkstra_heap(N,start1, edge)
    res2 = dijkstra_heap(N,start2, edge2)
    ans = []
    cur = res1[N-1] + res2[N-1]
    ans.append(cur)
    for i in range(N-2,-1,-1):
        cur = res1[i] + res2[i]
        if cur<ans[-1]:
            ans.append(cur)
        else:
            cur=ans[-1]
            ans.append(cur)
    ans.reverse()
    snuke = 10**15
    for v in ans:
        print(snuke-v)



import sys,copy,bisect,itertools,heapq,math
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
mod = 10**9 + 7
inf = float('inf')

examD()

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline

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

N, M, S, G = map(int, input().split())
ABCD = np.array([list(map(int, input().split())) for _ in range(M)])
A = ABCD[:, 0]
B = ABCD[:, 1]
C = ABCD[:, 2]
D = ABCD[:, 3]

graph_y = csr_matrix((C, (A, B)), (N+1, N+1))
graph_s = csr_matrix((D, (A, B)), (N+1, N+1))

Ds = dijkstra(graph_y, directed=False, indices=S).astype('int64')
Dg = dijkstra(graph_s, directed=False, indices=G).astype('int64')

ans = Ds + Dg
for i in reversed(range(1, N)):
    ans[i] = min(ans[i+1], ans[i])

for n in range(1, N+1):
    print(10**15-ans[n])

Submission status: Time Limit Exceeded

Solution code:
from scipy.sparse.csgraph import csgraph_from_dense, dijkstra

n, m, s, t = map(int, input().split())
moni = 10**15
graph_y = [[-1]*n for _ in range(n)]
graph_s = [[-1]*n for _ in range(n)]

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

op_y = dijkstra(csgraph_from_dense(graph_y, null_value=-1), directed=False)
op_s = dijkstra(csgraph_from_dense(graph_s, null_value=-1), directed=False)

l = [float("inf") for _ in range(n)]
l[n-1] = int(op_y[s-1][n-1] + op_s[t-1][n-1])
for i in range(n-2, -1, -1):
	k = int(op_y[s-1][i] + op_s[t-1][i])
	if k < l[i+1]:
		l[i] = k
	else:
		l[i] = l[i+1]

for i in range(n):
	print(moni - l[i])

Submission status: Time Limit Exceeded

Solution code:
from scipy.sparse.csgraph import csgraph_from_dense, dijkstra

n, m, s, t = map(int, input().split())
moni = 10**15
graph_y = [[-1 for _ in range(n)] for _ in range(n)]
graph_s = [[-1 for _ in range(n)] for _ in range(n)]

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

op_y = dijkstra(csgraph_from_dense(graph_y, null_value=-1), directed=False)
op_s = dijkstra(csgraph_from_dense(graph_s, null_value=-1), directed=False)

l = [float("inf") for _ in range(n)]
for i in range(n):
	l[i] = op_y[s-1][i] + op_s[t-1][i]

for i in range(n):
	print(int(moni - min(l[i::])))

Submission status: Time Limit Exceeded

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


class Edge:
    def __init__(self, to, yen, snuuk):
        self.to = to
        self.yen = yen
        self.snuuk = snuuk


edges = [list() for _ in range(n)]

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

dist = [10**10]*n

heap = []
heapq.heapify(heap)
# heapq.heappush()

q = collections.deque()
q.append((s-1, 0))

while(len(q) != 0):
    v, cost = q.popleft()
    if dist[v] <= cost:
        continue
    dist[v] = cost
    for i in edges[v]:
        q.append((i.to, cost+i.yen))

q2 = collections.deque()
q2.append((t-1, 0))
dist2 = [10**10]*n

while(len(q2) != 0):
    v, cost = q2.popleft()
    if dist2[v] <= cost:
        continue
    dist2[v] = cost
    for i in edges[v]:
        q2.append((i.to, cost+i.snuuk))

# print(dist)
# print(dist2)
ans = []

for i in range(n):
    ans.append(10**15-dist[i]-dist2[i])


for i in range(n):
    print(max(ans[i:]))

Submission status: Time Limit Exceeded

Solution code:
import heapq
import sys

input = sys.stdin.readline
INF = 10**18

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

graph_s = [[] for _ in range(n)]
graph_t = [[] for _ in range(n)]

for e in edge:
    graph_s[e[0]-1].append([e[1]-1,e[2]])
    graph_s[e[1]-1].append([e[0]-1,e[2]])
    graph_t[e[0]-1].append([e[1]-1,e[3]])
    graph_t[e[1]-1].append([e[0]-1,e[3]])

dist_s = [INF]*n
dist_t = [INF]*n

dist_s[s-1] = 0
dist_t[t-1] = 0

heap_s = [[0,s-1]]
heapq.heapify(heap_s)

heap_t = [[0,t-1]]
heapq.heapify(heap_t)

while heap_s:
    cost,node = heapq.heappop(heap_s)
    if dist_s[node] < cost:
        continue
    for adj,adjcost in graph_s[node]:
        if dist_s[node] + adjcost < dist_s[adj]:
            dist_s[adj] = dist_s[node] + adjcost
            heapq.heappush(heap_s,[dist_s[adj],adj])

while heap_t:
    cost,node = heapq.heappop(heap_t)
    if dist_t[node] < cost:
        continue
    for adj,adjcost in graph_t[node]:
        if dist_t[node] + adjcost < dist_t[adj]:
            dist_t[adj] = dist_t[node] + adjcost
            heapq.heappush(heap_t,[dist_t[adj],adj])

dist = INF
ans = []
                                              
for i in range(n):
    dist = min(dist,dist_t[-1-i]+dist_s[-1-i])
    ans.append(10**15-dist)
    
for i in range(n):
    print(ans[-1-i])

Submission status: Time Limit Exceeded

Solution code:
import heapq

INF = 10**18

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

graph_s = [[] for _ in range(n)]
graph_t = [[] for _ in range(n)]

for e in edge:
    graph_s[e[0]-1].append([e[1]-1,e[2]])
    graph_s[e[1]-1].append([e[0]-1,e[2]])
    graph_t[e[0]-1].append([e[1]-1,e[3]])
    graph_t[e[1]-1].append([e[0]-1,e[3]])

dist_s = [INF]*n
dist_t = [INF]*n

dist_s[s-1] = 0
dist_t[t-1] = 0

heap_s = [[0,s-1]]
heapq.heapify(heap_s)

heap_t = [[0,t-1]]
heapq.heapify(heap_t)

while heap_s:
    cost,node = heapq.heappop(heap_s)
    if dist_s[node] < cost:
        continue
    for adj,adjcost in graph_s[node]:
        if dist_s[node] + adjcost < dist_s[adj]:
            dist_s[adj] = dist_s[node] + adjcost
            heapq.heappush(heap_s,[dist_s[adj],adj])

while heap_t:
    cost,node = heapq.heappop(heap_t)
    if dist_t[node] < cost:
        continue
    for adj,adjcost in graph_t[node]:
        if dist_t[node] + adjcost < dist_t[adj]:
            dist_t[adj] = dist_t[node] + adjcost
            heapq.heappush(heap_t,[dist_t[adj],adj])

dist = INF
ans = []
                                              
for i in range(n):
    dist = min(dist,dist_t[-1-i]+dist_s[-1-i])
    ans.append(10**15-dist)
    
for i in range(n):
    print(ans[-1-i])

Submission status: Time Limit Exceeded

Solution code:
import heapq
import sys
input = sys.stdin.readline


def dijkstra(start: int, graph: list) -> list:
    """dijkstra法: 始点startから各頂点への最短距離を求める
    重み付きグラフgraphは隣接リストで与えられる
    計算量: O(|E|log|V|)
    """
    INF = float("inf")
    n = len(graph)
    distance = [INF] * n
    distance[start] = 0
    
    q = [(start, 0)]  #q = [(現在地, startからの距離)]
    while q:
        pos, dist = heapq.heappop(q)
        if distance[pos] < dist:
            continue
        distance[pos] = dist
        for next_pos, cost in graph[pos]:
            if distance[pos] + cost < distance[next_pos]:
                distance[next_pos] = distance[pos] + cost
                heapq.heappush(q, (next_pos, distance[next_pos]))
    return distance


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

y_graph = [[] for i in range(n)]
s_graph = [[] for i in range(n)]
for u, v, a, b in info:
    u -= 1
    v -= 1
    y_graph[u].append((v, a))
    y_graph[v].append((u, a))
    s_graph[u].append((v, b))
    s_graph[v].append((u, b))

s -= 1
t -= 1
y_cost = dijkstra(s, y_graph)
s_cost = dijkstra(t, s_graph)

ans = [0]*n
for i in range(n):
    ans[i] = y_cost[i] + s_cost[i]

for i in range(n-1)[::-1]:
    ans[i] = min(ans[i], ans[i+1])

for num in ans:
    print(10**15 - num)

Submission status: Time Limit Exceeded

Solution code:
import heapq
import sys
input = sys.stdin.readline

def dijkstra(start: int, graph: list) -> list:
    """dijkstra法: 始点startから各頂点への最短距離を求める
    重み付きグラフgraphは隣接リストで与えられる
    計算量: O(|E|log|V|)
    """
    INF = float("inf")
    n = len(graph)
    distance = [INF] * n
    distance[start] = 0
    q = [(start, 0)]  #q = [(現在地, startからの距離)]
    while q:
        pos, dist = heapq.heappop(q)
        if distance[pos] < dist:
            continue
        for next_pos, cost in graph[pos]:
            if distance[pos] + cost < distance[next_pos]:
                distance[next_pos] = distance[pos] + cost
                heapq.heappush(q, (next_pos, distance[next_pos]))
    return distance


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

y_graph = [[] for i in range(n)]
s_graph = [[] for i in range(n)]
for u, v, a, b in info:
    u -= 1
    v -= 1
    y_graph[u].append((v, a))
    y_graph[v].append((u, a))
    s_graph[u].append((v, b))
    s_graph[v].append((u, b))

s -= 1
t -= 1
y_cost = dijkstra(s, y_graph)
s_cost = dijkstra(t, s_graph)

ans = [0]*n
for i in range(n):
    ans[i] = y_cost[i] + s_cost[i]

for i in range(n-1)[::-1]:
    ans[i] = min(ans[i], ans[i+1])

for num in ans:
    print(10**15 - num)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
import sys
input = sys.stdin.readline
import heapq

N, M, S, T = map(int, input().split())
graph = [[] for _ in range(N+1)]
for _ in range(M):
	tu,tv,ta,tb = map(int, input().split())
	graph[tu].append([tv,ta,tb])
	graph[tv].append([tu,ta,tb])

def dijkstra(start, mode):
	inf = float("inf")
	dict = [inf]*(N+1)
	dict[start] = 0
	
	q = []
	heapq.heapify(q)
	heapq.heappush(q, [0, start])

	while q:
		cd, cp = heapq.heappop(q)
		next = []
		cost = []
		for i in range(len(graph[cp])):
			tnp, tna, tnb = graph[cp][i]
			next.append(tnp)
			cost.append([tna,tnb])

		for next_p, *d in graph[cp]:
			cmp_cost = cd + d[mode]
			if  cmp_cost < dict[next_p]:
				dict[next_p] = cmp_cost
				heapq.heappush(q, [cmp_cost, next_p])

	return dict
				
Yen = np.array(dijkstra(S,0))
Snk = np.array(dijkstra(T,1))

tot = (Yen + Snk).tolist()

heap = []
heapq.heapify(heap)
ans = []
for i in reversed(range(1,N+1)):
	heapq.heappush(heap,tot[i])
	ans.append(heap[0])

for i in reversed(range(len(ans))):
	print(int(10**15-ans[i]))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
import sys
input = sys.stdin.readline
import heapq

N, M, S, T = map(int, input().split())
graph = [[] for _ in range(N+1)]
for _ in range(M):
	tu,tv,ta,tb = map(int, input().split())
	graph[tu].append([tv,ta,tb])
	graph[tv].append([tu,ta,tb])

def dijkstra(start, mode):
	inf = float("inf")
	dict = [inf]*(N+1)
	dict[start] = 0
	
	q = []
	heapq.heapify(q)
	heapq.heappush(q, [0, start])

	while not(len(q)==0):
		cd, cp = heapq.heappop(q)
		next = []
		cost = []
		for i in range(len(graph[cp])):
			tnp, tna, tnb = graph[cp][i]
			next.append(tnp)
			cost.append([tna,tnb])

		for next_p, *d in graph[cp]:
			cmp_cost = cd + d[mode]
			if  cmp_cost < dict[next_p]:
				dict[next_p] = cmp_cost
				heapq.heappush(q, [cmp_cost, next_p])

	return dict
				
Yen = np.array(dijkstra(S,0))
Snk = np.array(dijkstra(T,1))

tot = (Yen + Snk).tolist()

heap = []
heapq.heapify(heap)
ans = []
for i in reversed(range(1,N+1)):
	heapq.heappush(heap,tot[i])
	ans.append(heap[0])

for i in reversed(range(len(ans))):
	print(int(10**15-ans[i]))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
import sys
input = sys.stdin.readline
import heapq

N, M, S, T = map(int, input().split())
graph = [[] for _ in range(N+1)]
for _ in range(M):
	tu,tv,ta,tb = map(int, input().split())
	graph[tu].append([tv,ta,tb])
	graph[tv].append([tu,ta,tb])

def dijkstra(start, mode):
	inf = float("inf")
	dict = [inf]*(N+1)
	dict[start] = 0
	flg = [0] * (N+1)
	flg[0] = 1
	flg[start] = 1

	q = []
	heapq.heapify(q)
	heapq.heappush(q, [0, start])

	while not(flg.count(0) == 0):
		cd, cp = heapq.heappop(q)
		flg[cp] = 1
		next = []
		cost = []
		for i in range(len(graph[cp])):
			tnp, tna, tnb = graph[cp][i]
			next.append(tnp)
			cost.append([tna,tnb])

		for next_p, *d in graph[cp]:
			cmp_cost = cd + d[mode]
			if  cmp_cost < dict[next_p]:
				dict[next_p] = cmp_cost
				heapq.heappush(q, [cmp_cost, next_p])

	return dict
				
Yen = np.array(dijkstra(S,0))
Snk = np.array(dijkstra(T,1))

tot = (Yen + Snk).tolist()

heap = []
heapq.heapify(heap)
ans = []
for i in reversed(range(1,N+1)):
	heapq.heappush(heap,tot[i])
	ans.append(heap[0])

for i in reversed(range(len(ans))):
	print(int(10**15-ans[i]))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
import sys
input = sys.stdin.readline
import heapq

N, M, S, T = map(int, input().split())
graph = [[] for _ in range(N+1)]
for _ in range(M):
	tu,tv,ta,tb = map(int, input().split())
	graph[tu].append([tv,ta,tb])
	graph[tv].append([tu,ta,tb])

def dijkstra(start, mode):
	inf = float("inf")
	dict = [inf]*(N+1)
	dict[start] = 0
	flg = [0] * (N+1)
	flg[0] = 1
	flg[start] = 1

	q = []
	heapq.heapify(q)
	heapq.heappush(q, [0, start])

	while not(flg.count(0) == 0):
		cd, cp = heapq.heappop(q)
		flg[cp] = 1
		next = []
		cost = []
		for i in range(len(graph[cp])):
			tnp, tna, tnb = graph[cp][i]
			next.append(tnp)
			cost.append([tna,tnb])

		for i in range(len(next)):
			cmp_cost = cd + cost[i][mode]
			next_p = next[i]
			if  cmp_cost < dict[next_p]:
				dict[next_p] = cmp_cost
				heapq.heappush(q, [cmp_cost, next_p])

	return dict
				
Yen = np.array(dijkstra(S,0))
Snk = np.array(dijkstra(T,1))

tot = (Yen + Snk).tolist()

heap = []
heapq.heapify(heap)
ans = []
for i in reversed(range(1,N+1)):
	heapq.heappush(heap,tot[i])
	ans.append(heap[0])

for i in reversed(range(len(ans))):
	print(int(10**15-ans[i]))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
from scipy.sparse.csgraph import shortest_path, floyd_warshall, dijkstra, bellman_ford, johnson
from scipy.sparse import csr_matrix
import heapq
import sys
input = sys.stdin.readline

N, M, S, T = map(int, input().split())
u = []
v = []
a = []
b = []

for _ in range(M):
	tu, tv, ta, tb = map(int, input().split())
	u.append(tu)
	v.append(tv)
	a.append(ta)
	b.append(tb)

inf = float("inf")
Yen = [[inf for _ in range(N)] for _ in range(N)]
Snk = [[inf for _ in range(N)] for _ in range(N)]

for i in range(M):
	uu = u[i]-1
	vv = v[i]-1
	Yen[uu][vv] = a[i]
	Yen[vv][uu] = a[i]
	Snk[uu][vv] = b[i]
	Snk[vv][uu] = b[i]

Yen = np.array(Yen)
Snk = np.array(Snk)

Yen_dij = dijkstra(Yen,indices = S-1)
Snk_dij = dijkstra(Snk, indices = T-1)

tot = [Yen_dij[i]+Snk_dij[i] for i in range(N)]

heap = []
ans = []
heapq.heapify(heap)
for i in reversed(range(N)):
	heapq.heappush(heap, tot[i])
	ans.append(heap[0])

for i in reversed(range(N)):
	print(int(10**15-ans[i]))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
from scipy.sparse.csgraph import shortest_path, floyd_warshall, dijkstra, bellman_ford, johnson
from scipy.sparse import csr_matrix
import heapq


N, M, S, T = map(int, input().split())
u = []
v = []
a = []
b = []

for _ in range(M):
	tu, tv, ta, tb = map(int, input().split())
	u.append(tu)
	v.append(tv)
	a.append(ta)
	b.append(tb)

inf = float("inf")
Yen = [[inf for _ in range(N)] for _ in range(N)]
Snk = [[inf for _ in range(N)] for _ in range(N)]

for i in range(M):
	uu = u[i]-1
	vv = v[i]-1
	Yen[uu][vv] = a[i]
	Yen[vv][uu] = a[i]
	Snk[uu][vv] = b[i]
	Snk[vv][uu] = b[i]

Yen = np.array(Yen)
Snk = np.array(Snk)

Yen_dij = dijkstra(Yen,indices = S-1)
Snk_dij = dijkstra(Snk, indices = T-1)

tot = [Yen_dij[i]+Snk_dij[i] for i in range(N)]

heap = []
ans = []
heapq.heapify(heap)
for i in reversed(range(N)):
	heapq.heappush(heap, tot[i])
	ans.append(heap[0])

for i in reversed(range(N)):
	print(int(10**15-ans[i]))

Submission status: Time Limit Exceeded

Solution code:
n,m,s,t=map(int,input().split())
uvab=[map(int,input().split()) for i in range(m)]
inf=10**20
c1=[[inf]*n for i in range(n)]
c2=[[inf]*n for i in range(n)]
for u,v,a,b in uvab:
    c1[u-1][v-1]=c1[v-1][u-1]=a
    c2[u-1][v-1]=c2[v-1][u-1]=b
for i in range(n):
    c1[i][i]=c2[i][i]=0
from heapq import heappop,heappush
def dij(c,N,start):
    d=[inf]*N
    d[start]=0
    prev=[-1]*N
    q=[]
    heappush(q,(0,start))
    while len(q)>0:
        u=heappop(q)[1]
        for v in range(N):
            if c[u][v]<inf:
                temp=d[u]+c[u][v]
                if d[v]>temp:
                    d[v]=temp
                    prev[v]=u
                    heappush(q,(d[v],v))
    return d
d1=dij(c1,n,s-1)
d2=dij(c2,n,t-1)
r=[0]*n
for i in range(n):
    r[-i-1]=max(r[-i],10**15-int(d1[-i-1])-int(d2[-i-1]))
print(*r,sep='\n')

Submission status: Time Limit Exceeded

Solution code:
n,m,s,t=map(int,input().split())
import numpy as np
import sys
def input(): return sys.stdin.readline().strip()
u,v,a,b=map(np.array,zip(*[map(int,input().split()) for i in range(m)]))
from scipy.sparse import csr_matrix
g1=csr_matrix((a,(u-1,v-1)),shape=(n,n))
g2=csr_matrix((b,(u-1,v-1)),shape=(n,n))
from scipy.sparse.csgraph import dijkstra
d1=dijkstra(g1,directed=False,indices=s-1)
d2=dijkstra(g2,directed=False,indices=t-1)
D=10**15-d1-d2
r=np.maximum.accumulate(D[::-1])[::-1].astype(np.int64)
print(*r,sep='\n')

Submission status: Time Limit Exceeded

Solution code:
n,m,s,t=map(int,input().split())
import numpy as np
import sys
def input(): return sys.stdin.readline().strip()
u,v,a,b=map(np.array,zip(*[map(int,input().split()) for i in range(m)]))
from scipy.sparse import csr_matrix
g1=csr_matrix((a,(u-1,v-1)),shape=(n,n))
g2=csr_matrix((b,(u-1,v-1)),shape=(n,n))
from scipy.sparse.csgraph import dijkstra
d1=dijkstra(g1,directed=False,indices=s-1)
d2=dijkstra(g2,directed=False,indices=t-1)
D=np.ones(n)*10**15-d1-d2
r=[0]*n
for i in range(n):
    r[-i-1]=max(r[-i],int(D[-i-1]))
print(*r,sep='\n')

Submission status: Time Limit Exceeded

Solution code:
n,m,s,t=map(int,input().split())
import numpy as np
import sys
def input(): return sys.stdin.readline().strip()
u,v,a,b=map(np.array,zip(*[map(int,input().split()) for i in range(m)]))
from scipy.sparse import csr_matrix
g1=csr_matrix((a,(u-1,v-1)),shape=(n,n))
g2=csr_matrix((b,(u-1,v-1)),shape=(n,n))
from scipy.sparse.csgraph import dijkstra
d1=dijkstra(g1,directed=False,indices=s-1)
d2=dijkstra(g2,directed=False,indices=t-1)
r=[0]*n
for i in range(n):
    r[-i-1]=max(r[-i],10**15-int(d1[-i-1])-int(d2[-i-1]))
print(*r,sep='\n')

Submission status: Time Limit Exceeded

Solution code:
n,m,s,t=map(int,input().split())
import numpy as np
u,v,a,b=map(np.array,zip(*[list(map(int,input().split())) for i in range(m)]))
from scipy.sparse import csr_matrix
g1=csr_matrix((a,(u-1,v-1)),shape=(n,n))
g2=csr_matrix((b,(u-1,v-1)),shape=(n,n))
from scipy.sparse.csgraph import dijkstra
d1=dijkstra(g1,directed=False,indices=s-1)
d2=dijkstra(g2,directed=False,indices=t-1)
r=[0]*n
for i in range(n):
    r[-i-1]=max(r[-i],10**15-int(d1[-i-1])-int(d2[-i-1]))
print(*r,sep='\n')

Submission status: Time Limit Exceeded

Solution code:
n,m,s,t=map(int,input().split())
uvab=[list(map(int,input().split())) for i in range(m)]
c1=[[0]*n for i in range(n)]
c2=[[0]*n for i in range(n)]
for u,v,a,b in uvab:
    c1[u-1][v-1]=c1[v-1][u-1]=a
    c2[u-1][v-1]=c2[v-1][u-1]=b
from scipy.sparse import csr_matrix
g1=csr_matrix(c1)
g2=csr_matrix(c2)
from scipy.sparse.csgraph import dijkstra
d1=dijkstra(g1,directed=False,indices=s-1)
d2=dijkstra(g2,directed=False,indices=t-1)
r=[0]*n
for i in range(n):
    r[-i-1]=max(r[-i],10**15-int(d1[-i-1])-int(d2[-i-1]))
print(*r,sep='\n')

Submission status: Time Limit Exceeded

Solution code:
import heapq

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

cost = [[] for _ in range(n)]
for i in range(m):
    cost[L[i][0]-1].append(L[i][2:]+[L[i][1]-1])
    cost[L[i][1]-1].append(L[i][2:]+[L[i][0]-1])
    
def dijkstra(s, n, cost, c):
    d = [10**15]*n
    used = [0]*n
    d[s] = 0
    used[s] = 1
    edgelist = []
    for e in cost[s]:
        heapq.heappush(edgelist, [e[c], e[2]])
    while len(edgelist):
        minedge = heapq.heappop(edgelist)
        if used[minedge[1]] == 1:
            continue
        v = minedge[1]
        d[v] = minedge[0]
        used[v] = 1
        for e in cost[v]:
            if used[e[2]] == 0:
                heapq.heappush(edgelist, [e[c]+d[v], e[2]])
    return d

jpy = dijkstra(s-1, n, cost, 0)
snuuk = dijkstra(t-1, n, cost, 1)
ans_list = [0]*n
ans_list[n-1] = jpy[n-1]+snuuk[n-1]
for i in range(n-2, -1, -1):
    ans_list[i] = min(ans_list[i+1], jpy[i]+snuuk[i])
    
for i in range(n):
    print(10**15-ans_list[i])

Submission status: Time Limit Exceeded

Solution code:
n,m,s,t = map(int, input().split())
L = [[int(x) for x in input().split()] for _ in range(m)]

cost = [[[10**15, 10**15] for _ in range(n)] for _ in range(n)]
for i in range(m):
    cost[L[i][0]-1][L[i][1]-1] = L[i][2:]
    cost[L[i][1]-1][L[i][0]-1] = L[i][2:]
    
for i in range(n):
    cost[i][i] = [0, 0]
    
def dijkstra(s, n, cost, c):
    d = [10**15]*n
    used = [0]*n
    d[s] = 0
    while True:
        v = -1
        for i in range(n):
            if used[i] == 0 and v == -1:
                v = i
            elif used[i] == 0 and d[i] < d[v]:
                v = i
        if v == -1:
            break
        used[v] = 1
        
        for j in range(n):
            d[j] = min(d[j], d[v]+cost[v][j][c])
    return d

jpy = dijkstra(s-1, n, cost, 0)
snook = dijkstra(t-1, n, cost, 1)
ans_list = [10**15]*n
ans_list[n-1] = jpy[n-1]+snook[n-1]
for i in range(n-2, -1, -1):
    ans_list[i] = min(ans_list[i+1], jpy[i]+snook[i])
    
for i in range(n):
    print(10**15-ans_list[i])