提出 #58203631


ソースコード 拡げる

import sys
from collections import deque
#sys.stdin = open('in.txt', 'r')
#sys.stdout = open('out.txt', 'w')
read = sys.stdin.readline
write = sys.stdout.write
def solve_graph(N, M, edges):
    adj = [[] for _ in range(N + 1)]
    for u, v, w in edges:
        adj[u].append((v, w))
        adj[v].append((u, -w))

    x = [None] * (N + 1)
    visited = [False] * (N + 1)

    def bfs(start):
        queue = deque([start])
        x[start] = 0 
        visited[start] = True
        component = []

        while queue:
            node = queue.popleft()
            current_value = x[node]
            component.append(node)

            for neighbor, weight in adj[node]:
                if not visited[neighbor]:
                    x[neighbor] = current_value + weight
                    visited[neighbor] = True
                    queue.append(neighbor)

        component_values = [x[node] for node in component]
        min_value = min(component_values)
        max_value = max(component_values)

        limit = 10**18
        if min_value < -limit or max_value > limit:
            shift = max(-min_value - limit, max_value - limit)
            for node in component:
                x[node] += shift

    for i in range(1, N + 1):
        if not visited[i]:
            bfs(i)

    return x[1:]

n, m = map(int, read().split())
edges = []
for i in range(m):
    u,v,w = map(int, read().split())
    edges.append((u,v,w))
write(' '.join(map(str, solve_graph(n,m,edges)))+'\n')

提出情報

提出日時
問題 D - Hidden Weights
ユーザ ary65537
言語 Python (PyPy 3.10-v7.3.12)
得点 400
コード長 1541 Byte
結果 AC
実行時間 338 ms
メモリ 169448 KiB

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 400 / 400
結果
AC × 3
AC × 29
セット名 テストケース
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_random_01.txt, 01_random_02.txt, 01_random_03.txt, 01_random_04.txt, 01_random_05.txt, 01_random_06.txt, 01_random_07.txt, 01_random_08.txt, 01_random_09.txt, 01_random_10.txt, 01_random_11.txt, 01_random_12.txt, 01_random_13.txt, 01_random_14.txt, 01_random_15.txt, 01_random_16.txt, 01_random_17.txt, 01_random_18.txt, 01_random_19.txt, 01_random_20.txt, 02_handmade_01.txt, 02_handmade_02.txt, 02_handmade_03.txt, 02_handmade_04.txt, 02_handmade_05.txt, 02_handmade_06.txt
ケース名 結果 実行時間 メモリ
00_sample_01.txt AC 68 ms 76816 KiB
00_sample_02.txt AC 68 ms 77092 KiB
00_sample_03.txt AC 67 ms 76824 KiB
01_random_01.txt AC 195 ms 113740 KiB
01_random_02.txt AC 302 ms 149720 KiB
01_random_03.txt AC 311 ms 150216 KiB
01_random_04.txt AC 323 ms 158192 KiB
01_random_05.txt AC 146 ms 94712 KiB
01_random_06.txt AC 261 ms 127736 KiB
01_random_07.txt AC 312 ms 155436 KiB
01_random_08.txt AC 338 ms 158180 KiB
01_random_09.txt AC 162 ms 97824 KiB
01_random_10.txt AC 300 ms 149776 KiB
01_random_11.txt AC 285 ms 147872 KiB
01_random_12.txt AC 313 ms 158372 KiB
01_random_13.txt AC 171 ms 102808 KiB
01_random_14.txt AC 329 ms 158100 KiB
01_random_15.txt AC 269 ms 146336 KiB
01_random_16.txt AC 334 ms 158512 KiB
01_random_17.txt AC 163 ms 114664 KiB
01_random_18.txt AC 268 ms 133300 KiB
01_random_19.txt AC 203 ms 128680 KiB
01_random_20.txt AC 334 ms 158460 KiB
02_handmade_01.txt AC 321 ms 167000 KiB
02_handmade_02.txt AC 318 ms 167436 KiB
02_handmade_03.txt AC 338 ms 165948 KiB
02_handmade_04.txt AC 299 ms 169448 KiB
02_handmade_05.txt AC 115 ms 98000 KiB
02_handmade_06.txt AC 223 ms 166912 KiB