Problem:


Score : 600 points

Problem Statement

We have N points numbered 1 to N arranged in a line in this order.

Takahashi decides to make an undirected graph, using these points as the vertices.
In the beginning, the graph has no edge. Takahashi will do M operations to add edges in this graph.
The i-th operation is as follows:

The operation uses integers L_i and R_i between 1 and N (inclusive), and a positive integer C_i. For every pair of integers (s, t) such that L_i \leq s < t \leq R_i, add an edge of length C_i between Vertex s and Vertex t.

The integers L_1, ..., L_M, R_1, ..., R_M, C_1, ..., C_M are all given as input.

Takahashi wants to solve the shortest path problem in the final graph obtained. Find the length of the shortest path from Vertex 1 to Vertex N in the final graph.

Constraints

2 \leq N \leq 10^5

1 \leq M \leq 10^5

1 \leq L_i < R_i \leq N

1 \leq C_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N M
L_1 R_1 C_1
:
L_M R_M C_M

Output

Print the length of the shortest path from Vertex 1 to Vertex N in the final graph.
If there is no shortest path, print -1 instead.

Sample Input 1

4 3
1 3 2
2 4 3
1 4 6

Sample Output 1

5

We have an edge of length 2 between Vertex 1 and Vertex 2, and an edge of length 3 between Vertex 2 and Vertex 4, so there is a path of length 5 between Vertex 1 and Vertex 4.

Sample Input 2

4 2
1 2 1
3 4 2

Sample Output 2

-1

Sample Input 3

10 7
1 5 18
3 4 8
1 3 5
4 7 10
5 9 8
6 10 5
8 10 3

Sample Output 3

28



Sample input:
4 3
1 3 2
2 4 3
1 4 6


Sample output:
5

Submission status: Wrong Answer

Solution code:
import heapq
N, M = map(int, input().split())
edge_list = [{} for i in range(N+1)]
for i in range(2, N + 1):
    edge_list[i][i-1] = 0
min_distance = [float('inf')] * (N+1)
for i in range(M):
    L, R, C = map(int, input().split())
    edge_list[L][R] = C
    edge_list[R][L] = C
def solve(s, t):
    fixed = {}
    q = []
    heapq.heappush(q, (0, s))
    while q:
        w, x = heapq.heappop(q)
        if x == t:
            return w
        if x in fixed:
            continue
        fixed[x] = w
        for y in edge_list[x]:
            if y not in fixed:
                heapq.heappush(q, (w + edge_list[x][y], y))
    return None

answer = solve(1, N)
if answer is None:
    print(-1)
else:
    print(answer)

Submission status: Wrong Answer

Solution code:
def dijkstra(s,n,w,cost):
    #始点sから各頂点への最短距離
    #n:頂点数,　w:辺の数, cost[u][v] : 辺uvのコスト(存在しないときはinf)
    d = [float("inf")] * n
    used = [False] * n
    d[s] = 0
    
    while True:
        v = -1
        #まだ使われてない頂点の中から最小の距離のものを探す
        for i in range(n):
            if (not used[i]) and (v == -1):
               v = i
            elif (not used[i]) and d[i] < d[v]:
                v = i
        if v == -1:
               break
        used[v] = True
               
        for j in range(n):
               d[j] = min(d[j],d[v]+cost[v][j])
    return d

################################
N, M = map(int,input().split()) #n:頂点数　w:辺の数

cost = [[float("inf") for i in range(N)] for i in range(N)] 
#cost[u][v] : 辺uvのコスト(存在しないときはinf この場合は10**10)
for i in range(M):
  l,r,c = map(int,input().split())
  cost[l-1][r-1] = c
for i in range(1, N - 2):
  cost[i+1][i] = 0

ans = dijkstra(0,N,M,cost)
print(ans[-1])

Submission status: Wrong Answer

Solution code:
import heapq
N, M = map(int, input().split())
edge_list = [{} for i in range(N+1)]
for i in range(1, N + 1):
    edge_list[i][i-1] = 0
min_distance = [float('inf')] * (N+1)
for i in range(M):
    L, R, C = map(int, input().split())
    edge_list[L][R] = C
    edge_list[R][L] = C
    # for l in range(L+1,R+1):
    #     edge_list[l][l-1] = 0
        # edge_list[l-1][l] = 0
# print(edge_list)
def solve(s, t):
    fixed = {}
    q = []
    heapq.heappush(q, (0, s))
    while q:
        w, x = heapq.heappop(q)
        # print(w, x)
        if x == t:
            return w
        if x in fixed:
            continue
        fixed[x] = w
        for y in edge_list[x]:
            if y not in fixed:
                heapq.heappush(q, (w + edge_list[x][y], y))
        # print(fixed)
    return None

answer = solve(1, N)
if answer is None:
    print(-1)
else:
    print(answer)
# print(fixed)

Submission status: Wrong Answer

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

N, M = map(int, input().split())
edge = np.array([input().split() for _ in range(M)], dtype = np.int64)
x = np.arange(N,1,-1)
y = np.arange(N-1,0,-1)
z = np.zeros(N-1,int)
X = np.vstack([x,y,z]).T
edge = np.concatenate([X,edge]).T
graph = csr_matrix((edge[2], (edge[:2] - 1)), (N, N))
distance_mat = dijkstra(graph, directed = True, indices = 0)
#print(distance_mat)
print(-1 if distance_mat[-1] == float("inf") else int(distance_mat[-1]))

Submission status: Wrong Answer

Solution code:
import heapq
N, M = map(int, input().split())
edge_list = [{} for i in range(N+1)]
min_distance = [float('inf')] * (N+1)
for i in range(M):
    L, R, C = map(int, input().split())
    edge_list[L][R] = C
    edge_list[R][L] = C
    for l in range(L+1,R+1):
        edge_list[l][l-1] = 0
        # edge_list[l-1][l] = 0
# print(edge_list)
def solve(s, t):
    fixed = {}
    q = []
    heapq.heappush(q, (0, s))
    while q:
        w, x = heapq.heappop(q)
        # print(w, x)
        if x == t:
            return w
        if x in fixed:
            continue
        fixed[x] = w
        for y in edge_list[x]:
            if y not in fixed:
                heapq.heappush(q, (w + edge_list[x][y], y))
        # print(fixed)
    return None

answer = solve(1, N)
if answer is None:
    print(-1)
else:
    print(answer)

Submission status: Wrong Answer

Solution code:
import queue
N, M = map(int, input().split())
edge_list = [{} for i in range(N+1)]
min_distance = [float('inf')] * (N+1)
for i in range(M):
    L, R, C = map(int, input().split())
    edge_list[L][R] = C
    edge_list[R][L] = C
    for l in range(L+1,R+1):
        edge_list[l][l-1] = 0
        # edge_list[l-1][l] = 0
# print(edge_list)
def solve(s, t):
    fixed = {}
    q = queue.PriorityQueue()
    q.put((0, s))
    while q.empty() == False:
        w, x = q.get()
        # print(w, x)
        if x == t:
            return w
        if x in fixed:
            continue
        fixed[x] = w
        for y in edge_list[x]:
            if y not in fixed:
                q.put((w + edge_list[x][y], y))
        # print(fixed)
    return None

answer = solve(1, N)
if answer is None:
    print(-1)
else:
    print(answer)

Submission status: Wrong Answer

Solution code:
import heapq

INF = 2**62-1

def dijkstra(route_map, start_point):
    num_of_city = len(route_map)
    dist = [INF for _ in range(num_of_city)]

    dist[start_point] = 0
    heap_q = []
    heapq.heappush(heap_q, (0, start_point))
    while heap_q:
        prev_cost, src = heapq.heappop(heap_q)

        if dist[src] < prev_cost:
            continue

        for dest, cost in route_map[src].items():
            if dist[dest] > dist[src] + cost:
                dist[dest] = dist[src] + cost
                heapq.heappush(heap_q, (dist[dest], dest))
    return dist

n, m = list(map(int, input().split()))
route_map = [{} for _ in range(n)]

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

for i in range(n-1):
    u, v = i+1, i
    route_map[u][v] = 0

# ゴールまでの最短経路
dijkstra_result = dijkstra(route_map, 0)

if dijkstra_result[-1] != INF:
    print(dijkstra_result[-1])
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
import heapq

INF = 2**62-1

class Dijkstra:
    def __init__(self, rote_map, start_point):
        self.rote_map = rote_map
        self.start_point = start_point

    def execute(self):
        num_of_city = len(self.rote_map)
        dist = [INF for _ in range(num_of_city)]

        dist[self.start_point] = 0
        heap_q = []
        heapq.heappush(heap_q, (0, self.start_point))
        while heap_q:
            prev_cost, src = heapq.heappop(heap_q)

            if dist[src] < prev_cost:
                continue

            for dest, cost in self.rote_map[src].items():
                if dist[dest] > dist[src] + cost:
                    dist[dest] = dist[src] + cost
                    heapq.heappush(heap_q, (dist[dest], dest))
        return dist

n, m = list(map(int, input().split()))
route_map = [{} for _ in range(n)]

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

for i in range(n-1):
    u, v = i+1, i
    route_map[u][v] = 0

# ゴールまでの最短経路
dijkstra_result = Dijkstra(route_map, 0).execute()

if dijkstra_result[-1] != INF:
    print(dijkstra_result[-1])
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
import heapq
  
class Dijkstra:
 
    def __init__(self, rote_map, start_point, goal_point=None):
        self.rote_map = rote_map
        self.start_point = start_point
        self.goal_point = goal_point
 
    def execute(self):
        num_of_city = len(self.rote_map)
        dist = [None for i in range(num_of_city)]
 		
        dist[self.start_point] = 0
        heap_q = []
        heapq.heappush(heap_q, (0, self.start_point))
        while heap_q:
            prev_cost, src = heapq.heappop(heap_q)
            if dist[src] <= prev_cost:
                continue
            if src == self.goal_point:
                return dist[self.goal_point]
 
            for dest, cost in self.rote_map[src].items():
                if dist[dest] == None or dist[dest] > dist[src] + cost:
                    dist[dest] = dist[src] + cost
                    heapq.heappush(heap_q, (dist[dest], dest))
        return -1
 
n,m=map(int, input().split()) 
route_map = [dict() for i in range(n)]
 
for i in range(m):
    u, v, a = map(int, input().split())
    route_map[u-1][v-1] = a
    
for i in range(n-1):
    route_map[i+1][i] = 0

# ゴールまでの最短経路
print(Dijkstra(route_map,0,n-1).execute())

Submission status: Wrong Answer

Solution code:
import heapq

INF = 2**62-1

class Dijkstra:
    def __init__(self, rote_map, start_point):
        self.rote_map = rote_map
        self.start_point = start_point

    def execute(self):
        num_of_city = len(self.rote_map)
        dist = [INF for _ in range(num_of_city)]

        dist[self.start_point] = 0
        heap_q = []
        heapq.heappush(heap_q, (0, self.start_point))
        while heap_q:
            prev_cost, src = heapq.heappop(heap_q)

            # if dist[src] < prev_cost:
                # continue

            for dest, cost in self.rote_map[src].items():
                if dist[dest] > dist[src] + cost:
                    dist[dest] = dist[src] + cost
                    heapq.heappush(heap_q, (dist[dest], dest))
        return dist

n, m = map(int, input().split())
route_map = [{} for _ in range(n)]

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

for i in range(n-1):
    u, v = i+1, i
    route_map[u][v] = 0

# ゴールまでの最短経路
dijkstra_result = Dijkstra(route_map,0).execute()

if dijkstra_result[-1] != INF:
    print(dijkstra_result[-1])
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
import heapq
  
class Dijkstra:
 
    def __init__(self, rote_map, start_point, goal_point=None):
        self.rote_map = rote_map
        self.start_point = start_point
        self.goal_point = goal_point
 
    def execute(self):
        num_of_city = len(self.rote_map)
        dist = [None for i in range(num_of_city)]
 		
        dist[self.start_point] = 0
        heap_q = []
        heapq.heappush(heap_q, (0, self.start_point))
        while heap_q:
            prev_cost, src = heapq.heappop(heap_q)
            if dist[src] < prev_cost:
                continue
            if src == self.goal_point:
                return dist[self.goal_point]
 
            for dest, cost in self.rote_map[src].items():
                if dist[dest] == None or dist[dest] > dist[src] + cost:
                    dist[dest] = dist[src] + cost
                    heapq.heappush(heap_q, (dist[dest], dest))
        return -1
 
n,m=map(int, input().split()) 
route_map = [dict() for i in range(n)]
 
for i in range(m):
    u, v, a = map(int, input().split())
    route_map[u-1][v-1] = a
    
for i in range(n-1):
    route_map[i+1][i] = 0

# ゴールまでの最短経路
print(Dijkstra(route_map,0,n-1).execute())

Submission status: Wrong Answer

Solution code:
import heapq

INF = 2**62-1

class Dijkstra:
    def __init__(self, rote_map, start_point):
        self.rote_map = rote_map
        self.start_point = start_point

    def execute(self):
        num_of_city = len(self.rote_map)
        dist = [INF for _ in range(num_of_city)]

        dist[self.start_point] = 0
        heap_q = []
        heapq.heappush(heap_q, (0, self.start_point))
        while heap_q:
            prev_cost, src = heapq.heappop(heap_q)

            if dist[src] < prev_cost:
                continue

            for dest, cost in self.rote_map[src].items():
                if dist[dest] > dist[src] + cost:
                    dist[dest] = dist[src] + cost
                    heapq.heappush(heap_q, (dist[dest], dest))
        return dist

n, m = map(int, input().split())
route_map = [{} for _ in range(n)]

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

for i in range(n-1):
    u, v = i+1, i
    route_map[u][v] = 0

# ゴールまでの最短経路
dijkstra_result = Dijkstra(route_map,0).execute()

if dijkstra_result[-1] != INF:
    print(dijkstra_result[-1])
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
import heapq

INF = 2**62-1

class Dijkstra:
    def __init__(self, rote_map, start_point):
        self.rote_map = rote_map
        self.start_point = start_point

    def execute(self):
        num_of_city = len(self.rote_map)
        dist = [INF for _ in range(num_of_city)]

        dist[self.start_point] = 0
        heap_q = []
        heapq.heappush(heap_q, (0, self.start_point))
        while len(heap_q) > 0:
            prev_cost, src = heapq.heappop(heap_q)

            if dist[src] < prev_cost:
                continue

            for dest, cost in self.rote_map[src].items():
                if dist[dest] > dist[src] + cost:
                    dist[dest] = dist[src] + cost
                    heapq.heappush(heap_q, (dist[dest], dest))
        return dist

n, m = map(int, input().split())
route_map = [{} for _ in range(n)]

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

for i in range(n-1):
    u, v = i+1, i
    route_map[u][v] = 0

# ゴールまでの最短経路
dijkstra_result = Dijkstra(route_map,0).execute()

if dijkstra_result[-1] != INF:
    print(dijkstra_result[-1])
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
import heapq
  
class Dijkstra:
 
    def __init__(self, rote_map, start_point, goal_point=None):
        self.rote_map = rote_map
        self.start_point = start_point
        self.goal_point = goal_point
 
    def execute(self):
        num_of_city = len(self.rote_map)
        dist = [None for i in range(num_of_city)]
 		
        dist[self.start_point] = 0
        heap_q = []
        heapq.heappush(heap_q, (0, self.start_point))
        while len(heap_q) != 0:
            prev_cost, src = heapq.heappop(heap_q)
            if dist[src] < prev_cost:
                continue
            if src == self.goal_point:
                return dist[self.goal_point]
 
            for dest, cost in self.rote_map[src].items():
                if dist[dest] == None or dist[dest] > dist[src] + cost:
                    dist[dest] = dist[src] + cost
                    heapq.heappush(heap_q, (dist[dest], dest))
        return -1
 
n,m=map(int, input().split()) 
route_map = [dict() for i in range(n)]
 
for i in range(m):
    u, v, a = map(int, input().split())
    route_map[u-1][v-1] = a
    
for i in range(n-1):
    route_map[i+1][i] = 0

# ゴールまでの最短経路
print(Dijkstra(route_map,0,n-1).execute())

Submission status: Wrong Answer

Solution code:
import heapq
 
 
class Dijkstra:
 
    def __init__(self, rote_map, start_point, goal_point=None):
        self.rote_map = rote_map
        self.start_point = start_point
        self.goal_point = goal_point
 
    def execute(self):
        num_of_city = len(self.rote_map)
        dist = [None for _ in range(num_of_city)]
 		
        dist[self.start_point] = 0
        heap_q = []
        heapq.heappush(heap_q, (0, self.start_point))
        while len(heap_q) != 0:
            prev_cost, src = heapq.heappop(heap_q)
            if dist[src] < prev_cost:
                continue
            if src == self.goal_point:
                return prev_cost
 
            for dest, cost in self.rote_map[src].items():
                if dist[dest] == None or dist[dest] > dist[src] + cost:
                    dist[dest] = dist[src] + cost
                    heapq.heappush(heap_q, (dist[dest], dest))
        return -1
 
n,m=map(int, input().split()) 
route_map = [dict() for _ in range(n)]
 
for i in range(m):
    u, v, a = map(int, input().split())
    u, v = u - 1, v - 1
    route_map[u][v] = a
    
for i in range(n-1):
    u, v = i+1, i
    route_map[u][v] = 0
    
 
# ゴールまでの最短経路
print(Dijkstra(route_map,0,n-1).execute())

Submission status: Wrong Answer

Solution code:
import heapq

class Dijkstra:
    def __init__(self, rote_map, start_point):
        self.rote_map = rote_map
        self.start_point = start_point

    def execute(self):
        num_of_city = len(self.rote_map)
        dist = [float("inf") for _ in range(num_of_city)]

        dist[self.start_point] = 0
        heap_q = []
        heapq.heappush(heap_q, (0, self.start_point))
        while len(heap_q) > 0:
            prev_cost, src = heapq.heappop(heap_q)

            if dist[src] < prev_cost:
                continue

            for dest, cost in self.rote_map[src].items():
                if dist[dest] > dist[src] + cost:
                    dist[dest] = dist[src] + cost
                    heapq.heappush(heap_q, (dist[dest], dest))
        return dist

n, m = map(int, input().split()) 
route_map = [{} for _ in range(n)]

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

for i in range(n-1):
    u, v = i+1, i
    route_map[u][v] = 0

# ゴールまでの最短経路
dijkstra_result = Dijkstra(route_map,0).execute()

if dijkstra_result[-1] != float('inf'):
    print(dijkstra_result[-1])
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
import heapq
 
 
class Dijkstra:
 
    def __init__(self, rote_map, start_point, goal_point=None):
        self.rote_map = rote_map
        self.start_point = start_point
        self.goal_point = goal_point
 
    def execute(self):
        num_of_city = len(self.rote_map)
        dist = [10**18 for _ in range(num_of_city)]
 		
        dist[self.start_point] = 0
        heap_q = []
        heapq.heappush(heap_q, (0, self.start_point))
        while len(heap_q) != 0:
            prev_cost, src = heapq.heappop(heap_q)
            if dist[src] < prev_cost:
                continue
            if src == self.goal_point:
                return prev_cost
 
            for dest, cost in self.rote_map[src].items():
                if dist[dest] == 10**18 or dist[dest] > dist[src] + cost:
                    dist[dest] = dist[src] + cost
                    heapq.heappush(heap_q, (dist[dest], dest))
        return -1
 
n,m=map(int, input().split()) 
route_map = [dict() for _ in range(n)]
 
for i in range(m):
    u, v, a = map(int, input().split())
    u, v = u - 1, v - 1
    route_map[u][v] = a
    
for i in range(n-1):
    u, v = i+1, i
    route_map[u][v] = 0
    
 
# ゴールまでの最短経路
print(Dijkstra(route_map,0,n-1).execute())

Submission status: Wrong Answer

Solution code:
import heapq
 
 
class Dijkstra:
 
    def __init__(self, rote_map, start_point, goal_point=None):
        self.rote_map = rote_map
        self.start_point = start_point
        self.goal_point = goal_point
 
    def execute(self):
        num_of_city = len(self.rote_map)
        dist = [10**16 for _ in range(num_of_city)]
        prev = [10**16 for _ in range(num_of_city)]
 		
        dist[self.start_point] = 0
        heap_q = []
        heapq.heappush(heap_q, (0, self.start_point))
        while len(heap_q) != 0:
            prev_cost, src = heapq.heappop(heap_q)
            if dist[src] < prev_cost:
                continue
            if src == self.goal_point:
                return prev_cost
 
            for dest, cost in self.rote_map[src].items():
                if dist[dest] > prev_cost + cost:
                    dist[dest] = prev_cost + cost
                    heapq.heappush(heap_q, (dist[dest], dest))
                    prev[dest] = src
        return -1
 
n,m=map(int, input().split()) 
route_map = [dict() for _ in range(n)]
 
for i in range(m):
    u, v, a = map(int, input().split())
    u, v = u - 1, v - 1
    route_map[u][v] = a
    
for i in range(n-1):
    u, v = i+1, i
    route_map[u][v] = 0
    
 
# ゴールまでの最短経路
print(Dijkstra(route_map,0,n-1).execute())

Submission status: Wrong Answer

Solution code:
import heapq

class Dijkstra:
    def __init__(self, rote_map, start_point):
        self.rote_map = rote_map
        self.start_point = start_point

    def execute(self):
        num_of_city = len(self.rote_map)
        dist = [float("inf") for _ in range(num_of_city)]

        dist[self.start_point] = 0
        heap_q = []
        heapq.heappush(heap_q, (0, self.start_point))
        while len(heap_q) != 0:
            prev_cost, src = heapq.heappop(heap_q)

            if dist[src] < prev_cost:
                continue

            for dest, cost in self.rote_map[src].items():
                if dist[dest] > dist[src] + cost:
                    dist[dest] = dist[src] + cost
                    heapq.heappush(heap_q, (dist[dest], dest))
        return dist

n, m = map(int, input().split()) 
route_map = [{} for _ in range(n)]

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

for i in range(n-1):
    u, v = i+1, i
    route_map[u][v] = 0

# ゴールまでの最短経路
dijkstra_result = Dijkstra(route_map,0).execute()

if dijkstra_result[-1] != float('inf'):
    print(dijkstra_result[-1])
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
import heapq
 
 
class Dijkstra:
 
    def __init__(self, rote_map, start_point, goal_point=None):
        self.rote_map = rote_map
        self.start_point = start_point
        self.goal_point = goal_point
 
    def execute(self):
        num_of_city = len(self.rote_map)
        dist = [10**16 for _ in range(num_of_city)]
        prev = [10**16 for _ in range(num_of_city)]
 
        dist[self.start_point] = 0
        heap_q = []
        heapq.heappush(heap_q, (0, self.start_point))
        while len(heap_q) != 0:
            prev_cost, src = heapq.heappop(heap_q)
 
            if dist[src] < prev_cost:
                continue
 
            for dest, cost in self.rote_map[src].items():
                if dist[dest] > prev_cost + cost:
                    dist[dest] = prev_cost + cost
                    heapq.heappush(heap_q, (dist[dest], dest))
                    prev[dest] = src
        return dist
 
n,m=map(int, input().split()) 
route_map = [dict() for _ in range(n)]
 
for i in range(m):
    u, v, a = map(int, input().split())
    u, v = u - 1, v - 1
    route_map[u][v] = a
    
for i in range(n-1):
    u, v = i+1, i
    route_map[u][v] = 0
    
 
# ゴールまでの最短経路
dijkstra_result = Dijkstra(route_map,0).execute()
 
 
if dijkstra_result[n-1]!=10**16:
    print(dijkstra_result[n-1])
 
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
import heapq
 
 
class Dijkstra:
 
    def __init__(self, rote_map, start_point, goal_point=None):
        self.rote_map = rote_map
        self.start_point = start_point
        self.goal_point = goal_point
 
    def execute(self):
        num_of_city = len(self.rote_map)
        dist = [10**16 for _ in range(num_of_city)]
        prev = [10**16 for _ in range(num_of_city)]
 
        dist[self.start_point] = 0
        heap_q = []
        heapq.heappush(heap_q, (0, self.start_point))
        while len(heap_q) != 0:
            prev_cost, src = heapq.heappop(heap_q)
 
            if dist[src] < prev_cost:
                continue
 
            for dest, cost in self.rote_map[src].items():
                if dist[dest] > dist[src] + cost:
                    dist[dest] = dist[src] + cost
                    heapq.heappush(heap_q, (dist[dest], dest))
                    prev[dest] = src
        return dist
 
n,m=map(int, input().split()) 
route_map = [dict() for _ in range(n)]
 
for i in range(m):
    u, v, a = map(int, input().split())
    u, v = u - 1, v - 1
    route_map[u][v] = a
    
for i in range(n-1):
    u, v = i+1, i
    route_map[u][v] = 0
    
 
# ゴールまでの最短経路
dijkstra_result = Dijkstra(route_map,0).execute()
 
 
if dijkstra_result[-1]!=10**16:
    print(dijkstra_result[n-1])
 
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
import heapq
 
 
class Dijkstra:
 
    def __init__(self, rote_map, start_point, goal_point=None):
        self.rote_map = rote_map
        self.start_point = start_point
        self.goal_point = goal_point
 
    def execute(self):
        num_of_city = len(self.rote_map)
        dist = [10**16 for _ in range(num_of_city)]
        prev = [10**16 for _ in range(num_of_city)]
 
        dist[self.start_point] = 0
        heap_q = []
        heapq.heappush(heap_q, (0, self.start_point))
        while len(heap_q) != 0:
            prev_cost, src = heapq.heappop(heap_q)
 
            if dist[src] < prev_cost:
                continue
 
            for dest, cost in self.rote_map[src].items():
                if cost != 10**16 and dist[dest] > dist[src] + cost:
                    dist[dest] = dist[src] + cost
                    heapq.heappush(heap_q, (dist[dest], dest))
                    prev[dest] = src
        if self.goal_point is not None:
            return self.get_path(self.goal_point, prev)
        else:
            return dist
 
    def get_path(self, goal, prev):
        path = [goal]
        dest = goal
 
        while prev[dest] != 10**16:
            path.append(prev[dest])
            dest = prev[dest]
 
        return list(reversed(path))
 
n,m=map(int, input().split()) 
route_map = [dict() for _ in range(n)]
 
for i in range(m):
    u, v, a = map(int, input().split())
    u, v = u - 1, v - 1
    route_map[u][v] = a
    
for i in range(n-1):
    u, v = i+1, i
    route_map[u][v] = 0
    
 
# ゴールまでの最短経路
dijkstra_result = Dijkstra(route_map,0).execute()
 
 
if dijkstra_result[-1]!=10**16:
    print(dijkstra_result[n-1])
 
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
import heapq
 
 
class Dijkstra:
 
    def __init__(self, rote_map, start_point, goal_point=None):
        self.rote_map = rote_map
        self.start_point = start_point
        self.goal_point = goal_point
 
    def execute(self):
        num_of_city = len(self.rote_map)
        dist = [10**16 for _ in range(num_of_city)]
        prev = [10**16 for _ in range(num_of_city)]
 
        dist[self.start_point] = 0
        heap_q = []
        heapq.heappush(heap_q, (0, self.start_point))
        while len(heap_q) != 0:
            prev_cost, src = heapq.heappop(heap_q)
 
            if dist[src] < prev_cost:
                continue
 
            for dest, cost in self.rote_map[src].items():
                if cost != 10**16 and dist[dest] > dist[src] + cost:
                    dist[dest] = dist[src] + cost
                    heapq.heappush(heap_q, (dist[dest], dest))
                    prev[dest] = src
        if self.goal_point is not None:
            return self.get_path(self.goal_point, prev)
        else:
            return dist
 
    def get_path(self, goal, prev):
        path = [goal]
        dest = goal
 
        while prev[dest] != 10**16:
            path.append(prev[dest])
            dest = prev[dest]
 
        return list(reversed(path))
 
n,m=map(int, input().split()) 
route_map = [dict() for _ in range(n)]
 
for i in range(m):
    u, v, a = map(int, input().split())
    u, v = u - 1, v - 1
    route_map[u][v] = a
    
for i in range(n-1):
    u, v = i+1, i
    route_map[u][v] = 0
    
 
# ゴールまでの最短経路
dijkstra_result = Dijkstra(route_map,0).execute()
 
 
if dijkstra_result[-1]!=float('inf'):
    print(dijkstra_result[n-1])
 
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
import heapq
 
 
class Dijkstra:
 
    def __init__(self, rote_map, start_point, goal_point=None):
        self.rote_map = rote_map
        self.start_point = start_point
        self.goal_point = goal_point
 
    def execute(self):
        num_of_city = len(self.rote_map)
        dist = [float("inf") for _ in range(num_of_city)]
        prev = [float("inf") for _ in range(num_of_city)]
 
        dist[self.start_point] = 0
        heap_q = []
        heapq.heappush(heap_q, (0, self.start_point))
        while len(heap_q) != 0:
            prev_cost, src = heapq.heappop(heap_q)
 
            if dist[src] < prev_cost:
                continue
 
            for dest, cost in self.rote_map[src].items():
                if cost != float("inf") and dist[dest] > dist[src] + cost:
                    dist[dest] = dist[src] + cost
                    heapq.heappush(heap_q, (dist[dest], dest))
                    prev[dest] = src
        if self.goal_point is not None:
            return self.get_path(self.goal_point, prev)
        else:
            return dist
 
    def get_path(self, goal, prev):
        path = [goal]
        dest = goal
 
        while prev[dest] != float("inf"):
            path.append(prev[dest])
            dest = prev[dest]
 
        return list(reversed(path))
 
n,m=map(int, input().split()) 
route_map = [dict() for _ in range(n)]
 
for i in range(m):
    u, v, a = map(int, input().split())
    u, v = u - 1, v - 1
    route_map[u][v] = a
    
for i in range(n-1):
    u, v = i+1, i
    route_map[u][v] = 0
    
 
# ゴールまでの最短経路
dijkstra_result = Dijkstra(route_map,0).execute()
 
 
if dijkstra_result[-1]!=float('inf'):
    print(int(dijkstra_result[n-1]))
 
else:
    print(-1)