-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🔀merge: [Week 23] BOJ 시뮬레이션 - 3문제 완료 (#84)
* ✨feat: [BOJ] 01_1197_최소스패닝트리 * ✨feat: [BOJ] 02_1922_네트워크연결 * ✨feat: [BOJ] 03_21924_도시걸설
- Loading branch information
1 parent
311299d
commit fdd8016
Showing
3 changed files
with
126 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import sys | ||
|
||
|
||
def findParent(parent, x): | ||
if parent[x] != x: | ||
parent[x] = findParent(parent, parent[x]) | ||
return parent[x] | ||
|
||
|
||
def unionParent(parent, A, B): | ||
A = findParent(parent, A) | ||
B = findParent(parent, B) | ||
if A < B: | ||
parent[B] = A | ||
else: | ||
parent[A] = B | ||
|
||
|
||
def spanningCheck(tree, V): | ||
tree.sort(key=lambda x: x[2]) | ||
parent = [i for i in range(V + 1)] | ||
costSum = 0 | ||
|
||
for A, B, C in tree: | ||
if findParent(parent, A) != findParent(parent, B): | ||
unionParent(parent, A, B) | ||
costSum += C | ||
|
||
return costSum | ||
|
||
|
||
v, e = map(int, sys.stdin.readline().split()) | ||
|
||
spanningTree = [] | ||
for _ in range(e): | ||
a, b, c = map(int, sys.stdin.readline().split()) | ||
spanningTree.append([a, b, c]) | ||
|
||
print(spanningCheck(spanningTree, v)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import sys | ||
|
||
|
||
def findParent(parent, x): | ||
if parent[x] != x: | ||
parent[x] = findParent(parent, parent[x]) | ||
return parent[x] | ||
|
||
|
||
def unionParent(parent, A, B): | ||
A = findParent(parent, A) | ||
B = findParent(parent, B) | ||
if A < B: | ||
parent[B] = A | ||
else: | ||
parent[A] = B | ||
|
||
|
||
def spanningCheck(tree, V): | ||
tree.sort(key=lambda x: x[2]) | ||
parent = [i for i in range(V + 1)] | ||
costSum = 0 | ||
|
||
for A, B, C in tree: | ||
if findParent(parent, A) != findParent(parent, B): | ||
unionParent(parent, A, B) | ||
costSum += C | ||
|
||
return costSum | ||
|
||
|
||
n = int(sys.stdin.readline()) | ||
m = int(sys.stdin.readline()) | ||
|
||
sT = [] | ||
for _ in range(m): | ||
a, b, c = map(int, sys.stdin.readline().split()) | ||
sT.append([a, b, c]) | ||
|
||
print(spanningCheck(sT, n)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import sys | ||
|
||
|
||
def findParent(parent, x): | ||
if parent[x] != x: | ||
parent[x] = findParent(parent, parent[x]) | ||
return parent[x] | ||
|
||
|
||
def unionParent(parent, A, B): | ||
A = findParent(parent, A) | ||
B = findParent(parent, B) | ||
if A < B: | ||
parent[B] = A | ||
else: | ||
parent[A] = B | ||
|
||
|
||
n, m = map(int, sys.stdin.readline().split()) | ||
|
||
sT = [] | ||
totalCost = 0 | ||
for _ in range(m): | ||
a, b, c = map(int, sys.stdin.readline().split()) | ||
sT.append([a, b, c]) | ||
totalCost += c | ||
|
||
sT.sort(key=lambda x: x[2]) | ||
parent = [i for i in range(n + 1)] | ||
costSum = 0 | ||
for A, B, C in sT: | ||
if findParent(parent, A) != findParent(parent, B): | ||
unionParent(parent, A, B) | ||
costSum += C | ||
|
||
tempSet = set(parent[1:]) | ||
tempSet.remove(1) | ||
flag = 0 | ||
for x in tempSet: | ||
if parent[x] != 1: | ||
flag = 1 | ||
break | ||
|
||
if flag == 1: | ||
print(-1) | ||
else: | ||
print(totalCost - costSum) |