Submission #72405429


Source Code Expand

import sys
import string
import math
import bisect
import os
import heapq
import operator
from io import BytesIO, IOBase
from heapq import heappop,heappush
from functools import lru_cache,cache
from copy import copy,deepcopy
from collections import deque,defaultdict,Counter
from itertools import permutations,combinations
from array import array


INF = float('inf')
BUFSIZE = 8192

class FastIO(IOBase):
    newlines = 0
 
    def __init__(self, file):
        self._file = file
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None
 
    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()
 
    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()
 
    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)
 
class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")
 
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = sys.stdin.buffer.readline

ask = lambda *x:print('?',*x,flush=True)
reply = lambda *x:print('!',*x,flush=True)

RI = lambda: int(sys.stdin.readline())
RF = lambda: float(sys.stdin.readline())
RS = lambda: sys.stdin.readline().strip()
RFF = lambda: map(float, sys.stdin.readline().split())
RII = lambda: map(int, sys.stdin.readline().split())
RSS = lambda: map(str, sys.stdin.readline().strip().split())
RIL = lambda: list(RII())
RFL = lambda: list(RFF())
RSL = lambda: list(RSS())

from types import GeneratorType
def bootstrap(f, stack=[]):
    def wrappedfunc(*args, **kwargs):
        if stack:
            return f(*args, **kwargs)
        else:
            to = f(*args, **kwargs)
            while True:
                if type(to) is GeneratorType:
                    stack.append(to)
                    to = next(to)
                else:
                    stack.pop()
                    if not stack:
                        break 
                    to = stack[-1].send(to)
            return to

    return wrappedfunc


def main():
    n,m = RII()
    
    a = []
    
    ans = 0
    g = [[]for i in range(n+1)]
    
    en,st,val = RII()
    ans += val
    
    for i in range(m-1):
        x,y,z = RII()
        g[x].append((y,z))
        

    def dijkstra(g,st,st_dist=0):
        h = []
        n = len(g)
        dist = [INF]*n
        dist[st] = st_dist
        heappush(h,dist[st]*n+st)
        while h:
            val = heappop(h)
            d = val//n
            p = val%n
            if d>dist[p]:
                continue
            for i,x in g[p]:
                if x+d<dist[i]:
                    dist[i] = x+d
                    heappush(h,(x+d)*n+i)
        return dist
    
    d = dijkstra(g,st,0)
    
    ans += d[en]
    
    if ans==INF:
        print(-1)
        return
    
    print(ans)

    
if __name__ == '__main__':
    main()

                

Submission Info

Submission Time
Task B - Stones on Grid
User x3x3
Language Python (PyPy 3.11-v7.3.20)
Score 500
Code Size 4052 Byte
Status AC
Exec Time 364 ms
Memory 143672 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 500 / 500
Status
AC × 3
AC × 41
Set Name Test Cases
Sample 00_sample_01.txt, 00_sample_02.txt, 00_sample_03.txt
All 00_sample_01.txt, 00_sample_02.txt, 00_sample_03.txt, 01_min_01.txt, 01_min_02.txt, 01_min_03.txt, 02_random_01.txt, 02_random_02.txt, 02_random_03.txt, 02_random_04.txt, 02_random_05.txt, 03_max_rand_01.txt, 03_max_rand_02.txt, 03_max_rand_03.txt, 03_max_rand_04.txt, 03_max_rand_05.txt, 03_max_rand_06.txt, 03_max_rand_07.txt, 03_max_rand_08.txt, 03_max_rand_09.txt, 03_max_rand_10.txt, 04_continue_01.txt, 04_continue_02.txt, 04_continue_03.txt, 04_continue_04.txt, 04_continue_05.txt, 05_heap_01.txt, 05_heap_02.txt, 05_heap_03.txt, 05_heap_04.txt, 05_heap_05.txt, 06_path_01.txt, 06_path_02.txt, 06_path_03.txt, 06_path_04.txt, 06_path_05.txt, 07_m_small_01.txt, 07_m_small_02.txt, 07_m_small_03.txt, 07_m_small_04.txt, 07_m_small_05.txt
Case Name Status Exec Time Memory
00_sample_01.txt AC 116 ms 110072 KiB
00_sample_02.txt AC 115 ms 110468 KiB
00_sample_03.txt AC 114 ms 110244 KiB
01_min_01.txt AC 115 ms 110488 KiB
01_min_02.txt AC 114 ms 110328 KiB
01_min_03.txt AC 115 ms 110408 KiB
02_random_01.txt AC 141 ms 123684 KiB
02_random_02.txt AC 158 ms 122764 KiB
02_random_03.txt AC 229 ms 124220 KiB
02_random_04.txt AC 267 ms 130572 KiB
02_random_05.txt AC 257 ms 127268 KiB
03_max_rand_01.txt AC 188 ms 123380 KiB
03_max_rand_02.txt AC 197 ms 123912 KiB
03_max_rand_03.txt AC 191 ms 123880 KiB
03_max_rand_04.txt AC 197 ms 123900 KiB
03_max_rand_05.txt AC 191 ms 123700 KiB
03_max_rand_06.txt AC 178 ms 123212 KiB
03_max_rand_07.txt AC 186 ms 123892 KiB
03_max_rand_08.txt AC 186 ms 123708 KiB
03_max_rand_09.txt AC 185 ms 123396 KiB
03_max_rand_10.txt AC 187 ms 123624 KiB
04_continue_01.txt AC 251 ms 141984 KiB
04_continue_02.txt AC 255 ms 141872 KiB
04_continue_03.txt AC 258 ms 142988 KiB
04_continue_04.txt AC 248 ms 142308 KiB
04_continue_05.txt AC 254 ms 142688 KiB
05_heap_01.txt AC 345 ms 141580 KiB
05_heap_02.txt AC 364 ms 141696 KiB
05_heap_03.txt AC 333 ms 140484 KiB
05_heap_04.txt AC 355 ms 141676 KiB
05_heap_05.txt AC 339 ms 141664 KiB
06_path_01.txt AC 181 ms 143672 KiB
06_path_02.txt AC 147 ms 116864 KiB
06_path_03.txt AC 181 ms 143660 KiB
06_path_04.txt AC 146 ms 116936 KiB
06_path_05.txt AC 179 ms 143484 KiB
07_m_small_01.txt AC 168 ms 125400 KiB
07_m_small_02.txt AC 140 ms 119144 KiB
07_m_small_03.txt AC 126 ms 114300 KiB
07_m_small_04.txt AC 151 ms 126132 KiB
07_m_small_05.txt AC 147 ms 120068 KiB