목차

Bad Cowtractors

ps
링크acmicpc.net/…
출처BOJ
문제 번호7044
문제명Bad Cowtractors
레벨골드 4
분류

최소 신장 트리

시간복잡도O(ElogV)
인풋사이즈E<=20,000, V<=1,000
사용한 언어Python
제출기록33040KB / 116ms
최고기록108ms
해결날짜2022/10/13

풀이

코드

"""Solution code for "BOJ 7044. Bad Cowtractors".

- Problem link: https://www.acmicpc.net/problem/7044
- Solution link: http://www.teferi.net/ps/problems/boj/7044

Tags: [Minimum spanning tree]
"""

import sys
from teflib import graph as tgraph


def main():
    N, M = [int(x) for x in sys.stdin.readline().split()]
    graph_edges = []
    for _ in range(M):
        A, B, C = [int(x) for x in sys.stdin.readline().split()]
        graph_edges.append((A - 1, B - 1, -C))

    try:
        mst_edges = tgraph.minimum_spanning_tree(graph_edges, N)
        print(-sum(w for u, v, w in mst_edges))
    except ValueError:
        print('-1')


if __name__ == '__main__':
    main()