結果

問題 No.1767 BLUE to RED
ユーザー tobusakanatobusakana
提出日時 2022-12-16 21:24:42
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,504 ms / 2,000 ms
コード長 1,149 bytes
コンパイル時間 164 ms
コンパイル使用メモリ 82,364 KB
実行使用メモリ 299,088 KB
最終ジャッジ日時 2024-04-27 20:53:42
合計ジャッジ時間 21,673 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
53,416 KB
testcase_01 AC 41 ms
54,440 KB
testcase_02 AC 42 ms
53,076 KB
testcase_03 AC 39 ms
53,000 KB
testcase_04 AC 49 ms
61,532 KB
testcase_05 AC 67 ms
70,976 KB
testcase_06 AC 45 ms
60,284 KB
testcase_07 AC 87 ms
77,604 KB
testcase_08 AC 96 ms
77,552 KB
testcase_09 AC 836 ms
202,860 KB
testcase_10 AC 989 ms
223,256 KB
testcase_11 AC 939 ms
207,672 KB
testcase_12 AC 795 ms
202,072 KB
testcase_13 AC 1,075 ms
261,868 KB
testcase_14 AC 1,455 ms
297,760 KB
testcase_15 AC 1,464 ms
297,920 KB
testcase_16 AC 1,461 ms
298,956 KB
testcase_17 AC 1,491 ms
298,684 KB
testcase_18 AC 1,441 ms
298,588 KB
testcase_19 AC 1,427 ms
298,784 KB
testcase_20 AC 1,450 ms
297,736 KB
testcase_21 AC 1,504 ms
298,016 KB
testcase_22 AC 1,453 ms
299,088 KB
testcase_23 AC 1,453 ms
298,312 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# グラフ
# 頂点0を作成し、全ての赤の頂点に対して長さ0の辺を貼る
# 全ての頂点を昇順に並べ、隣合った点に距離を表す辺を貼る
# 最小全域木

N,M = map(int,input().split())
A = list(map(int,input().split()))
B = list(map(int,input().split()))

G = [[] for i in range(N + M + 1)]
for i in range(1, N + 1):
    G[0].append([i, 0])

A = [[A[i], i + 1] for i in range(N)]
B = [[B[i], N + i + 1] for i in range(M)]
AB = A + B
AB = sorted(AB, key = lambda x:x[0])

for i in range(N + M - 1):
    dist = abs(AB[i + 1][0] - AB[i][0])
    to_v = AB[i + 1][1]
    from_v = AB[i][1]
    G[to_v].append([from_v, dist])
    G[from_v].append([to_v, dist])
    
import heapq as hq
q = []
hq.heappush(q, (0, 0))
visited = [False] * (N + M + 1)
INF = 1 << 60
best = [INF] * (N + M + 1)
ans = 0
while q:
    d, v = hq.heappop(q)
    if visited[v]:
        continue
    visited[v] = True
    ans += d
    for child, dist in G[v]:
        if visited[child]:
            continue
        if best[child] <= dist:
            continue
        best[child] = dist
        hq.heappush(q, (dist, child))
        
print(ans)
0