結果

問題 No.748 yuki国のお財布事情
ユーザー ckawatak
提出日時 2018-12-29 22:40:22
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 841 ms / 2,000 ms
コード長 1,108 bytes
コンパイル時間 99 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 34,908 KB
最終ジャッジ日時 2024-10-02 11:51:42
合計ジャッジ時間 10,036 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

diff #

parent = []
rank = []
edges = []

def init(n):
    for i in range(n):
        parent.append(i)
        rank.append(0)

def find(x):
    if parent[x] == x:
        return x
    else:
        parent[x] = find(parent[x])
        return parent[x]
        
def unite(x, y):
    x = find(x)
    y = find(y)
    if rank[x] < rank[y]:
        parent[x] = y
    else:
        parent[y] = x
        if rank[x] == rank[y]:
            rank[x] = rank[x] + 1

def same(x, y):
    return find(x) == find(y)

def kruskal(e):
    edges.sort()
    cost = 0
    for i in range(e):
        if not same(edges[i][1], edges[i][2]):
            unite(edges[i][1], edges[i][2])
            cost = cost + edges[i][0]
    return cost

N,M,K = list(map(int, input().split(' ')))

a = [0 for _ in range(10**5)]
b = [0 for _ in range(10**5)]
c = [0 for _ in range(10**5)]

for i in range(M):
    a[i], b[i], c[i] = list(map(int, input().split(' ')))
    edges.append((c[i], a[i], b[i]))
cost = sum(c)

init(N+1)
for i in range(K):
    e = int(input())
    e = e - 1
    cost = cost - c[e]
    unite(a[e], b[e])

print(cost - kruskal(M))
0