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:
n, m, s, t = map(int, input().split())
route = []
for i in range(m):
    route.append(list(map(int, input().split())))


def dijkstra(n, s, t, route, a_or_b):
    if a_or_b == "a":
        kane = 2
    else:
        kane = 3

    cost = {}
    for i in range(n):
        cost[i+1] = float("inf")

    decision = {s:0}
    cost[s] = 0

    while(not t in decision):
        for i in route:
            if (i[0] in decision) and (i[1] not in decision):
                cost[i[1]] = min(cost[i[1]], cost[i[0]] + i[kane])
            elif (i[1] in decision) and (i[0] not in decision):
                cost[i[0]] = min(cost[i[0]], cost[i[1]] + i[kane])

        min_tosi = [0,float("inf")]
        for i in range(n):
            if i+1 not in decision:
                if min_tosi[1] > cost[i+1]:
                    min_tosi = [i+1, cost[i+1]]
        decision[min_tosi[0]] = min_tosi[1]

    return cost[t]

# print(dijkstra(n, s, t, rout, "b"))

for year in range(n):
    min_cost = float("inf")
    for i in range(year,n):
        min_cost = min(min_cost, dijkstra(n, s, i+1, route, "a") + dijkstra(n, i+1, t, route, "b"))

    print(10**15 - min_cost)

Submission status: Time Limit Exceeded

Solution code:
import heapq

INF = 10 ** 15

# node, edge, start, stop
n, m, s, t = map(int, input().split())
s -= 1
t -= 1

u = [] # edge (smaller)
v = [] # edge (larger)
a = [] # fare (yen)
b = [] # fare (snuuk)
for i in range(m):
    inp = list(map(int, input().split()))
    u.append(inp[0] - 1)
    v.append(inp[1] - 1)
    a.append(inp[2])
    b.append(inp[3])

uu = [[] for i in range(n)]

for i in range(m):
    uu[u[i]].append([v[i], a[i], b[i]])
    uu[v[i]].append([u[i], a[i], b[i]])

# Dijkstra's Algorithm
#   fare: 1 => yen, 2 => snuuk
def dijkstra(start, fare):

    flag = [0 for i in range(n)]
    distance = [-1 for i in range(n)] # -1: infinity
    flag[start] = 1
    distance[start] = 0
    base = start
    changeList = set([])

    # min heap (distance, node)
    hp = []

    for i in range(n):
        # update distance
        for j in range(len(uu[base])):
            if (flag[uu[base][j][0]] == 0):
                befo = distance[uu[base][j][0]]
                afte = distance[base] + uu[base][j][fare]
                if (afte < befo or befo == -1):
                    distance[uu[base][j][0]] = afte
                    heapq.heappush(hp, (afte, uu[base][j][0]))

        # update base
        while True:
            if (len(hp) == 0):
                break
            base = hp[0][1]
            heapq.heappop(hp)
            if (flag[base] == 0):
                break

        # update flag (using updated base)
        flag[base] = 1

    return distance

totalYen = dijkstra(s, 1)
totalSnuuke = dijkstra(t, 2)

totalFare = []
for ex in range(n): # ex: exchange
    totalFare.append(totalYen[ex] + totalSnuuke[ex])

for i in range(n):
    money = 10 ** 15
    print(money - min(totalFare))
    del totalFare[0]

Submission status: Time Limit Exceeded

Solution code:
# node, edge, start, stop
n, m, s, t = map(int, input().split())
s -= 1
t -= 1

u = [] # edge (smaller)
v = [] # edge (larger)
a = [] # fare (yen)
b = [] # fare (snuuk)
for i in range(m):
    inp = list(map(int, input().split()))
    u.append(inp[0] - 1)
    v.append(inp[1] - 1)
    a.append(inp[2])
    b.append(inp[3])

uu = [[] for i in range(n)]

for i in range(m):
    uu[u[i]].append([v[i], a[i], b[i]])
    uu[v[i]].append([u[i], a[i], b[i]])

# Dijkstra's Algorithm
#   fare: 1 => yen, 2 => snuuk
def dijkstra(start, fare):

    flag = [0 for i in range(n)]
    distance = [-1 for i in range(n)] # -1: infinity
    flag[start] = 1
    distance[start] = 0
    base = start
    changeList = set([])

    for i in range(n - 1):
        # update distance
        for j in range(len(uu[base])):
                befo = distance[uu[base][j][0]]
                afte = distance[base] + uu[base][j][fare]
                if (afte < befo or befo == -1):
                    distance[uu[base][j][0]] = afte
                    changeList.add(uu[base][j][0])

        # update base
        base = list(changeList)[0]
        for j in changeList:
            if (distance[j] < distance[base]):
                base = j
        changeList.remove(base)

        # update flag (using updated base)
        flag[base] = 1

    return distance

totalYen = dijkstra(s, 1)
totalSnuuke = dijkstra(t, 2)

totalFare = []
for ex in range(n): # ex: exchange
    totalFare.append(totalYen[ex] + totalSnuuke[ex])

for i in range(n):
    money = 10 ** 15
    print(money - min(totalFare))
    del totalFare[0]

Submission status: Time Limit Exceeded

Solution code:
from heapq import *
from numpy import array,inf


def djikstra(s,G):
    que = []
    d = [inf]*V
    d[s] = 0
    heappush(que,(0,s))
    while(len(que)!=0):
        p = heappop(que)
        v = p[1]
        if d[v] < p[0]:
            continue
        for i in range(len(G[v])):
            e = G[v][i]
            if d[e[0]] > d[v] + e[1]:
                d[e[0]] = d[v] + e[1]
                heappush(que,(d[e[0]],e[0]))
    return d




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


S = djikstra(s-1,Ga)
T = djikstra(t-1,Gb)

M = 10**15-(array(S)+array(T))
ans = [0]*n
ans[n-1] = M[n-1]
for i in range(n-2,-1,-1):
    ans[i] = max(ans[i+1],M[i])

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

Submission status: Time Limit Exceeded

Solution code:
from heapq import *
from numpy import array,inf


def djikstra(s):
    que = []
    d = [inf]*V
    d[s] = 0
    visited = [False] * V
    heappush(que,(0,s))
    while(len(que)!=0):
        p = heappop(que)
        v = p[1]
        if visited[v]:
            continue
        visited[v] = True
        for i in range(len(G[v])):
            e = G[v][i]
            if visited[e[0]]:
                continue
            if d[e[0]] > d[v] + e[1]:
                d[e[0]] = d[v] + e[1]
                heappush(que,(d[e[0]],e[0]))
    return d


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

G = Ga
S = djikstra(s-1)
G = Gb
T = djikstra(t-1)

M = 10**15-(array(S)+array(T))
ans = [0]*n
ans[n-1] = M[n-1]
for i in range(n-2,-1,-1):
    ans[i] = max(ans[i+1],M[i])

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

Submission status: Time Limit Exceeded

Solution code:
from heapq import *
from numpy import array,inf


def djikstra(s):
    que = []
    d = [inf]*V
    d[s] = 0
    heappush(que,(0,s))
    while(len(que)!=0):
        p = heappop(que)
        v = p[1]
        if d[v] < p[0]:
            continue
        for i in range(len(G[v])):
            e = G[v][i]
            if d[e[0]] > d[v] + e[1]:
                d[e[0]] = d[v] + e[1]
                heappush(que,(d[e[0]],e[0]))
    return d


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

G = Ga
S = djikstra(s-1)
G = Gb
T = djikstra(t-1)

M = 10**15-(array(S)+array(T))
ans = [0]*n
ans[n-1] = M[n-1]
for i in range(n-2,-1,-1):
    ans[i] = max(ans[i+1],M[i])

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

Submission status: Time Limit Exceeded

Solution code:
from heapq import *
class PriorityQueue(object):
    def __init__(self):
        self.queue = []
    def push(self, value):
        heappush(self.queue, value)
    def pop(self):
        return heappop(self.queue)
    def empty(self):
        return len(self.queue) == 0
    def size(self):
        return len(self)

from numpy import array,inf


def djikstra(s):
    que = PriorityQueue()
    d = [inf]*V
    d[s] = 0
    que.push((0,s))
    while(not que.empty()):
        p = que.pop()
        v = p[1]
        if d[v] < p[0]:
            continue
        for i in range(len(G[v])):
            e = G[v][i]
            if d[e[0]] > d[v] + e[1]:
                d[e[0]] = d[v] + e[1]
                que.push((d[e[0]],e[0]))
    return d


n,m,s,t = map(int,input().split())
V = n
Ga = [[]]*n
Gb = [[]]*n
for i in range(n):
    Ga[i]=[]
    Gb[i]=[]
for i in range(m):
    u,v,a,b = map(int,input().split())
    Ga[u-1].append((v-1,a))
    Ga[v-1].append((u-1,a))
    Gb[u-1].append((v-1,b))
    Gb[v-1].append((u-1,b))

G = Ga
S = djikstra(s-1)
G = Gb
T = djikstra(t-1)

M = array(S)+array(T)

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

Submission status: Time Limit Exceeded

Solution code:
class edge():
    def __init__(self,to,cost):
        self.to = to
        self.cost = cost

from heapq import *
class PriorityQueue(object):
    def __init__(self):
        self.queue = []
    def push(self, value):
        heappush(self.queue, value)
    def pop(self):
        return heappop(self.queue)
    def empty(self):
        return len(self.queue) == 0
    def size(self):
        return len(self)

from numpy import array,inf


def djikstra(s):
    que = PriorityQueue()
    d = [inf]*V
    d[s] = 0
    que.push((0,s))
    while(not que.empty()):
        p = que.pop()
        v = p[1]
        if d[v] < p[0]:
            continue
        for i in range(len(G[v])):
            e = G[v][i]
            if d[e.to] > d[v] + e.cost:
                d[e.to] = d[v] + e.cost
                que.push((d[e.to],e.to))
    return d


n,m,s,t = map(int,input().split())
V = n
Ga = {}
Gb = {}
for i in range(n):
    Ga[i]=[]
    Gb[i]=[]
for i in range(m):
    u,v,a,b = map(int,input().split())
    Ga[u-1].append(edge(v-1,a))
    Ga[v-1].append(edge(u-1,a))
    Gb[u-1].append(edge(v-1,b))
    Gb[v-1].append(edge(u-1,b))

G = Ga
S = djikstra(s-1)
G = Gb
T = djikstra(t-1)

M = array(S)+array(T)

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

Submission status: Time Limit Exceeded

Solution code:
inp = list(map(int, input().split()))
n = inp[0] # the num of node
m = inp[1] # the num of edge
s = inp[2] - 1 # start
t = inp[3] - 1 # stop

u = [] # edge (smaller)
v = [] # edge (larger)
a = [] # fare (yen)
b = [] # fare (snuke)
for i in range(m):
    inp = list(map(int, input().split()))
    u.append(inp[0] - 1)
    v.append(inp[1] - 1)
    a.append(inp[2])
    b.append(inp[3])

uu = [[] for i in range(n)]

for i in range(m):
    uu[u[i]].append([v[i], a[i], b[i]])
    uu[v[i]].append([u[i], a[i], b[i]])

# Dijkstra's Algorithm
#   fare: 1 => yen, 2 => snuke
def cheaper(start, stop, fare):

    if (start == stop):
        return 0

    flag = [0 for i in range(n)]
    distance = [-1 for i in range(n)]
    flag[start] = 1
    distance[start] = 0
    base = start
    changeList = set([])

    for i in range(n - 1):
        # update distance
        for j in range(len(uu[base])):
                befo = distance[uu[base][j][0]]
                afte = distance[base] + uu[base][j][fare]
                if (afte < befo or befo == -1):
                    distance[uu[base][j][0]] = afte
                    changeList.add(uu[base][j][0])

        # update base
        base = list(changeList)[0]
        for j in changeList:
            if (distance[j] < distance[base]):
                base = j
        changeList.remove(base)

        # update flag (using updated base)
        flag[base] = 1

        # check break
        if (base == stop):
            break

    return distance[base]

totalFare = []
for ex in range(n): # ex: exchange
    totalFare.append(cheaper(s, ex, 1) + cheaper(ex, t, 2))

for i in range(n):
    money = 10 ** 15
    print(money - min(totalFare))
    del totalFare[0]

Submission status: Time Limit Exceeded

Solution code:
inp = list(map(int, input().split()))
n = inp[0] # the num of node
m = inp[1] # the num of edge
s = inp[2] - 1 # start
t = inp[3] - 1 # stop

u = [] # edge (smaller)
v = [] # edge (larger)
a = [] # fare (yen)
b = [] # fare (snuke)
for i in range(m):
    inp = list(map(int, input().split()))
    u.append(inp[0] - 1)
    v.append(inp[1] - 1)
    a.append(inp[2])
    b.append(inp[3])

# Dijkstra's Algorithm
def cheaper(start, stop, fare):

    if (start == stop):
        return 0

    flag = [0 for i in range(n)]
    distance = [-1 for i in range(n)]
    flag[start] = 1
    distance[start] = 0
    base = start
    changeList = set([])

    for i in range(n - 1):
        # update distance
        for j in range(m):
            if (u[j] == base):
                befo = distance[v[j]]
                afte = distance[base] + fare[j]
                if (afte < befo or befo == -1):
                    distance[v[j]] = afte
                    changeList.add(v[j])
            if (v[j] == base):
                befo = distance[u[j]]
                afte = distance[base] + fare[j]
                if (afte < befo or befo == -1):
                    distance[u[j]] = afte
                    changeList.add(u[j])

        # update base
        base = list(changeList)[0]
        for j in changeList:
            if (distance[j] < distance[base]):
                base = j
        changeList.remove(base)

        # update flag (using updated base)
        flag[base] = 1

        # check break
        if (base == stop):
            break

    return distance[base]

totalFare = []
for ex in range(n): # ex: exchange
    totalFare.append(cheaper(s, ex, a) + cheaper(ex, t, b))

for i in range(n):
    money = 10 ** 15
    print(money - min(totalFare))
    del totalFare[0]

Submission status: Time Limit Exceeded

Solution code:
inp = list(map(int, input().split()))
n = inp[0] # the num of node
m = inp[1] # the num of edge
s = inp[2] - 1 # start
t = inp[3] - 1 # stop

u = [] # edge (smaller)
v = [] # edge (larger)
a = [] # fare (yen)
b = [] # fare (snuke)
for i in range(m):
    inp = list(map(int, input().split()))
    u.append(inp[0] - 1)
    v.append(inp[1] - 1)
    a.append(inp[2])
    b.append(inp[3])

# Dijkstra's Algorithm
def cheaper(start, stop, fare):

    if (start == stop):
        return 0

    flag = [0 for i in range(n)]
    distance = [-1 for i in range(n)]
    flag[start] = 1
    distance[start] = 0
    base = start

    for i in range(n - 1):
        # update distance
        for j in range(m):
            if (u[j] == base):
                befo = distance[v[j]]
                afte = distance[base] + fare[j]
                if (afte < befo or befo == -1):
                    distance[v[j]] = afte
            if (v[j] == base):
                befo = distance[u[j]]
                afte = distance[base] + fare[j]
                if (afte < befo or befo == -1):
                    distance[u[j]] = afte

        # update base
        j = 0
        while (flag[j] == 1 or distance[j] == -1):
            j = j + 1
        base = j # flag[j] == 0 and distance[j] != -1
        j = j + 1
        while j < n:
            if (flag[j] == 0):
                if (distance[j] != -1):
                    if (distance[j] < distance[base]):
                        base = j
            j = j + 1

        # update flag (using updated base)
        flag[base] = 1

        # check break
        if (base == stop):
            break

    return distance[base]

totalFare = []
for ex in range(n): # ex: exchange
    totalFare.append(cheaper(s, ex, a) + cheaper(ex, t, b))

for i in range(n):
    money = 10 ** 15
    print(money - min(totalFare))
    del totalFare[0]

Submission status: Time Limit Exceeded

Solution code:
from heapq import *
import numpy as np

n, m, s, t = map(int, input().split())
# zero start
s -= 1
t -= 1

# 
vertices = []
for i in range(n):
    v = []
    vertices.append(v)

#print(n, m, s, t)
# load node
for k in range(m):
    u, v, a, b = map(int, input().split())
    vertices[u-1].append([v-1, a, b])
    vertices[v-1].append([u-1, a, b])


#print('-----------------')

#print(n, m, s, t)
#for i in range(n):
#    print(vertices[i].list_adj)

mode = 0
# return list of [no_adj, cost]
def get_adj(v):
    res = []
    for adj in vertices[v]:
        res.append([adj[0], adj[1+mode]])
    return res

#print('show adjcent(a)')
#for k in range(n):
#    print(get_adj(vertices[k]))

mode = 1
#print('show adjcent(b)')
#for k in range(n):
#    print(get_adj(vertices[k]))

def dijkstra(s):
    inf = int(10e20)
    dist_pre = [inf] * n
    dist = [inf] * n
    h = []
    heappush(h, (0, s))
    dist_pre[s] = 0
    while len(h) > 0:
        # pop 1 node
        dv, v = heappop(h)
        # if already decided, continue
        if dist[v] < inf:
            continue
        dist[v] = dv
        # push adjcent node
        list_adj = get_adj(v)
        for adj in list_adj:
            if dist[adj[0]] == inf:
                dnew = dv + adj[1]
                if dnew < dist_pre[adj[0]]:
                    dist_pre[adj[0]] = dnew
                    heappush(h, (dnew, adj[0]))
                pass
        pass
    return dist

mode = 0
dist_a = dijkstra(s)
mode = 1
dist_b = dijkstra(t)
#cost = []
#for k in range(len(dist_a)):
#    cost.append(dist_a[k] + dist_b[k])
cost = np.array(dist_a,dtype = 'int') + np.array(dist_b,dtype = 'int')
cost = np.array(cost, dtype='int')
#print(cost)

m0 = int(1e15)
for i in range(n):
    cost_available = cost[i:]
    print(m0-np.amin(cost_available))

Submission status: Time Limit Exceeded

Solution code:
from heapq import *
import numpy as np

class vertex:
    def __init__(self):
        self.list_adj = []  # list of [no_adjnode, a, b]

    def add_edge(self, adj):
        self.list_adj.append(adj)

n, m, s, t = map(int, input().split())
# zero start
s -= 1
t -= 1

# 
vertices = []
for i in range(n):
    v = vertex()
    vertices.append(v)

#print(n, m, s, t)
# load node
for k in range(m):
    u, v, a, b = map(int, input().split())
    vertices[u-1].add_edge([v-1, a, b])
    vertices[v-1].add_edge([u-1, a, b])


#print('-----------------')

#print(n, m, s, t)
#for i in range(n):
#    print(vertices[i].list_adj)

mode = 0
# return list of [no_adj, cost]
def get_adj(v):
    res = []
    for adj in v.list_adj:
        res.append([adj[0], adj[1+mode]])
    return res

#print('show adjcent(a)')
#for k in range(n):
#    print(get_adj(vertices[k]))

mode = 1
#print('show adjcent(b)')
#for k in range(n):
#    print(get_adj(vertices[k]))

def dijkstra(s):
    inf = int(10e20)
    dist_pre = [inf] * n
    dist = [inf] * n
    h = []
    heappush(h, (0, s))
    dist_pre[s] = 0
    while len(h) > 0:
        # pop 1 node
        dv, v = heappop(h)
        # if already decided, continue
        if dist[v] < inf:
            continue
        dist[v] = dv
        # push adjcent node
        list_adj = get_adj(vertices[v])
        for adj in list_adj:
            if dist[adj[0]] == inf:
                dnew = dv + adj[1]
                if dnew < dist_pre[adj[0]]:
                    dist_pre[adj[0]] = dnew
                    heappush(h, (dnew, adj[0]))
                pass
        pass
    return dist

mode = 0
dist_a = dijkstra(s)
mode = 1
dist_b = dijkstra(t)
#cost = []
#for k in range(len(dist_a)):
#    cost.append(dist_a[k] + dist_b[k])
cost = np.array(dist_a,dtype = 'int') + np.array(dist_b,dtype = 'int')
cost = np.array(cost, dtype='int')
#print(cost)

m0 = int(1e15)
for i in range(n):
    cost_available = cost[i:]
    print(m0-np.amin(cost_available))

Submission status: Time Limit Exceeded

Solution code:
from heapq import *
import numpy as np

class vertex:
    def __init__(self):
        self.list_adj = []  # list of [no_adjnode, a, b]

    def add_edge(self, adj):
        self.list_adj.append(adj)

n, m, s, t = map(int, input().split())
# zero start
s -= 1
t -= 1

# 
vertices = []
for i in range(n):
    v = vertex()
    vertices.append(v)

#print(n, m, s, t)
# load node
for k in range(m):
    u, v, a, b = map(int, input().split())
    vertices[u-1].add_edge([v-1, a, b])
    vertices[v-1].add_edge([u-1, a, b])


#print('-----------------')

#print(n, m, s, t)
#for i in range(n):
#    print(vertices[i].list_adj)

mode = 0
# return list of [no_adj, cost]
def get_adj(v):
    res = []
    for adj in v.list_adj:
        res.append([adj[0], adj[1+mode]])
    return res

#print('show adjcent(a)')
#for k in range(n):
#    print(get_adj(vertices[k]))

mode = 1
#print('show adjcent(b)')
#for k in range(n):
#    print(get_adj(vertices[k]))

def dijkstra(s):
    inf = int(10e20)
    dist = [inf] * n
    h = []
    heappush(h, (0, s))
    while len(h) > 0:
        # pop 1 node
        dv, v = heappop(h)
        # if already decided, continue
        if dist[v] < inf:
            continue
        dist[v] = dv
        # push adjcent node
        list_adj = get_adj(vertices[v])
        for adj in list_adj:
            if dist[adj[0]] == inf:
                heappush(h, (dv + adj[1], adj[0]))
                pass
        pass
    return dist

mode = 0
dist_a = dijkstra(s)
mode = 1
dist_b = dijkstra(t)
#cost = []
#for k in range(len(dist_a)):
#    cost.append(dist_a[k] + dist_b[k])
cost = np.array(dist_a,dtype = 'int') + np.array(dist_b,dtype = 'int')
cost = np.array(cost, dtype='int')
#print(cost)

m0 = int(1e15)
for i in range(n):
    cost_available = cost[i:]
    print(m0-np.amin(cost_available))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
INF = 10**20

n,m,s,t = map(int,input().split())
u = np.zeros(m)
v = np.zeros(m)
a = np.zeros(m)
b = np.zeros(m)

i = 0
while(i < m):
    u[i],v[i],a[i],b[i] = map(int,input().split())
    i += 1

#s to i
dist = np.ones(n)*INF
dist[s-1] = 0
Q1 = np.arange(n).reshape((1,n))
Q2 = np.zeros(n).reshape((1,n))
Q = np.concatenate((Q1,Q2),axis=0)

while(np.sum(Q[1,:] == -1) < n):
    dist_masked = dist[Q[1] == 0]
    Q_masked = Q[:,Q[1] == 0]
    dijk_u = int(Q_masked[0,np.argmin(dist_masked)])
    Q[1,dijk_u] = -1
    queue = list(np.intersect1d(np.concatenate((u[np.where(v == dijk_u + 1)],v[np.where(u == dijk_u + 1)]),axis=0) -1,Q[0,np.where(Q[1]==0)]))
    for dijk_v in queue:
        small = np.min(np.array([dijk_u, dijk_v]))
        big = np.max(np.array([dijk_u, dijk_v]))
        raw = np.intersect1d(np.where(u == small+1)[0],np.where(v == big+1)[0])[0]
        dijk_v = int(dijk_v)
        if (dist[dijk_v] > dist[dijk_u] + a[raw]):
            dist[dijk_v] = dist[dijk_u] + a[raw]


yens = dist.copy()

#t to i
dist = np.ones(n)*INF
dist[t-1] = 0
Q1 = np.arange(n).reshape((1,n))
Q2 = np.zeros(n).reshape((1,n))
Q = np.concatenate((Q1,Q2),axis=0)

while(np.sum(Q[1,:] == -1) < n):
    dist_masked = dist[Q[1] == 0]
    Q_masked = Q[:,Q[1] == 0]
    dijk_u = int(Q_masked[0,np.argmin(dist_masked)])
    Q[1,dijk_u] = -1
    queue = list(np.intersect1d(np.concatenate((u[np.where(v == dijk_u + 1)],v[np.where(u == dijk_u + 1)]),axis=0) -1,Q[0,np.where(Q[1]==0)]))
    for dijk_v in queue:
        small = np.min(np.array([dijk_u, dijk_v]))
        big = np.max(np.array([dijk_u, dijk_v]))
        raw = np.intersect1d(np.where(u == small+1)[0],np.where(v == big+1)[0])[0]
        dijk_v = int(dijk_v)
        if (dist[dijk_v] > dist[dijk_u] + b[raw]):
            dist[dijk_v] = dist[dijk_u] + b[raw]

snuuks = dist.copy()

total_costs = yens + snuuks

for i in range(n):
    print(int(10**15 - np.min(total_costs[i::])))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
INF = 10**20

n,m,s,t = map(int,input().split())
u = np.zeros(m)
v = np.zeros(m)
a = np.zeros(m)
b = np.zeros(m)

i = 0
while(i < m):
    u[i],v[i],a[i],b[i] = map(int,input().split())
    i += 1

#s to i
dist = np.ones(n)*INF
dist[s-1] = 0
Q1 = np.arange(n).reshape((1,n))
Q2 = np.zeros(n).reshape((1,n))
Q = np.concatenate((Q1,Q2),axis=0)

while(np.sum(Q[1,:] == -1) < n):
    dist_masked = dist[Q[1] == 0]
    Q_masked = Q[:,Q[1] == 0]
    dijk_u = int(Q_masked[0,np.argmin(dist_masked)])
    Q[1,dijk_u] = -1
    queue = list(np.intersect1d(np.concatenate((u[np.where(v == dijk_u + 1)],v[np.where(u == dijk_u + 1)]),axis=0) -1,Q[0,np.where(Q[1]==0)]))
    for dijk_v in queue:
        small = np.min(np.array([dijk_u, dijk_v]))
        big = np.max(np.array([dijk_u, dijk_v]))
        raw = np.intersect1d(np.where(u == small+1)[0],np.where(v == big+1)[0])[0]
        dijk_v = int(dijk_v)
        if (dist[dijk_v] > dist[dijk_u] + a[raw]):
            dist[dijk_v] = dist[dijk_u] + a[raw]


yens = dist.copy()

#t to i
dist = np.ones(n)*INF
dist[t-1] = 0
Q1 = np.arange(n).reshape((1,n))
Q2 = np.zeros(n).reshape((1,n))
Q = np.concatenate((Q1,Q2),axis=0)

while(np.sum(Q[1,:] == -1) < n):
    dist_masked = dist[Q[1] == 0]
    Q_masked = Q[:,Q[1] == 0]
    Q[1,dijk_u] = -1
    queue = list(np.intersect1d(np.concatenate((u[np.where(v == dijk_u + 1)],v[np.where(u == dijk_u + 1)]),axis=0) -1,Q[0,np.where(Q[1]==0)]))
    for dijk_v in queue:
        small = np.min(np.array([dijk_u, dijk_v]))
        big = np.max(np.array([dijk_u, dijk_v]))
        raw = np.intersect1d(np.where(u == small+1)[0],np.where(v == big+1)[0])[0]
        dijk_v = int(dijk_v)
        if (dist[dijk_v] > dist[dijk_u] + b[raw]):
            dist[dijk_v] = dist[dijk_u] + b[raw]

snuuks = dist.copy()

total_costs = yens + snuuks

for i in range(n):
    print(int(10**15 - np.min(total_costs[i::])))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
INF = 10**20

n,m,s,t = map(int,input().split())
u = np.zeros(m)
v = np.zeros(m)
a = np.zeros(m)
b = np.zeros(m)

i = 0
while(i < m):
    u[i],v[i],a[i],b[i] = map(int,input().split())
    i += 1

#s to i
dist = np.ones(n)*INF
dist[s-1] = 0
Q1 = np.arange(n).reshape((1,n))
Q2 = np.zeros(n).reshape((1,n))
Q = np.concatenate((Q1,Q2),axis=0)

while(np.sum(Q[1,:] == -1) < n):
    dist_argsorted = np.argsort(dist)
    sorted_Q = Q[:,dist_argsorted]
    dijk_u = int(sorted_Q[0,np.argmax(sorted_Q[1])])
    Q[1,dijk_u] = -1
    queue = list(np.intersect1d(np.concatenate((u[np.where(v == dijk_u + 1)],v[np.where(u == dijk_u + 1)]),axis=0) -1,Q[0,np.where(Q[1]==0)]))
    for dijk_v in queue:
        small = np.min(np.array([dijk_u, dijk_v]))
        big = np.max(np.array([dijk_u, dijk_v]))
        raw = np.intersect1d(np.where(u == small+1)[0],np.where(v == big+1)[0])[0]
        dijk_v = int(dijk_v)
        if (dist[dijk_v] > dist[dijk_u] + a[raw]):
            dist[dijk_v] = dist[dijk_u] + a[raw]


yens = dist.copy()

#t to i
dist = np.ones(n)*INF
dist[t-1] = 0
Q1 = np.arange(n).reshape((1,n))
Q2 = np.zeros(n).reshape((1,n))
Q = np.concatenate((Q1,Q2),axis=0)

while(np.sum(Q[1,:] == -1) < n):
    dist_argsorted = np.argsort(dist)
    sorted_Q = Q[:,dist_argsorted]
    dijk_u = int(sorted_Q[0,np.argmax(sorted_Q[1])])
    Q[1,dijk_u] = -1
    queue = list(
        np.intersect1d(np.concatenate((u[np.where(v == dijk_u + 1)], v[np.where(u == dijk_u + 1)]), axis=0) - 1,
                       Q[0, np.where(Q[1] == 0)]))
    for dijk_v in queue:
        small = np.min(np.array([dijk_u, dijk_v]))
        big = np.max(np.array([dijk_u, dijk_v]))
        raw = np.intersect1d(np.where(u == small+1)[0],np.where(v == big+1)[0])[0]
        dijk_v = int(dijk_v)
        if (dist[dijk_v] > dist[dijk_u] + b[raw]):
            dist[dijk_v] = dist[dijk_u] + b[raw]

snuuks = dist.copy()

total_costs = yens + snuuks

for i in range(n):
    print(int(10**15 - np.min(total_costs[i::])))