結果

問題 No.748 yuki国のお財布事情
ユーザー titiatitia
提出日時 2019-04-20 06:08:56
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 725 ms / 2,000 ms
コード長 1,258 bytes
コンパイル時間 297 ms
コンパイル使用メモリ 81,736 KB
実行使用メモリ 111,092 KB
最終ジャッジ日時 2023-10-25 06:14:51
合計ジャッジ時間 8,603 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
55,672 KB
testcase_01 AC 41 ms
55,672 KB
testcase_02 AC 42 ms
55,672 KB
testcase_03 AC 41 ms
55,672 KB
testcase_04 AC 41 ms
55,672 KB
testcase_05 AC 41 ms
55,672 KB
testcase_06 AC 42 ms
55,672 KB
testcase_07 AC 42 ms
55,672 KB
testcase_08 AC 42 ms
55,672 KB
testcase_09 AC 42 ms
55,672 KB
testcase_10 AC 42 ms
55,672 KB
testcase_11 AC 41 ms
55,672 KB
testcase_12 AC 42 ms
55,672 KB
testcase_13 AC 158 ms
79,284 KB
testcase_14 AC 230 ms
81,992 KB
testcase_15 AC 156 ms
79,360 KB
testcase_16 AC 290 ms
83,640 KB
testcase_17 AC 636 ms
110,144 KB
testcase_18 AC 599 ms
99,132 KB
testcase_19 AC 535 ms
96,160 KB
testcase_20 AC 469 ms
93,024 KB
testcase_21 AC 725 ms
105,492 KB
testcase_22 AC 44 ms
55,672 KB
testcase_23 AC 44 ms
55,672 KB
testcase_24 AC 43 ms
55,672 KB
testcase_25 AC 453 ms
108,376 KB
testcase_26 AC 676 ms
110,844 KB
testcase_27 AC 659 ms
111,092 KB
testcase_28 AC 308 ms
85,864 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

N,M,K=map(int,input().split())
ROAD=[list(map(int,input().split())) for i in range(M)]
exc=[int(input()) for i in range(K)]

S=[0 for i in range(M+1)]
for e in exc:
    S[e]=1

SAMELIST=[i for i in range(N+1)]
def find(a):
    while SAMELIST[a]!=a:
        a=SAMELIST[a]
    return a

def Union(x,y):
    if find(x)!=find(y):
        SAMELIST[find(x)]=min(find(y),find(x))
        SAMELIST[find(y)]=min(find(y),find(x))
        SAMELIST[x]=min(find(y),find(x))
        SAMELIST[y]=min(find(y),find(x))

for e in exc:
    Union(ROAD[e-1][0],ROAD[e-1][1])

from collections import defaultdict
EDGE=defaultdict(int)

ANS=0
for i in range(M):
    a,b,c=ROAD[i]
    if a>b:
        a,b=b,a
    if find(a)==find(b) and S[i+1]!=1:
        ANS+=c
        continue
    elif find(a)==find(b):
        continue
    
    elif EDGE[find(a),find(b)]!=0:
        if EDGE[find(a),find(b)]>c:
            ANS+=EDGE[find(a),find(b)]
            EDGE[find(a),find(b)]=c
        else:
            ANS+=c
    else:
        EDGE[find(a),find(b)]=c

EDGE=sorted(list(EDGE.items()),key=lambda x:x[1])

from collections import deque
QUE=deque(EDGE)

while QUE:
    (a,b),cost=QUE.popleft()
    if find(a)==find(b):
        ANS+=cost
    else:
        Union(a,b)

print(ANS)
    
    
0