-
Notifications
You must be signed in to change notification settings - Fork 104
/
Solution.java
265 lines (203 loc) · 7.15 KB
/
Solution.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/// Leetcode 1168. Optimize Water Distribution in a Village
/// https://leetcode.com/problems/optimize-water-distribution-in-a-village/
///
/// 课程中在这里暂时没有介绍这个问题
/// 该代码主要用于使用 Leetcode 上的问题测试我们的 Prim 算法类
import java.util.*;
/// Prim Optimized
class Solution {
public class WeightedGraph implements Cloneable{
private int V;
private int E;
private TreeMap<Integer, Integer>[] adj;
public WeightedGraph(int V){
this.V = V;
adj = new TreeMap[V];
for(int i = 0; i < V; i ++)
adj[i] = new TreeMap<Integer, Integer>();
E = 0;
}
public void addEdge(int v, int w, int weight){
if(v == w)
throw new IllegalArgumentException("Self Loop is Detected!");
validateVertex(v);
validateVertex(w);
if(adj[v].containsKey(w)){
int newWeight = Math.min(weight, adj[v].get(w));
adj[v].put(w, newWeight);
adj[w].put(v, newWeight);
return;
}
adj[v].put(w, weight);
adj[w].put(v, weight);
E ++;
}
public void validateVertex(int v){
if(v < 0 || v >= V)
throw new IllegalArgumentException("vertex " + v + "is invalid");
}
public int V(){
return V;
}
public int E(){
return E;
}
public boolean hasEdge(int v, int w){
validateVertex(v);
validateVertex(w);
return adj[v].containsKey(w);
}
public int getWeight(int v, int w){
if(hasEdge(v, w)) return adj[v].get(w);
throw new IllegalArgumentException(String.format("No edge %d-%d", v, w));
}
public Iterable<Integer> adj(int v){
validateVertex(v);
return adj[v].keySet();
}
public int degree(int v){
validateVertex(v);
return adj[v].size();
}
public void removeEdge(int v, int w){
validateVertex(v);
validateVertex(w);
if(adj[v].containsKey(w)) E --;
adj[v].remove(w);
adj[w].remove(v);
}
@Override
public Object clone(){
try{
WeightedGraph cloned = (WeightedGraph) super.clone();
cloned.adj = new TreeMap[V];
for(int v = 0; v < V; v ++){
cloned.adj[v] = new TreeMap<Integer, Integer>();
for(Map.Entry<Integer, Integer> entry: adj[v].entrySet())
cloned.adj[v].put(entry.getKey(), entry.getValue());
}
return cloned;
}
catch (CloneNotSupportedException e){
e.printStackTrace();
}
return null;
}
@Override
public String toString(){
StringBuilder sb = new StringBuilder();
sb.append(String.format("V = %d, E = %d\n", V, E));
for(int v = 0; v < V; v ++){
sb.append(String.format("%d : ", v));
for(Map.Entry<Integer, Integer> entry: adj[v].entrySet())
sb.append(String.format("(%d: %d) ", entry.getKey(), entry.getValue()));
sb.append('\n');
}
return sb.toString();
}
}
public class WeightedEdge implements Comparable<WeightedEdge>{
private int v, w, weight;
public WeightedEdge(int v, int w, int weight){
this.v = v;
this.w = w;
this.weight = weight;
}
public int getV(){return v;}
public int getW(){return w;}
public int getWeight(){return weight;}
@Override
public int compareTo(WeightedEdge another){
return weight - another.weight;
}
@Override
public String toString(){
return String.format("(%d-%d: %d)", v, w, weight);
}
}
public class CC {
private WeightedGraph G;
private int[] visited;
private int cccount = 0;
public CC(WeightedGraph G){
this.G = G;
visited = new int[G.V()];
for(int i = 0; i < visited.length; i ++)
visited[i] = -1;
for(int v = 0; v < G.V(); v ++)
if(visited[v] == -1){
dfs(v, cccount);
cccount ++;
}
}
private void dfs(int v, int ccid){
visited[v] = ccid;
for(int w: G.adj(v))
if(visited[w] == -1)
dfs(w, ccid);
}
public int count(){
return cccount;
}
public boolean isConnected(int v, int w){
G.validateVertex(v);
G.validateVertex(w);
return visited[v] == visited[w];
}
public ArrayList<Integer>[] components(){
ArrayList<Integer>[] res = new ArrayList[cccount];
for(int i = 0; i < cccount; i ++)
res[i] = new ArrayList<Integer>();
for(int v = 0; v < G.V(); v ++)
res[visited[v]].add(v);
return res;
}
}
public class Prim {
private WeightedGraph G;
private ArrayList<WeightedEdge> mst;
public Prim(WeightedGraph G){
this.G = G;
mst = new ArrayList<>();
CC cc = new CC(G);
if(cc.count() > 1) return;
boolean visited[] = new boolean[G.V()];
visited[0] = true;
Queue pq = new PriorityQueue<WeightedEdge>();
for(int w: G.adj(0))
pq.add(new WeightedEdge(0, w, G.getWeight(0, w)));
while(!pq.isEmpty()){
WeightedEdge minEdge = (WeightedEdge) pq.remove();
if(visited[minEdge.getV()] && visited[minEdge.getW()])
continue;
mst.add(minEdge);
int newp = visited[minEdge.getV()] ? minEdge.getW() : minEdge.getV();
visited[newp] = true;
for(int w: G.adj(newp))
if(!visited[w])
pq.add(new WeightedEdge(newp, w, G.getWeight(newp, w)));
}
}
public ArrayList<WeightedEdge> result(){
return mst;
}
}
public int minCostToSupplyWater(int n, int[] wells, int[][] pipes) {
WeightedGraph g = new WeightedGraph(n + 1);
for(int i = 0; i < wells.length; i ++)
g.addEdge(0, i + 1, wells[i]);
for(int i = 0; i < pipes.length; i ++)
g.addEdge(pipes[i][0], pipes[i][1], pipes[i][2]);
Prim prim = new Prim(g);
ArrayList<WeightedEdge> mst = prim.result();
int res = 0;
for(WeightedEdge edge: mst)
res += edge.getWeight();
return res;
}
public static void main(String[] args){
int[] wells = {1, 2, 2};
int[][] pipes = {{1, 2, 1}, {2, 3, 1}};
System.out.println((new Solution()).minCostToSupplyWater(3, wells, pipes));
}
}