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

Solution (Python):
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
            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)])

Solution (Python):
from heapq import heappush, heappop


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


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


if __name__ == "__main__":
    main()

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

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

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

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

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

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

for i in range(m):
    graph[data[i][0]].append([data[i][1], data[i][2], data[i][3]])
    graph[data[i][1]].append([data[i][0], data[i][2], data[i][3]])
city_yen_cost = [INF] * (n+1)
city_yen_cost[s] = 0
queue = []

heapq.heappush(queue, [0, s])
count = 0
while count != n:
    now = heapq.heappop(queue)
    if check[now[1]]:
        continue
    else:
        count += 1
        check[now[1]] = True
        for path in graph[now[1]]:
            if not check[path[0]]:
                if city_yen_cost[now[1]]+path[1] < city_yen_cost[path[0]]:
                    city_yen_cost[path[0]] = city_yen_cost[now[1]]+path[1]
                    heapq.heappush(queue, [city_yen_cost[now[1]]+path[1], path[0]])




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


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

Solution (Python):
import heapq
import sys

import numpy as np

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


def dijkstra(graph, start):
    """
    :param list of (list of (int, int)) graph:
    :param int start:
    :return:
    """
    dist = [INF for _ in range(len(graph))]
    dist[start] = 0
    heap = []
    heapq.heappush(heap, (0, start))

    while len(heap) > 0:
        w, v = heapq.heappop(heap)
        if w > dist[v]:
            continue
        for u, dw in graph[v]:
            if w + dw < dist[u]:
                dist[u] = w + dw
                heapq.heappush(heap, (w + dw, u))
    return dist


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


yen_mins = np.array(dijkstra(graph_yen, start=S))

snuke_mins = np.array(dijkstra(graph_snuuk, start=T))


totals = 10 ** 15 - (yen_mins + snuke_mins)[1:]

for a in np.maximum.accumulate(totals[::-1])[::-1]:
    print(int(a))

Solution (Python):
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 = []

heapq.heappush(queue, [0, s])
count = 0
while len(queue) > 0:
    if count == n:
        break
    now = heapq.heappop(queue)
    if check[now[1]]:
        continue
    else:
        count += 1
        check[now[1]] = True
        for path in graph[now[1]]:
            if city_yen_cost[now[1]]+path[1] < city_yen_cost[path[0]]:
                city_yen_cost[path[0]] = min(city_yen_cost[now[1]]+path[1], city_yen_cost[path[0]])
                if not check[path[0]]:
                    heapq.heappush(queue, [city_yen_cost[now[1]]+path[1], path[0]])




check = [False 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])
count = 0
while len(queue) > 0:
    if count == n:
        break
    now = heapq.heappop(queue)
    if check[now[1]]:
        continue
    else:
        count += 1
        check[now[1]] = True
        for path in graph[now[1]]:
            if city_snuke_cost[now[1]]+path[2] < city_snuke_cost[path[0]]:
                city_snuke_cost[path[0]] = min(city_snuke_cost[now[1]]+path[2], city_snuke_cost[path[0]])
                if not check[path[0]]:
                    heapq.heappush(queue, [city_snuke_cost[now[1]]+path[2], path[0]])


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

Solution (Python):
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 = [10**30 for _ in range(self.size)]
        self.dist[self.start] = 0
        
        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
                    
                    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])

Solution (Python):
import heapq, sys

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

    def path(self, goal):
        self.path = [goal]
        while self.path[-1] != self.start:
            self.path.append(self.prev[self.path[-1]])
        return self.path[::-1]

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

Solution (Python):
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 = []

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]]:
            if city_yen_cost[now[1]]+path[1] < city_yen_cost[path[0]]:
                city_yen_cost[path[0]] = min(city_yen_cost[now[1]]+path[1], city_yen_cost[path[0]])
                if not check[path[0]]:
                    heapq.heappush(queue, [city_yen_cost[now[1]]+path[1], path[0]])




check = [False 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]]:
            if city_snuke_cost[now[1]]+path[2] < city_snuke_cost[path[0]]:
                city_snuke_cost[path[0]] = min(city_snuke_cost[now[1]]+path[2], city_snuke_cost[path[0]])
                if not check[path[0]]:
                    heapq.heappush(queue, [city_snuke_cost[now[1]]+path[2], path[0]])


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

Solution (Python):
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].items():
            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][v] = a
    route_y[v][u] = a
    route_s[u][v] = b
    route_s[v][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)]

Solution (Python):
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)])

Solution (Python):
from heapq import *

n,m,s,t=map(int,input().split())
s-=1
t-=1
INF=10**19
Gyen=[[]for i in range(n)]
Gsnu=[[]for i in range(n)]
for i in range(m):
  u,v,a,b=map(int,input().split())
  u-=1
  v-=1
  Gyen[u].append([v,a])
  Gyen[v].append([u,a])
  Gsnu[u].append([v,b])
  Gsnu[v].append([u,b])

def dijkstra(G,s):
  d=[INF]*n
  d[s]=0
  visited={s}
  que=[(0,s)]
  while(que):
    p=heappop(que)
    v=p[1]
    visited.add(v)
    for node,cost in G[v]:
      if (node not in visited) and d[node]>d[v]+cost:
        d[node]=d[v]+cost
        heappush(que,(d[node],node))
  return d

yen=dijkstra(Gyen,s)
snu=dijkstra(Gsnu,t)
ans=10**15
ans_list=[]
for i in reversed(range(n)):
  ans=min(ans,yen[i]+snu[i])
  ans_list.append(10**15-ans)
print(*ans_list[::-1], sep='\n')

Solution (Python):
from heapq import *

n,m,s,t=map(int,input().split())
s-=1
t-=1
INF=float("inf")
Gyen=[[]for i in range(n)]
Gsnu=[[]for i in range(n)]
for i in range(m):
  u,v,a,b=map(int,input().split())
  u-=1
  v-=1
  Gyen[u].append([v,a])
  Gyen[v].append([u,a])
  Gsnu[u].append([v,b])
  Gsnu[v].append([u,b])

def dijkstra(G,s):
  d=[INF]*n
  d[s]=0
  visited={s}
  que=[(0,s)]
  while(que):
    p=heappop(que)
    v=p[1]
    visited.add(v)
    for node,cost in G[v]:
      if (node not in visited) and d[node]>d[v]+cost:
        d[node]=d[v]+cost
        heappush(que,(d[node],node))
  return d

yen=dijkstra(Gyen,s)
snu=dijkstra(Gsnu,t)
ans=10**15
ans_list=[]
for i in reversed(range(n)):
  ans=min(ans,yen[i]+snu[i])
  ans_list.append(10**15-ans)
for i in ans_list[::-1]:
  print(i)

Solution (Python):
from heapq import *

n,m,s,t=map(int,input().split())
s-=1
t-=1
INF=10**19
Gyen=[[]for i in range(n)]
Gsnu=[[]for i in range(n)]
for i in range(m):
  u,v,a,b=map(int,input().split())
  u-=1
  v-=1
  Gyen[u].append([v,a])
  Gyen[v].append([u,a])
  Gsnu[u].append([v,b])
  Gsnu[v].append([u,b])

def dijkstra(G,s):
  d=[INF]*n
  d[s]=0
  visited={s}
  que=[(0,s)]
  while(que):
    p=heappop(que)
    v=p[1]
    visited.add(v)
    if d[v]<p[0]:
      continue

    for node,cost in G[v]:
      if (node not in visited) and d[node]>d[v]+cost:
        d[node]=d[v]+cost
        heappush(que,(d[node],node))
  return d

yen=dijkstra(Gyen,s)
snu=dijkstra(Gsnu,t)
ans=10**15
ans_list=[]
for i in reversed(range(n)):
  ans=min(ans,yen[i]+snu[i])
  ans_list.append(10**15-ans)
print(*ans_list[::-1], sep='\n')

Solution (Python):
from heapq import *

n,m,s,t=map(int,input().split())
s-=1
t-=1
INF=float("inf")
Gyen=[[]for i in range(n)]
Gsnu=[[]for i in range(n)]
for i in range(m):
  u,v,a,b=map(int,input().split())
  u-=1
  v-=1
  Gyen[u].append([v,a])
  Gyen[v].append([u,a])
  Gsnu[u].append([v,b])
  Gsnu[v].append([u,b])

def dijkstra(G,s):
  d=[INF]*n
  d[s]=0
  visited={s}
  que=[(0,s)]
  while(que):
    p=heappop(que)
    v=p[1]
    visited.add(v)
    for node,cost in G[v]:
      if (node not in visited) and d[node]>d[v]+cost:
        d[node]=d[v]+cost
        heappush(que,(d[node],node))
  return d

yen=dijkstra(Gyen,s)
snu=dijkstra(Gsnu,t)
ans=10**15
ans_list=[]
for i in reversed(range(n)):
  ans=min(ans,yen[i]+snu[i])
  ans_list.append(10**15-ans)
print(*ans_list[::-1], sep='\n')

Solution (Python):
from heapq import *

n,m,s,t=map(int,input().split())
s-=1
t-=1
INF=float("inf")
Gyen=[[]for i in range(n)]
Gsnu=[[]for i in range(n)]
for i in range(m):
  u,v,a,b=map(int,input().split())
  u-=1
  v-=1
  Gyen[u].append([v,a])
  Gyen[v].append([u,a])
  Gsnu[u].append([v,b])
  Gsnu[v].append([u,b])

def dijkstra(G,s):
  d=[INF]*n
  d[s]=0
  visited={s}
  que=[(0,s)]
  while(que):
    p=heappop(que)
    v=p[1]
    visited.add(v)
    for node,cost in G[v]:
      if (node not in visited) and d[node]>d[v]+cost:
        d[node]=d[v]+cost
        heappush(que,(d[node],node))
  return d

yen=dijkstra(Gyen,s)
snu=dijkstra(Gsnu,t)
ans=10**15
ans_list=[]
for i in reversed(range(n)):
  ans=min(ans,yen[i]+snu[i])
  ans_list.append(10**15-ans)
print(*ans_list[::-1], sep='\n')

Solution (Python):
from heapq import *

n,m,s,t=map(int,input().split())
s-=1
t-=1
INF=10**19
Gyen=[[]for i in range(n)]
Gsnu=[[]for i in range(n)]
for i in range(m):
  u,v,a,b=map(int,input().split())
  u-=1
  v-=1
  Gyen[u].append([v,a])
  Gyen[v].append([u,a])
  Gsnu[u].append([v,b])
  Gsnu[v].append([u,b])

def dijkstra(G,s):
  que=[]
  d=[INF]*n
  d[s]=0
  visited={s}
  heappush(que,[0,s])
  while(que):
    p=heappop(que)
    v=p[1]
    visited.add(v)
    for node,cost in G[v]:
      if (node not in visited) and d[node]>d[v]+cost:
        d[node]=d[v]+cost
        heappush(que,[d[node],node])
  return d

yen=dijkstra(Gyen,s)
snu=dijkstra(Gsnu,t)
ans=10**15
ans_list=[]
for i in reversed(range(n)):
  ans=min(ans,yen[i]+snu[i])
  ans_list.append(10**15-ans)
print(*ans_list[::-1], sep='\n')

Solution (Python):
from heapq import heappop,heappush
n,m,s,t = (int(i) for i in input().split())
x,y = [[] for i in range(n+1)],[[] for i in range(n+1)]
for i in range(m):
    u,v,a,b = (int(i) for i in input().split())
    x[u].append((v,a))
    x[v].append((u,a))
    y[u].append((v,b))
    y[v].append((u,b))
def dijkstra(n,s,adj):
	dis = [float("inf") for i in range(n+1)]
	dis[s],q = 0,[[0,s]]
	while q:
		num2,num = heappop(q)
		for i,j in adj[num]:
			if dis[i]>num2+j:
				dis[i] = num2+j
				heappush(q,[dis[i],i])
	return dis
X,Y,z = dijkstra(n,s,x),dijkstra(n,t,y),[]
for i in range(1,n+1): z.append((X[i]+Y[i],i))
z,k = sorted(z),0
for i in range(n):
    while z[k][1]<=i: k+=1
    print(10**15-z[k][0])

Solution (Python):
import sys
input=sys.stdin.readline
from collections import defaultdict
from heapq import heappush,heappop
from itertools import accumulate
N,m,s,t=map(int,input().split())
en=[set() for i in range(N+1)]
sneek=[set() for i in range(N+1)]
for i in range(m):
    u,v,a,b=map(int,input().split())
    en[u]|={(a,v)}
    en[v]|={(a,u)}
    sneek[u]|={(b,v)}
    sneek[v]|={(b,u)}
inf=float('inf')
def makedist(first,branch):
    dist=[inf]*(N+1)
    dist[first]=0
    check=[(0,first)]
    while len(check)>0:
        d,now=heappop(check)
        if dist[now]<d:
            continue
        for lenth,nex in branch[now]:
            if d+lenth<dist[nex]:
                dist[nex]=d+lenth
                heappush(check,(d+lenth,nex))
    return dist
disten=makedist(s,en)
distsneek=makedist(t,sneek)
ans=list(accumulate([y+s for y,s in zip(disten,distsneek)][::-1],min))
big=10**15
[print(big-ans[i]) for i in range(N-1,-1,-1)]

Solution (Python):
import sys
input=sys.stdin.readline
from collections import defaultdict
from heapq import heappush,heappop
from itertools import accumulate
N,m,s,t=map(int,input().split())
en=defaultdict(set)
sneek=defaultdict(set)
for i in range(m):
    u,v,a,b=map(int,input().split())
    en[u]|={(a,v)}
    en[v]|={(a,u)}
    sneek[u]|={(b,v)}
    sneek[v]|={(b,u)}
inf=float('inf')
def makedist(first,branch):
    dist=[inf]*(N+1)
    dist[first]=0
    check=[(0,first)]
    while len(check)>0:
        d,now=heappop(check)
        if dist[now]<d:
            continue
        for lenth,nex in branch[now]:
            if d+lenth<dist[nex]:
                dist[nex]=d+lenth
                heappush(check,(d+lenth,nex))
    return dist
disten=makedist(s,en)
distsneek=makedist(t,sneek)
ans=list(accumulate([y+s for y,s in zip(disten,distsneek)][::-1],min))
big=10**15
for i in range(N-1,-1,-1):
    print(big-ans[i])

Solution (Python):
import sys
input=sys.stdin.readline
from collections import defaultdict
from heapq import heappush,heappop
from itertools import accumulate
N,m,s,t=map(int,input().split())
en=defaultdict(set)
sneek=defaultdict(set)
for i in range(m):
    u,v,a,b=map(int,input().split())
    en[u]|={(a,v)}
    en[v]|={(a,u)}
    sneek[u]|={(b,v)}
    sneek[v]|={(b,u)}
inf=float('inf')
def makedist(first,branch):
    dist=[inf]*(N+1)
    dist[first]=0
    check=[(0,first)]
    while len(check)>0:
        d,now=heappop(check)
        if dist[now]<d:
            continue
        for lenth,nex in branch[now]:
            if d+lenth<dist[nex]:
                dist[nex]=d+lenth
                heappush(check,(d+lenth,nex))
    return dist
disten=makedist(s,en)
distsneek=makedist(t,sneek)
x=[y+s for y,s in zip(disten,distsneek)][::-1]
ans=list(accumulate(x,min))
big=10**15
[print(big-ans[i]) for i in range(N-1,-1,-1)]

Solution (Python):
import sys
input=sys.stdin.readline
from collections import defaultdict
from heapq import heappush,heappop
N,m,s,t=map(int,input().split())
en=defaultdict(set)
sneek=defaultdict(set)
for i in range(m):
    u,v,a,b=map(int,input().split())
    en[u]|={(a,v)}
    en[v]|={(a,u)}
    sneek[u]|={(b,v)}
    sneek[v]|={(b,u)}
inf=float('inf')
def makedist(first,branch):
    dist=[inf]*(N+1)
    dist[first]=0
    check=[(0,first)]
    while len(check)>0:
        d,now=heappop(check)
        if dist[now]<d:
            continue
        for lenth,nex in branch[now]:
            if d+lenth<dist[nex]:
                dist[nex]=d+lenth
                heappush(check,(d+lenth,nex))
    return dist
disten=makedist(s,en)
distsneek=makedist(t,sneek)
ans=[0]*(N+1)
ans[N]=disten[N]+distsneek[N]
for i in range(N-1,0,-1):
    ans[i]=min(ans[i+1],disten[i]+distsneek[i])
big=10**15
for i in range(1,N+1):
    print(big-ans[i])

Solution (Python):
import heapq

INF = 1 << 60
MOD = 1000000007

def dijkstra(s):
    
    global d, G, n
    d = [INF] * n
    d[s] = 0
    que = [(0, s)]
    heapq.heapify(que)

    while len(que) > 0:
        
        p = heapq.heappop(que)
        v = p[1]
        
        if d[v] < p[0]:
            continue
        for e in G[v]:
            if d[e[0]] > d[v] + e[1]:
                d[e[0]] = d[v] + e[1]
                heapq.heappush(que, (d[e[0]], e[0]))

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

d = [0] * n
dijkstra(s - 1)

ds = d[:]
G = []
for i in range(n):
    G.append([])
for i in range(m):
    G[u[i] - 1].append((v[i] - 1, b[i]))
    G[v[i] - 1].append((u[i] - 1, b[i]))
d = [0] * n
dijkstra(t - 1)
dst = []
for i in range(n):
    dst.append((d[i] + ds[i], i))
dst.sort()
change = 0
money = 1000000000000000
for i in range(n):
    while dst[change][1] < i:
        change += 1
    print(money - dst[change][0])

Solution (Python):
import heapq

INF = 1 << 60
MOD = 1000000007

def dijkstra(s):
    
    global d, G, n
    d = [INF] * n
    d[s] = 0
    que = [(0, s)]
    heapq.heapify(que)

    while len(que) > 0:
        
        p = heapq.heappop(que)
        v = p[1]
        
        if d[v] < p[0]:
            continue
        for e in G[v]:
            if d[e[0]] > d[v] + e[1]:
                d[e[0]] = d[v] + e[1]
                heapq.heappush(que, (d[e[0]], e[0]))

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

d = [0] * n
dijkstra(s - 1)

ds = d[:]
G = []
for i in range(n):
    G.append([])
for i in range(m):
    G[u[i] - 1].append((v[i] - 1, b[i]))
    G[v[i] - 1].append((u[i] - 1, b[i]))
d = [0] * n
dijkstra(t - 1)
dst = []
for i in range(n):
    dst.append((d[i] + ds[i], i))
dst.sort()
change = 0
money = 1000000000000000
for i in range(n):
    while dst[change][1] < i:
        change += 1
    print(money - dst[change][0])