Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add solution(63.64%) to 03188, not done yet

  • Loading branch information...
commit e2950b000a39c376b982ae3701080b5352318704 1 parent 8444f5e
@mingyc authored
Showing with 58 additions and 0 deletions.
  1. +23 −0 03188_MST/MST.py
  2. +29 −0 03188_MST/README.md
  3. +6 −0 03188_MST/input
View
23 03188_MST/MST.py
@@ -0,0 +1,23 @@
+from sys import stdin
+
+N, M = map(int, stdin.readline().split())
+graph = sorted([map(int, line.split()[::-1]) for line in stdin ])
+
+mst_weight = 0
+node = [0]*(N+1)
+group = 1
+for edge in graph: # Kruskal's algorithm
+ w, u, v = edge[0], edge[2], edge[1]
+ if not node[u] and not node[v]:
+ node[u], node[v], group = group, group, group+1
+
+ elif not node[u] or not node[v]:
+ node[u], node[v] = (node[v], node[v]) if not node[u] else (node[u], node[u])
+ elif node[u] != node[v]:
+ group_u, group_v = node[u], node[v]
+ node = [group_u if (g == group_u or g == group_v) else g for g in node ]
+ else:
+ continue
+ mst_weight += w
+
+print mst_weight
View
29 03188_MST/README.md
@@ -0,0 +1,29 @@
+3188\. Minimum Spanning Tree
+============================
+
+## Problem code: MST
+
+Find the minimum spanning tree of the graph.
+
+## Input
+
+On the first line there will be two integers N - the number of nodes and M - the number of edges. (1 <= N <= 10000), (1 <= M <= 100000)
+M lines follow with three integers i j k on each line representing an edge between node i and j with weight k. The IDs of the nodes are between 1 and n inclusive. The weight of each edge will be <= 1000000.
+
+## Output
+
+Single number representing the total weight of the minimum spanning tree on this graph. There will be only one possible MST.
+
+## Example
+
+### Input:
+4 5
+1 2 10
+2 3 15
+1 3 5
+4 2 2
+4 3 40
+
+### Output:
+17
+
View
6 03188_MST/input
@@ -0,0 +1,6 @@
+4 5
+1 2 10
+2 3 15
+1 3 5
+4 2 2
+4 3 40
Please sign in to comment.
Something went wrong with that request. Please try again.