-
Notifications
You must be signed in to change notification settings - Fork 858
/
PrimsMSTSpecialSubtree.java
200 lines (168 loc) · 3.83 KB
/
PrimsMSTSpecialSubtree.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/**
*
* Problem Statement-
* [Prim's (MST) : Special Subtree](https://www.hackerrank.com/challenges/primsmstsub/problem)
*
*/
package com.javaaid.hackerrank.solutions.algorithms.graphtheory;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Scanner;
/**
* @author Kanahaiya Gupta
*
*/
public class PrimsMSTSpecialSubtree {
static class EdgeWeightedGraph {
private final int V;
private int E;
private final ArrayList<Edge>[] adj;
@SuppressWarnings("unchecked")
public EdgeWeightedGraph(int V) {
super();
this.V = V;
this.E = 0;
this.adj = (ArrayList<Edge>[]) new ArrayList[V + 1];
for (int v = 1; v <= V; v++)
adj[v] = new ArrayList<Edge>();
}
public EdgeWeightedGraph(Scanner sc) {
this(sc.nextInt());
int E = sc.nextInt();
for (int i = 0; i < E; i++) {
int v = sc.nextInt();
int w = sc.nextInt();
double weight = sc.nextDouble();
Edge e = new Edge(v, w, weight);
addEdge(e);
}
}
public void addEdge(Edge e) {
int v = e.either();
int w = e.other(v);
adj[v].add(e);
adj[w].add(e);
E++;
}
public Iterable<Edge> adj(int v) {
return adj[v];
}
public int V() {
return V;
}
public Iterable<Edge> edges() {
ArrayList<Edge> edges = new ArrayList<Edge>();
for (int v = 1; v <= V; v++) {
for (Edge e : adj[v]) {
if (e.other(v) > v)
edges.add(e);
}
}
return edges;
}
@Override
public String toString() {
StringBuilder s = new StringBuilder();
s.append(V + " " + E + "\n");
for (int v = 0; v < V; v++) {
s.append(v + ":");
for (Edge e : adj[v]) {
s.append(e + " ");
}
s.append("\n");
}
return s.toString();
}
}
static class Edge implements Comparable<Edge> {
private final int v, w;
private final double weight;
public Edge(int v, int w, double weight) {
super();
this.v = v;
this.w = w;
this.weight = weight;
}
public int either() {
return v;
}
public int other(int vertex) {
if (vertex == v)
return w;
return v;
}
public int compareTo(Edge that) {
if (this.weight > that.weight)
return 1;
if (this.weight < that.weight)
return -1;
return 0;
}
public double weight() {
return weight;
}
@Override
public String toString() {
return String.format("%d-%d %.5f", v, w, weight);
}
}
static class LazyPrimMST {
private boolean[] marked;
private Queue<Edge> mst;
private PriorityQueue<Edge> pq;
private double weight;
public LazyPrimMST(EdgeWeightedGraph G) {
super();
pq = new PriorityQueue<Edge>();
mst = new LinkedList<Edge>();
marked = new boolean[G.V() + 1];
for (int v = 1; v <= G.V(); v++)
if (!marked[v])
prim(G, v);
}
/**
* @param g
* @param v
*/
private void prim(EdgeWeightedGraph G, int s) {
scan(G, s);
while (!pq.isEmpty()) {
Edge e = pq.poll();
int v = e.either();
int w = e.other(v);
if (marked[v] && marked[w])
continue;
mst.add(e);
weight += e.weight();
if (!marked[v])
scan(G, v);
if (!marked[w])
scan(G, w);
}
}
/**
* @param g
* @param s
*/
private void scan(EdgeWeightedGraph G, int v) {
marked[v] = true;
for (Edge e : G.adj(v))
if (!marked[e.other(v)])
pq.add(e);
}
public Iterable<Edge> edges() {
return mst;
}
public double weight() {
return weight;
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
EdgeWeightedGraph G = new EdgeWeightedGraph(sc);
LazyPrimMST mst = new LazyPrimMST(G);
System.out.println((int) mst.weight());
}
}