-
Notifications
You must be signed in to change notification settings - Fork 0
/
Connecting_cities.java
55 lines (45 loc) · 1.4 KB
/
Connecting_cities.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
package Graphs;
import java.util.PriorityQueue;
public class Connecting_cities {
public static class Edge implements Comparable<Edge> {
int dest;
int cost;
public Edge(int dest, int cost){
this.dest = dest;
this.cost = cost;
}
@Override
public int compareTo(Edge e){
return this.cost - e.cost;
}
}
public static int connectCities(int[][] cities){
PriorityQueue<Edge> pq = new PriorityQueue<>();
boolean[] vis = new boolean[cities.length];
int finalCost = 0;
pq.add(new Edge(0, 0));
while (!pq.isEmpty()){
Edge curr = pq.remove();
if (!vis[curr.dest]){
finalCost += curr.cost;
vis[curr.dest] = true;
for (int i=0; i<cities.length; i++){
if (cities[curr.dest][i] != 0){
pq.add(new Edge(i, cities[curr.dest][i]));
}
}
}
}
return finalCost;
}
public static void main(String[] args) {
int[][] cities = {
{0, 1, 2, 3, 4},
{1, 0, 5, 0, 7},
{2, 5, 0, 6, 0},
{3, 0, 6, 0, 0},
{4, 7, 0, 0, 0}
};
System.out.println(connectCities(cities));
}
}