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:
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(100000)
input = sys.stdin.readline
INF = 2**62-1


def read_int():
    return int(input())


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


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


def Dijkstra(g, s):
    V = list(g.keys())
    for v in g.values():
        V.extend(list(v.keys()))
    V = set(V)

    d = {}
    prev = {}
    q = []
    done = set()
    for v in V:
        if v == s:
            d[v] = 0
        else:
            d[v] = INF
    heapq.heappush(q, (d[s], s))

    while q:
        c, u = heapq.heappop(q)
        if u in done:
            continue
        done.add(u)
        for v in g[u]:
            alt = g[u][v] + d[u]
            if d[v] > alt:
                d[v] = alt
                prev[v] = u
                heapq.heappush(q, (d[v], v))
    return d


@mt
def slv(N, LRC):
    g = defaultdict(dict)
    for i in range(1, N):
        g[i+1][i] = 0

    for l, r, c in LRC:
        g[l][r] = c
        g[r][l] = c
    
    d = Dijkstra(g, 1)

    return -1 if d[N] == INF else d[N]


def main():
    N, M = read_int_n()
    LRC = [read_int_n() for _ in range(M)]
    print(slv(N, LRC))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(100000)
input = sys.stdin.readline
INF = 2**62-1


def read_int():
    return int(input())


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


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


def Dijkstra(g, s):
    V = list(g.keys())
    for v in g.values():
        V.extend(list(v.keys()))
    V = set(V)

    d = {}
    prev = {}
    q = []
    done = set()
    for v in V:
        if v == s:
            d[v] = 0
        else:
            d[v] = INF
    heapq.heappush(q, (d[s], s))

    while q:
        c, u = heapq.heappop(q)
        if u in done:
            continue
        done.add(u)
        for v in g[u]:
            alt = g[u][v] + d[u]
            if d[v] > alt:
                d[v] = alt
                prev[v] = u
                heapq.heappush(q, (d[v], v))
    return d


@mt
def slv(N, LRC):
    g = defaultdict(dict)
    for i in range(1, N+1):
        g[i+1][i] = 0

    for l, r, c in LRC:
        g[l][r] = c
        g[r][l] = c
    
    d = Dijkstra(g, 1)

    return -1 if d[N] == INF else d[N]


def main():
    N, M = read_int_n()
    LRC = [read_int_n() for _ in range(M)]
    print(slv(N, LRC))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
n, m = map(int,input().split())
C = [list(map(int,input().split())) for i in range(m)]
S = [0] * n

for i in range(m):
    for j in range(2):
        C[i][j] -= 1

for i in range(m):
    S[C[i][0]] += 1
    S[C[i][1]] -= 1

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

for i in range(n-1):
    if S[i] > 0:
        C.append([i+1, i, 0])

M = [[] for i in range(n)]
for i in range(len(C)):
    M[C[i][0]].append([C[i][1], C[i][2]])
    if C[i][2] > 0:
        M[C[i][1]].append([C[i][0], C[i][2]])

import heapq

D = [float("inf")] * n
P = [-1] * n
D[0] = 0
V = [0] * n
Q = []
for v in range(n):
    heapq.heappush(Q, [D[v], v])

le = len(Q)
while le > 0:
    q = heapq.heappop(Q)
    le -= 1
    u = q[1]
    du = q[0]
    V[u] = 1
    for i in range(len(M[u])):
        v = M[u][i][0]
        luv = M[u][i][1]
        if V[v] == 0:
            alt = du + luv
            if D[v] > alt:
                D[v] = alt
                P[v] = u
                heapq.heappush(Q, [alt, v])

ans = D[-1]
if ans == float("inf"):
    ans = -1
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
n, m = map(int,input().split())
C = [list(map(int,input().split())) for i in range(m)]
S = [0] * n

for i in range(m):
    for j in range(2):
        C[i][j] -= 1

for i in range(m):
    S[C[i][0]] += 1
    S[C[i][1]] -= 1

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

for i in range(n-1):
    if S[i] > 0:
        C.append([i+1, i, 0])

M = [[] for i in range(n)]
for i in range(len(C)):
    M[C[i][0]].append([C[i][1], C[i][2]])
    if C[i][2] > 0:
        M[C[i][1]].append([C[i][0], C[i][2]])

import heapq

D = [float("inf")] * n
P = [-1] * n
D[0] = 0
Q = []
for v in range(n):
    heapq.heappush(Q, [D[v], v])

le = len(Q)
while le > 0:
    q = heapq.heappop(Q)
    le -= 1
    u = q[1]
    du = q[0]
    for i in range(len(M[u])):
        v = M[u][i][0]
        luv = M[u][i][1]
        alt = du + luv
        if D[v] > alt:
            D[v] = alt
            P[v] = u
            heapq.heappush(Q, [alt, v])

ans = D[-1]
if ans == float("inf"):
    ans = -1
print(ans)

Submission status: Wrong Answer

Solution code:
from heapq import heappush, heappop
INF = 10**10
n,m=map(int,input().split())
graph=[[] for i in range(n)]
for i in range(m):
  l,r,c=map(int,input().split())
  l-=1;r-=1
  graph[l].append((r,c))
for i in range(n-1):
  graph[i+1].append((i,0))
def dijkstra(N, G, s):
    global dist
    dist = [INF] * N
    que = [(0, s)]
    dist[s] = 0
    while que:
        c, v = heappop(que)
        if dist[v] < c:
            continue
        for t, cost in G[v]:
            if dist[v] + cost < dist[t]:
                dist[t] = dist[v] + cost
                heappush(que, (dist[t], t))
dijkstra(n,graph,0)
print(dist[n-1])

Submission status: Wrong Answer

Solution code:
"""
#editorialより
頂点1から頂点iへの最短路の長さをdiとするとd1≦d2≦....≦dN
各iに対して頂点i+ 1から頂点iに長さ0の辺を追加
このときi回目に追加される辺について考えると、
頂点Liから頂点Riへの長さCiの辺を追加すれば十分
"""
import sys,heapq
input = sys.stdin.readline

n,m = map(int,input().split( ))
Ad = [[] for _ in range(n)]
for i in range(m):
    li,ri,ci = map(int, input().split( ))
    Ad[li-1].append((ri-1,ci))
for i in range(1,n):
    Ad[i].append((i-1,0))

inf = 10**10
D = [inf for _ in range(n)]
#Prev = [-1 for _ in range(n)]
D[0] = 0
Q = []
heapq.heappush(Q, (0,0))

while Q:
    d,q = heapq.heappop(Q) #qは[距離、頂点番号]：キューの優先度の都合で番号が後
    #print(q)
    for v,c in Ad[q]:#nは[辺の終点、辺の長さ]
        if D[v]>d+c:
            D[v] = d+c
            #Prev[n[0]] = q[1]
            heapq.heappush(Q, (d+c,v))

if D[n-1]<inf:
    print(D[n-1])
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
"""
#editorialより
頂点1から頂点iへの最短路の長さをdiとするとd1≦d2≦....≦dN
各iに対して頂点i+ 1から頂点iに長さ0の辺を追加
このときi回目に追加される辺について考えると、
頂点Liから頂点Riへの長さCiの辺を追加すれば十分
"""

import heapq
n,m = map(int,input().split( ))
Ad = [[] for _ in range(n)]
for i in range(m):
    li,ri,ci = map(int, input().split( ))
    Ad[li-1].append((ri-1,ci))
for i in range(1,n):
    Ad[i].append((i-1,0))

inf = 10**10
D = [inf for _ in range(n)]
#Prev = [-1 for _ in range(n)]
D[0] = 0
Q = []
heapq.heappush(Q, [0,0])

while Q:
    d,q = Q.pop() #qは[距離、頂点番号]：キューの優先度の都合で番号が後
    #print(q)
    for v,c in Ad[q]:#nは[辺の終点、辺の長さ]
        if D[v]>d+c:
            D[v] = d+c
            #Prev[n[0]] = q[1]
            heapq.heappush(Q, [D[v],v])

if D[n-1]<inf:
    print(D[n-1])
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
"""
#editorialより
頂点1から頂点iへの最短路の長さをdiとするとd1≦d2≦....≦dN
各iに対して頂点i+ 1から頂点iに長さ0の辺を追加
このときi回目に追加される辺について考えると、
頂点Liから頂点Riへの長さCiの辺を追加すれば十分
"""

import heapq
n,m = map(int,input().split( ))
Ad = [[] for _ in range(n)]
for i in range(m):
    li,ri,ci = map(int, input().split( ))
    Ad[li-1].append((ri-1,ci))
for i in range(1,n):
    Ad[i].append((i-1,0))

inf = 10**10
D = [inf for _ in range(n)]
#Prev = [-1 for _ in range(n)]
D[0] = 0
Q = []
heapq.heappush(Q, [0,0])

while Q:
    q = Q.pop() #qは[距離、頂点番号]：キューの優先度の都合で番号が後
    #print(q)
    for v in Ad[q[1]]:#nは[辺の終点、辺の長さ]
        if D[v[0]]>D[q[1]]+v[1]:
            D[v[0]] = D[q[1]]+v[1]
            #Prev[n[0]] = q[1]
            heapq.heappush(Q, [D[v[0]],v[0]])

if D[n-1]<inf:
    print(D[n-1])
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
def update(k,x):
  k+=N0-1
  data[k]=x
  while k>=0:
    k=~-k//2
    data[k]=min(data[2*k+1],data[2*k+2])
def query(l,r):
  L,R=l+N0,r+N0
  s=INF
  while L<R:
    if R&1:
      R-=1
      s=min(s,data[R-1])
    if L&1:
      s=min(s,data[L-1])
      L+=1
    L//=2
    R//=2
  return s
n,m,*t=map(int,open(0).read().split())
N0=2**(n).bit_length()
INF=2**31-1
data=[INF]*2*N0
update(1,0)
for l,r,c in sorted(zip(*[iter(t)]*3)):update(r,min(data[r+N0-1],query(l,r)+c))
print(data[n+N0-1]%INF or-1)

Submission status: Wrong Answer

Solution code:
import numpy as np
import heapq

INF=10**13
N,M=map(int,input().split())
adj=[[] for i in range(N)]
for i in range(M):
    L,R,C=map(int,input().split())
    L-=1
    R-=1
    adj[L].append((R,C))
    adj[R].append((L,C))
for i in range(N-1):
    adj[i+1].append((i,0))
    
s=0
g=N-1
heap=[]
table=np.array([INF for i in range(N)])
table[s]=0
heapq.heappush(heap,(s,table[s]))
                
while heap:
    q=heapq.heappop(heap)
    a=q[0]
    if q[1]>table[a]: continue
    for ne in adj[a]:
        p=ne[0]
        co=ne[1]
        if table[p]>table[a]+co:
            table[p]=table[a]+co
            heapq.heappush(heap,(p,table[p]))
ans=table[g]
if ans==INF:
    print(-1)
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np
import heapq

INF=10**13
N,M=map(int,input().split())
adj=[[] for i in range(N)]
for i in range(M):
    L,R,C=map(int,input().split())
    L-=1
    R-=1
    adj[L].append((R,C))
    adj[R].append((L,C))
for i in range(N-1):
    adj[i+1].append((i,0))
    
s=0
g=N-1
heap=[]
table=np.array([INF for i in range(N)])
table[s]=0
heapq.heappush(heap,(table[s],s))
                
while heap:
    q=heapq.heappop(heap)
    a=q[1]
    if q[0]>table[a]: continue
    for ne in adj[a]:
        p=ne[0]
        co=ne[1]
        if table[p]>table[a]+co:
            table[p]=table[a]+co
            heapq.heappush(heap,(table[p],p))
ans=table[g]
if ans==INF:
    print(-1)
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np
import heapq

INF=10**12
N,M=map(int,input().split())
adj=[[] for i in range(N)]
for i in range(M):
    L,R,C=map(int,input().split())
    L-=1
    R-=1
    adj[L].append((R,C))
    adj[R].append((L,C))
for i in range(N-1):
    adj[i+1].append((i,0))
    
s=0
g=N-1
heap=[]
table=np.array([INF for i in range(N)])
table[s]=0
heapq.heappush(heap,(s,table[s]))
                
while heap:
    q=heapq.heappop(heap)
    a=q[0]
    if q[1]>table[a]: continue
    for ne in adj[a]:
        p=ne[0]
        co=ne[1]
        if table[p]>table[a]+co:
            table[p]=table[a]+co
            heapq.heappush(heap,(p,table[p]))
ans=table[g]
if ans==INF:
    print(-1)
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np
import heapq

INF=10**12
N,M=map(int,input().split())
adj=[[] for i in range(N)]
for i in range(M):
    L,R,C=map(int,input().split())
    L-=1
    R-=1
    adj[L].append((R,C))
    adj[R].append((L,C))
for i in range(N-1):
    adj[i+1].append((i,0))
    
s=0
g=N-1
heap=[]
table=np.array([INF for i in range(N)])
table[s]=0
heapq.heappush(heap,(table[s],s))
                
while heap:
    q=heapq.heappop(heap)
    a=q[1]
    if q[0]>table[a]: continue
    for ne in adj[a]:
        p=ne[0]
        co=ne[1]
        if table[p]>table[a]+co:
            table[p]=table[a]+co
            heapq.heappush(heap,(table[p],p))
ans=table[g]
if ans==INF:
    print(-1)
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np
import heapq

INF=10**12
N,M=map(int,input().split())
adj=[[] for i in range(N)]
for i in range(M):
    L,R,C=map(int,input().split())
    L-=1
    R-=1
    adj[L].append((R,C))
    adj[R].append((L,C))
for i in range(N-1):
    adj[i+1].append((i,0))
    
s=0
g=N-1
heap=[]
table=np.array([INF for i in range(N)])
table[s]=0
heapq.heappush(heap,(table[s],s))
                
while heap:
    q=heapq.heappop(heap)
    a=q[1]
    if q[0]>table[a]: continue
    for ne in adj[a]:
        p=ne[0]
        co=ne[1]
        if table[p]>table[a]+co:
            table[p]=table[a]+co
            heapq.heappush(heap,(table[p],p))
ans=table[g]
if ans==INF:
    print(-1)
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np
import heapq

INF=10**12
N,M=map(int,input().split())
adj=[[] for i in range(N)]
for i in range(M):
    L,R,C=map(int,input().split())
    L-=1
    R-=1
    adj[L].append((R,C))
    adj[R].append((L,C))
for i in range(N-1):
    adj[i+1].append((i,0))
    
s=0
g=N-1
heap=[]
table=np.array([INF for i in range(N)])
table[s]=0
heapq.heappush(heap,(table[s],s))
                
while heap:
    q=heapq.heappop(heap)
    a=q[1]
    if q[0]<table[a]: continue
    for ne in adj[a]:
        p=ne[0]
        co=ne[1]
        if table[p]>table[a]+co:
            table[p]=table[a]+co
            heapq.heappush(heap,(table[p],p))
ans=table[g]
if ans==INF:
    print(-1)
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np
import heapq

INF=10**12
N,M=map(int,input().split())
adj=[[] for i in range(N)]
for i in range(M):
    L,R,C=map(int,input().split())
    L-=1
    R-=1
    adj[L].append((R,C))
    adj[R].append((L,C))
for i in range(N-1):
    adj[i+1].append((i,0))
    
s=0
g=N-1
heap=[]
table=np.array([INF for i in range(N)])
table[s]=0
heapq.heappush(heap,(table[s],s))
                
while heap:
    q=heapq.heappop(heap)
    a=q[1]
    for ne in adj[a]:
        p=ne[0]
        co=ne[1]
        if table[p]>table[a]+co:
            table[p]=table[a]+co
            heapq.heappush(heap,(table[p],p))
ans=table[g]
if ans==INF:
    print(-1)
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
from sys import stdin
import heapq

N, M = [int(x) for x in stdin.readline().rstrip().split()]
B = [[int(x) for x in stdin.readline().rstrip().split()] for _ in range(M)]

graph = [[10 ** 7] * N for _ in range(N)]
for i in range(1, N):
    graph[i][i - 1] = 0
for m in B:
    graph[m[0] - 1][m[1] - 1] = m[2]

d = [(0, 0)]
distance = [10 ** 7] * N
distance[0] = 0
color = ['white'] * N
heapq.heapify(d)

while len(d) > 0:
    temp, u = heapq.heappop(d)
    color[u] = 'black'
    if distance[u] < temp:
        continue
    for v in range(N):
        if color[v] != 'black':
            if distance[u] + graph[u][v] < distance[v]:
                distance[v] = distance[u] + graph[u][v]
                color[v] = 'gray'
                heapq.heappush(d, (distance[v], v))

print(distance[N - 1])

Submission status: Wrong Answer

Solution code:
n,m,*t,d=map(int,open(0).read().split())
d=[0]*2+[1e18]*n
for l,r,c in sorted(zip(*[iter(t)]*3)):
 for i in range(r,l,-1):
  if d[l]+c<d[i]:d[i]=d[l]+c
  else:break
print(d[-2]%10**18or-1)

Submission status: Wrong Answer

Solution code:
n,m,*t,d=map(int,open(0).read().split())
d=[0]*2+[1e18]*n
for l,r,c in sorted(zip(*[iter(t)]*3)):
 for i in range(r,l,-1):
  a=d[l]+c
  if a<d[i]:d[i]=a
  else:break
print(d[-2]%10**18or-1)

Submission status: Wrong Answer

Solution code:
n, m = [ int(v) for v in input().split() ]
edge = sorted([ [ int(v) for v in input().split() ] for i in range(m) ])
inf = 10**12
ln = len(bin(n-1))-1
ln2 = 2**(ln-1)
segtree = [ inf for i in range(2**ln-1) ]


def update_tomin(p, q):
    p += 2**(ln-1)-2
    r = 0
    while p >= 0:
        segtree[p] = min(segtree[p], q)
        p = (p-1)//2

def getmin(a,b,k=0,l=0,r=ln2):
    if r <= a or b <= l:
        return inf
    elif l == r:
        return inf
    elif a == l and b == r:
        return segtree[k]
    elif l + 1 == r:
        return segtree[k]
    else:
        return min( getmin(a,min(b,(l+r)//2),2*k+1,l,(l+r)//2), getmin(max(a,(l+r)//2),b,2*k+2,(l+r)//2,r) )

update_tomin(1, 0)
for i in edge:
    l, r, c = i
    mincost = getmin(l-1,r)
    update_tomin(r, c+mincost)
print( getmin(n-1,n) if getmin(n-1,n) != inf else -1 )

Submission status: Wrong Answer

Solution code:

n, m = [ int(v) for v in input().split() ]
edge = sorted([ [ int(v) for v in input().split() ] for i in range(m) ])
inf = 10**12
ln = len(bin(n-1))-1
ln2 = 2**(ln-1)
segtree = [ inf for i in range(2**ln-1) ]


def update_tomin(p, q):
    p += 2**(ln-1)-2
    r = 0
    while p >= 0:
        segtree[p] = min(segtree[p], q)
        p = (p-1)//2

def getmin(a,b,k=0,l=0,r=ln2):
    if r <= a or b <= l:
        return inf
    elif l == r:
        return inf
    elif a == l and b == r:
        return segtree[k]
    elif l + 1 == r:
        return segtree[k]
    else:
        return min( getmin(a,min(b,(l+r)//2),2*k+1,l,(l+r)//2), getmin(max(a,(l+r)//2),b,2*k+2,(l+r)//2,r) )

update_tomin(1, 0)
for i in edge:
    l, r, c = i
    mincost = getmin(l-1,r)
    update_tomin(r, c+mincost)
print(getmin(n-1,n))

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())
ro = [[float("inf") for i in range(n)] for i in range(n)]
lis = [[int(i) for i in input().split(" ")] for i in range(m)]
for i in lis:
    ro[i[0]-1][i[1]-1] = i[2]

cost = ro         
te = dijkstra(0,n,n-1,cost)
if te[-1] == float("inf"):
    print(-1)
else:
    print(te[-1])

Submission status: Wrong Answer

Solution code:
import sys
from collections import defaultdict as dd
from bisect import bisect_left as bl
import heapq
input = sys.stdin.readline
N, M = map(int, input().split())
a = []
e = dd(list)
s = []
for _ in range(M):
  a.append(list(map(int, input().split())))
  e[a[-1][0]].append((a[-1][1], a[-1][2]))
  s.append(a[-1][0])
a.sort(key = lambda x: (x[0], x[1]))
s.sort()
x = 0
#print(s)
for i in range(M):
  y = bl(s, a[i][1])
  #print(a[i], x, y)
  for j in range(max(0, x - 1), min(y, N + 1)):
    e[a[i][1]].append((a[j][0], 0))
  x = y
class dijkstra:
  def __init__(self, n, e):
    self.e = e
    self.n = n
  def path(self, s, t):
    d = [float("inf")] * (self.n + 1)
    vis = set()
    d[s] = 0
    h = [s]
    while not t in vis and len(h):
      v = heapq.heappop(h)
      v1 = v % (10 ** 10)
      v0 = v // (10 ** 10)
      if v1 in vis: continue
      vis.add(v1)
      for p in self.e[v1]:
        d[p[0]] = min(d[p[0]], d[v1] + p[1])
        if p[0] in vis: continue
        heapq.heappush(h, d[p[0]] * (10 ** 10) + p[0])
    return d[t]
d = dijkstra(N, e)
res = d.path(1, N)
if res == float("inf"): print(-1)
else: print(res)

Submission status: Wrong Answer

Solution code:
import sys
from collections import defaultdict as dd
from bisect import bisect_left as bl
import heapq
input = sys.stdin.readline
N, M = map(int, input().split())
a = []
e = dd(list)
s = []
for _ in range(M):
  a.append(list(map(int, input().split())))
  e[a[-1][0]].append((a[-1][1], a[-1][2]))
  s.append(a[-1][0])
a.sort(key = lambda x: (x[0], x[1]))
s.sort()
x = 0
#print(s)
for i in range(M):
  y = bl(s, a[i][1])
  #print(a[i], x, y)
  for j in range(max(0, x - 1), min(y, N + 1)):
    e[a[i][1]].append((a[j][0], 0))
  x = y
class dijkstra:
  def __init__(self, n, e):
    self.e = e
    self.n = n
  def path(self, s, t):
    d = [float("inf")] * (self.n + 1)
    vis = set()
    d[s] = 0
    h = [s]
    while not t in vis and len(h):
      v = heapq.heappop(h)
      v1 = v % (10 ** 6)
      v0 = v // (10 ** 6)
      if v1 in vis: continue
      vis.add(v1)
      for p in self.e[v1]:
        d[p[0]] = min(d[p[0]], d[v1] + p[1])
        if p[0] in vis: continue
        heapq.heappush(h, d[p[0]] * (10 ** 6) + p[0])
    return d[t]
d = dijkstra(N, e)
res = d.path(1, N)
if res == float("inf"): print(-1)
else: print(res)

Submission status: Wrong Answer

Solution code:
N,M = map(int,input().split(' '))

tree_list = [[] for _ in range(N) ]
for _ in range(M):
    L,R,C = map(int,input().split(' '))
    tree_list[R-1].append([C,L-1])
dp = [1000000000000] * N
dp[N-1] = 0
current_num = -1
flag = False
if len(tree_list[N-1]) > 0: 
    for i in reversed(range(N)):
        current_start_paths = tree_list[i]
        if len(current_start_paths) > 0:
            current_start_paths.sort()
            for path in current_start_paths:
                if dp[path[1]] > dp[i] + path[0]:
                    for j in range(path[1],i):
                        if (dp[i] + path[0]) < dp[j]:
                            dp[j] = dp[i] + path[0]
                        else:
                            break
    if dp[0] == 1000000000000:
        print(-1)
    else:
        print(dp[0])
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(dijkstra_result[n-1])

else:
    print(-1)

Submission status: Wrong Answer

Solution code:
N,M = map(int,input().split(' '))

tree_list = [[] for _ in range(N) ]
LRC = sorted([list(map(int, input().split())) for i in range(M)], key=lambda x: (x[0], x[1]))
dp = [1000000000000] * N
dp[0] = 0
flag = False
if LRC[0][0] != 1:
    print(-1) 
else:
    for l, r, c in LRC:
        l = l -1
        r = r -1
        if dp[l] == 1000000000000:
            flag = True
            break
        if dp[r] > dp[l] + c:
            for i in reversed(range(l+1,r+1)):
                if dp[i] > dp[l] + c:
                    dp[i] = dp[l] + c
                else:
                    break 
    if dp[N-1] == 1000000000000 or flag:
        print(-1)
    else:
        print(dp[N-1])
'''
for _ in range(M):
    L,R,C = map(int,input().split(' '))
    tree_list[R-1].append([C,L-1])
dp = [1000000000000] * N
dp[N-1] = 0
current_num = -1
flag = False
if len(tree_list[N-1]) > 0: 
    for i in reversed(range(N)):
        if dp[i] == 1000000000000:
            flag = True
            break
        current_start_paths = tree_list[i]
        if len(current_start_paths) > 0:
            current_start_paths.sort()
            for path in current_start_paths:
                if dp[path[1]] > dp[i] + path[0]:
                    for j in range(path[1],i):
                        if (dp[i] + path[0]) < dp[j]:
                            dp[j] = dp[i] + path[0]
                        else:
                            break
    if dp[0] == 1000000000000 or flag:
        print(-1)
    else:
        print(dp[0])
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(dijkstra_result[-1])

else:
    print(-1)

Submission status: Wrong Answer

Solution code:
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(dijkstra_result[-1])

else:
    print(-1)