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: Accepted

Solution code:
from heapq import heappop, heappush

def inpl(): return list(map(int, input().split()))

N, M, s, t = inpl()
Gs = [[] for _ in range(N+1)]
Gt = [[] for _ in range(N+1)]

for i in range(M):
    u, v, a, b = inpl()
    Gs[u].append([v, a])
    Gs[v].append([u, a])
    Gt[u].append([v, b])
    Gt[v].append([u, b])

S = [10**15]*(N+1)
T = [10**15]*(N+1)
S[s] = 0
T[t] = 0

Q = []
heappush(Q, [0, s])

while Q:
    pc, p = heappop(Q)
    if pc > S[p]:
        continue
    for q, dc in Gs[p]:
        if S[q] > pc + dc:
            S[q] = pc + dc
            heappush(Q, [S[q], q])

Q = []
heappush(Q, [0, t])

while Q:
    pc, p = heappop(Q)
    if pc > T[p]:
        continue
    for q, dc in Gt[p]:
        if T[q] > pc + dc:
            T[q] = pc + dc
            heappush(Q, [T[q], q])

tmp = 0
X = 10**15
ans = [0 for _ in range(N)]
for i in range(N, 0, -1):
    t = X - T[i] - S[i]
    tmp = max(tmp, t)
    ans[i-1] = tmp*1

print(*ans, sep="\n")

Submission status: Accepted

Solution code:
#ダイクストラ
import heapq
n,m,s,t = map(int,input().split())
UVAB = [list(map(int,input().split())) for _ in [0]*m]

Ea = [{} for _ in [0]*n]
Eb = [{} for _ in [0]*n]
for u,v,a,b in UVAB:
    Ea[u-1][v-1] = a
    Ea[v-1][u-1] = a
    Eb[u-1][v-1] = b
    Eb[v-1][u-1] = b

#import heapq
def Dijkstra(N,E,start):
    """
    N: ノード数
    E: 辺(dictのlist)
    start:開始ノード番号(0-indexed)
    """
    d = [-1]*N
    q = [(0,start-1)]
    while q:
        ci,i = heapq.heappop(q)
        if d[i]!= -1:continue
        d[i] = ci
        for j,cj in E[i].items():
            if d[j]!=-1:continue
            heapq.heappush(q,(ci+cj,j))
    return d

da = Dijkstra(n,Ea,s)
db = Dijkstra(n,Eb,t)

C = [a+b for a,b in zip(da,db)]

ans = [C[-1]]
for c in C[-2::-1]:
    ans.append(min(ans[-1],c))

for out in ans[::-1]:
    print(10**15-out)

Submission status: Accepted

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


def dijkstra(adj, start):
    # adj: [[to, cost] * vertices], 0th index must be empty
    dist = [float('inf')] * len(adj)
    dist[start] = 0
    q = []
    heapq.heappush(q, [0, start])
    while q:
        min_dist, v_from = heapq.heappop(q)
        if min_dist > dist[v_from]:
            continue
        v_tos = adj[v_from]
        for v_to in v_tos:
            if min_dist + v_to[1] < dist[v_to[0]]:
                dist[v_to[0]] = min_dist + v_to[1]
                heapq.heappush(q, [dist[v_to[0]], v_to[0]])
    return dist


n, m, start, goal = map(int, input().split())
adj_yen = [[] for _ in range(n+1)]
adj_snuuk = [[] for _ in range(n+1)]
for _ in range(m):
    u, v, a, b = map(int, input().split())
    adj_yen[u].append([v, a])
    adj_yen[v].append([u, a])
    adj_snuuk[u].append([v, b])
    adj_snuuk[v].append([u, b])

min_dist_yen = dijkstra(adj_yen, start)
min_dist_snuuk = dijkstra(adj_snuuk, goal)
lose = [float('inf')] * n
for i in range(1, n+1):
    lose[-i] = min(lose[-i+1], min_dist_yen[n-i+1] + min_dist_snuuk[n-i+1])

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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

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

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

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

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

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

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

    return d

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

ans = []
tmp = float('inf')
for i in reversed(range(1, n + 1)):
    tmp = min(tmp, d1[i] + d2[i])
    ans.append(10 ** 15 - tmp)
    
print(*ans[::-1], sep='\n')

Submission status: Accepted

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

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

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

    return d

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

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

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

Submission status: Accepted

Solution code:
from heapq import *
n,m,s,t = map(int,input().split())
uvab = [list(map(int,input().split())) for _ in [0]*m]
connect = [[] for _ in [0]*n]
for u,v,a,b in uvab:
    connect[u-1].append((v-1,a,b))
    connect[v-1].append((u-1,a,b))
da = [-1]*n
db = [-1]*n
q = [(0,s-1)]
while q:
    c,i = heappop(q)
    if da[i]!=-1:continue
    da[i] = c
    for j,a,_ in connect[i]:
        if da[j]==-1:
            heappush(q,(c+a,j))
q = [(0,t-1)]
while q:
    c,i = heappop(q)
    if db[i]!=-1:continue
    db[i] = c
    for j,_,b in connect[i]:
        if db[j]==-1:
            heappush(q,(c+b,j))
ans = [10**15]
for ca,cb in zip(da[::-1],db[::-1]):
    ans.append(min(ans[-1],ca+cb))
for out in ans[:0:-1]:
    print(10**15-out)

Submission status: Accepted

Solution code:
import sys
input=sys.stdin.readline
N,M,S,T=map(int,input().split())
S-=1
T-=1
from collections import defaultdict
branch=defaultdict(dict)
for i in range(M):
    u,v,a,b=map(int,input().split())
    u-=1
    v-=1
    branch[u][v]=(a,b)
    branch[v][u]=(a,b)
def dijk(s,x):
    dist=[float('inf')]*N
    dist[s]=0
    check={s}
    while len(check)>0:
        now=check.pop()
        for nex,d in branch[now].items():
            if dist[now]+d[x]<dist[nex]:
                check|={nex}
                dist[nex]=dist[now]+d[x]
    return dist
en=dijk(S,0)
snuuk=dijk(T,1)
ans=[0]*N
ans[N-1]=en[N-1]+snuuk[N-1]
for i in range(N-2,-1,-1):
    ans[i]=min(ans[i+1],en[i]+snuuk[i])
for i in range(N):
    print(10**15-ans[i])

Submission status: Accepted

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


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

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

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

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


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

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

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

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

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

Submission status: Accepted

Solution code:
import heapq
INF = 10**15

n, m, s, t = [int(item) for item in input().split()]
yen = [[] for _ in range(n)]
snk = [[] for _ in range(n)]

class Dijkstra:
    def __init__(self, adj):
        self.adj = adj
        self.num = len(adj)
        self.dist = [INF] * self.num
        self.prev = [INF] * self.num
        self.q = []

    def calc(self, start):
        self.dist[start] = 0
        heapq.heappush(self.q, (0, start))
        while len(self.q) != 0:
            prov_cost, src = heapq.heappop(self.q)
            if self.dist[src] < prov_cost:
                continue
            for dest, cost in self.adj[src]:
                if self.dist[dest] > self.dist[src] + cost:
                    self.dist[dest] = self.dist[src] + cost
                    heapq.heappush(self.q, (self.dist[dest], dest))
                    self.prev[dest] = src
        return self.dist

for i in range(m):
    u, v, a, b = [int(item) for item in input().split()]
    u-=1; v-=1
    yen[u].append([v, a])
    yen[v].append([u, a])
    snk[u].append([v, b])
    snk[v].append([u, b])

yen_dijk = Dijkstra(yen)
snk_dijk = Dijkstra(snk)

yen_dist = yen_dijk.calc(s-1)
snk_dist = snk_dijk.calc(t-1)
 
ans = [0] * n
min_cost = 10**15
i = n-1
for yy, ss in zip(yen_dist[::-1], snk_dist[::-1]):
    min_cost = min(min_cost, yy + ss)
    ans[i] = min_cost
    i -= 1

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

Submission status: Accepted

Solution code:
from heapq import heappop, heappush
 
 
def dijkstra(costs, s, i):
    q = [(0, s)]
    while q:
        c, v = heappop(q)
        if costs[v] >= 0:
            continue
        costs[v] = c
        for link in links[v]:
            u = link[0]
            if costs[u] == -1:
                heappush(q, (c + link[i], u))
 
 
n, m, s, t = map(int, input().split())
links = [[] for _ in range(n+1)]
for _ in range(m):
    u, v, a, b = map(int, input().split())
    links[u] += (v, a, b),
    links[v] += (u, a, b),

yen_costs = [-1] * (n+1)
snk_costs = [-1] * (n+1)
dijkstra(yen_costs, s, 1)
dijkstra(snk_costs, t, 2)
ansl = []
ans = 0

for i in range(1, n+1):
    cost = yen_costs[-i]+snk_costs[-i]
    m = 10**15 - cost
    if ans < m:
        ans = m
    ansl.append(ans)
for _ in range(n):
    print(ansl.pop())

Submission status: Accepted

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

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

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

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

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

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

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

d.reverse()
ans = []
m = float('inf')
for i in range(n):
    m = min(m,d[i])
    ans.append(10**15-m)
ans.reverse()

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

Submission status: Accepted

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


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


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


class Dijkstra():
    def __init__(self, graph, start):
        self.g = graph.graph
        self.dist = defaultdict(lambda: float("inf"))
        self.dist[start] = 0
        self.prev = defaultdict(lambda: None)
        self.queue = []
        heapq.heappush(self.queue, (self.dist[start], start))

        while self.queue:
            dist_u, u = heapq.heappop(self.queue)
            if self.dist[u] < dist_u:
                continue
            for v, weight in self.g[u]:
                alt = dist_u + weight
                if self.dist[v] > alt:
                    self.dist[v] = alt
                    self.prev[v] = u
                    heapq.heappush(self.queue, (alt, v))


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

    
    def shortest_path(self, goal):
        path = []
        node = goal
        while node is not None:
            path.append(node)
            node = self.prev[node]
        return path[::-1]


yen_max = 10 ** 15
cost = [0] * n
train_yen = Graph()
train_sno = Graph()
for i in range(m):
    u, v, a, b = map(int, input().split())
    train_yen.add_edge(u - 1, v - 1, a)
    train_yen.add_edge(v - 1, u - 1, a)
    train_sno.add_edge(u - 1, v - 1, b)
    train_sno.add_edge(v - 1, u - 1, b)

cost_yen = Dijkstra(train_yen, s - 1)
cost_sno = Dijkstra(train_sno, t - 1)
for i in range(n):
    cost[i] = yen_max - (cost_yen.dist[i] + cost_sno.dist[i])

ans = [yen_max] * n
ans[n - 1] = cost[n - 1]
for i in range(n - 2, -1, -1):
    ans[i] = max(ans[i + 1], cost[i])
print(*ans, sep="\n")

Submission status: Accepted

Solution code:
import sys
N, M, s, t = map(int, input().split())

from heapq import heappop as hpp, heappush as hp
def dijkstra(N, s, Edge):
    inf = 10**10
    dist = [inf] * N
    Q = [(0, s)]
    decided = set()
    for _ in range(N):
        while True:
            dn, vn = hpp(Q)
            if vn not in decided:
                decided.add(vn)
                dist[vn] = dn
                break
        for vf, df in Edge[vn]:
            if vf not in decided:
                hp(Q, (dn + df, vf))
    return dist
        
        

Edge1 = [[] for _ in range(N)]
Edge2 = [[] for _ in range(N)]
geta = 10**6
for _ in range(M):
    u, v, a, b = map(int, sys.stdin.readline().split())
    u -= 1
    v -= 1
    Edge1[u].append((v, a))
    Edge1[v].append((u, a))
    Edge2[u].append((v, b))
    Edge2[v].append((u, b))

D1 = dijkstra(N, s-1, Edge1)
D2 = dijkstra(N, t-1, Edge2)

D = [d1 + d2 for d1, d2 in zip(D1, D2)]
for i in range(N-2, -1, -1):
   D[i] = min(D[i+1], D[i])

for d in D:
    print(10**15 - d)

Submission status: Accepted

Solution code:
from collections import deque

inf = float('inf')

N, M, S, T = map(int, input().split())
S -= 1
T -= 1

have = 10 ** 15

es = [[] for _ in range(N)]
# es[u]:=(u,a,b), 都市u(0-indexed)の隣接頂点v(0-indexed)とコストa,b

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

cost_a = [inf] * N
cost_a[S] = 0

q = deque()
q.append((S, 0))
while q:
    u, c = q.popleft()
    for v, a, _ in es[u]:
        if cost_a[v] > (c + a):
            cost_a[v] = (c + a)
            q.append((v, c + a))
# print(cost_a)

cost_b = [inf] * N
cost_b[T] = 0

q = deque()
q.append((T, 0))
while q:
    u, c = q.popleft()
    for v, _, b in es[u]:
        if cost_b[v] > (c + b):
            cost_b[v] = (c + b)
            q.append((v, c + b))
# print(cost_b)

cost = [None for _ in range(N)]
t = inf
for i in reversed(range(N)):
    # 移動年i, 0-indexed
    # 都市番号i以上の都市で両替可能
    t = min(t, cost_a[i] + cost_b[i])
    cost[i] = t
    # 都市iで両替する場合の支払い合計
    # costを最小としたい

# print(cost)

for i in range(N):
    print(have - cost[i])

Submission status: Accepted

Solution code:
import collections
import heapq


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

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

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

    def search(self, s):
        """
        :param s: 始点
        :return: 始点から各点までの最短経路
        """
        d = collections.defaultdict(lambda: float('inf'))
        d[s] = 0
        q = []
        heapq.heappush(q, (0, s))
        v = collections.defaultdict(bool)
        while len(q):
            k, u = heapq.heappop(q)
            if v[u]:
                continue
            for uv, ud in self.e[u]:
                if v[uv]:
                    continue
                vd = k + ud
                if d[uv] > vd:
                    d[uv] = vd
                    heapq.heappush(q, (vd, uv))

        return d


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

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

result_a = graph1.search(S)
result_b = graph2.search(T)
ans_list = [float('inf')]
for i in range(N, 0, -1):
    cur = result_a[i] + result_b[i]
    if cur > ans_list[-1]:
        cur = ans_list[-1]
    ans_list.append(cur)

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

Submission status: Accepted

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

Submission status: Accepted

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

def dijkstra(S, N, paths):
    from heapq import heappush,heappop
    q=[(0,S)]
    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))

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
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[i]: S から i まで行くときの最小金額
yen_mins = np.array(dijkstra(graph_yen, start=S))
# snuke_mins[i]: T から i まで行くときの最小金額
snuke_mins = np.array(dijkstra(graph_snuuk, start=T))

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

Submission status: Accepted

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

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

for i in range(m):
    graph[data[i][0]].append([data[i][1], data[i][2], data[i][3]])
    graph[data[i][1]].append([data[i][0], data[i][2], data[i][3]])
city_yen_cost = [INF for i in range(n+1)]
city_yen_cost[s] = 0
queue = []
#[cost, vertex]
heapq.heappush(queue, [0, s])
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])

Submission status: Accepted

Solution code:
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():
    #隣接リスト版
    #同一頂点の複数回探索を防ぐため訪問した頂点数を変数cntで持つ
    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])