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()