From 051e59c10b423c5376de2613a15a924280ab2b1b Mon Sep 17 00:00:00 2001 From: seongwon030 <105052068+seongwon030@users.noreply.github.com> Date: Sun, 14 Jul 2024 16:55:32 +0900 Subject: [PATCH] =?UTF-8?q?2024-7-14=20=EC=A0=84=EB=A0=A5=EB=82=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../\354\240\204\353\240\245\353\202\234.py" | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 "seongwon030/\354\265\234\354\206\214\354\212\244\355\214\250\353\213\235\355\212\270\353\246\254/\354\240\204\353\240\245\353\202\234.py" diff --git "a/seongwon030/\354\265\234\354\206\214\354\212\244\355\214\250\353\213\235\355\212\270\353\246\254/\354\240\204\353\240\245\353\202\234.py" "b/seongwon030/\354\265\234\354\206\214\354\212\244\355\214\250\353\213\235\355\212\270\353\246\254/\354\240\204\353\240\245\353\202\234.py" new file mode 100644 index 0000000..85d87b4 --- /dev/null +++ "b/seongwon030/\354\265\234\354\206\214\354\212\244\355\214\250\353\213\235\355\212\270\353\246\254/\354\240\204\353\240\245\353\202\234.py" @@ -0,0 +1,35 @@ +import sys +input = sys.stdin.readline + +def find(x): + if x!=root[x]: + root[x] = find(root[x]) + return root[x] + + +while True: + V,E = map(int,input().split()) + if V==0 and E==0: + break + root = [i for i in range(V+1)] + edge = [] # 간선리스트 + for i in range(E): + a,b,c = map(int,input().split()) + edge.append((a,b,c)) + + # 비용을 기준으로 오름차순 + edge.sort(key=lambda x:x[2]) + + ans = 0 + for a,b,c in edge: + aRoot = find(a) + bRoot = find(b) + if aRoot != bRoot: + if aRoot < bRoot: + root[bRoot] = aRoot + else: + root[aRoot] = bRoot + else: + ans+=c + + print(ans) \ No newline at end of file