init
This commit is contained in:
commit
851751ea87
82 changed files with 2093 additions and 0 deletions
41
4C.py
Normal file
41
4C.py
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
import sys
|
||||
|
||||
data = sys.stdin.read().split()
|
||||
n, m = int(data[0]), int(data[1])
|
||||
edges = []
|
||||
idx = 2
|
||||
for i in range(m):
|
||||
edges.append([int(data[idx+2]), int(data[idx]), int(data[idx+1]), i])
|
||||
idx += 3
|
||||
edges.sort()
|
||||
def find(i, p):
|
||||
if p[i] == i: return i
|
||||
p[i] = find(p[i], p)
|
||||
return p[i]
|
||||
p = list(range(n + 1))
|
||||
s1 = 0
|
||||
mst_edges = []
|
||||
for j in range(m):
|
||||
w, u, v, eid = edges[j]
|
||||
ru, rv = find(u, p), find(v, p)
|
||||
if ru != rv:
|
||||
p[ru] = rv
|
||||
s1 += w
|
||||
mst_edges.append(j)
|
||||
s2 = float('inf')
|
||||
for i in mst_edges:
|
||||
p = list(range(n + 1))
|
||||
w_tot = 0
|
||||
cnt = 0
|
||||
for j in range(m):
|
||||
if j == i:
|
||||
continue
|
||||
w, u, v, eid = edges[j]
|
||||
ru, rv = find(u, p), find(v, p)
|
||||
if ru != rv:
|
||||
p[ru] = rv
|
||||
w_tot += w
|
||||
cnt += 1
|
||||
if cnt == n - 1 and w_tot < s2:
|
||||
s2 = w_tot
|
||||
print(s1, s2)
|
||||
Loading…
Add table
Add a link
Reference in a new issue