結果

問題 No.1767 BLUE to RED
ユーザー tobusakanatobusakana
提出日時 2022-12-16 21:23:03
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,737 ms / 2,000 ms
コード長 1,040 bytes
コンパイル時間 226 ms
コンパイル使用メモリ 82,268 KB
実行使用メモリ 296,068 KB
最終ジャッジ日時 2024-04-27 20:52:17
合計ジャッジ時間 23,067 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
53,312 KB
testcase_01 AC 39 ms
53,376 KB
testcase_02 AC 39 ms
52,888 KB
testcase_03 AC 39 ms
53,420 KB
testcase_04 AC 50 ms
61,844 KB
testcase_05 AC 70 ms
71,676 KB
testcase_06 AC 47 ms
60,776 KB
testcase_07 AC 102 ms
77,284 KB
testcase_08 AC 104 ms
77,416 KB
testcase_09 AC 826 ms
206,544 KB
testcase_10 AC 1,069 ms
224,680 KB
testcase_11 AC 938 ms
211,892 KB
testcase_12 AC 886 ms
206,852 KB
testcase_13 AC 1,198 ms
264,024 KB
testcase_14 AC 1,571 ms
295,644 KB
testcase_15 AC 1,662 ms
295,792 KB
testcase_16 AC 1,542 ms
295,708 KB
testcase_17 AC 1,568 ms
295,132 KB
testcase_18 AC 1,577 ms
295,608 KB
testcase_19 AC 1,573 ms
295,968 KB
testcase_20 AC 1,603 ms
295,636 KB
testcase_21 AC 1,623 ms
296,068 KB
testcase_22 AC 1,577 ms
295,788 KB
testcase_23 AC 1,737 ms
295,876 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)
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
        hq.heappush(q, (dist, child))
        
print(ans)    







0