(모든 간선을 조사한 후 MST에모든 꼭짓점이없다면, 해당 그래프에는 MST가 존재하지 않습니다.)
3. 구현
static class Edge implements Comparable<Edge> {
final int dest, weight;
public Edge(int dest, int weight) {
this.dest = dest;
this.weight = weight;
}
@Override
public int compareTo(Edge o) {
return Integer.compare(this.weight, o.weight);
}
}
public class MyPrim {
private final int INF = Integer.MAX_VALUE, NON_EXIST = 0;
private final Scanner sc;
private final PriorityQueue<Edge> pq;
private final BitSet mstV;
private final int[][] adjMatrix;
private final int[] minDists;
private final int vCnt;
public MyPrim() {
sc = new Scanner(System.in);
pq = new PriorityQueue<>();
vCnt = sc.nextInt();
adjMatrix = new int[vCnt][vCnt];
for (int i = 0; i < vCnt; i++) {
for (int j = 0; j < vCnt; j++) adjMatrix[i][j] = sc.nextInt();
}
minDists = new int[vCnt];
Arrays.fill(minDists, INF);
mstV = new BitSet(vCnt);
}
public int prim(int start) {
pq.clear();
mstV.clear();
minDists[start] = NON_EXIST;
pq.offer(new Edge(start, NON_EXIST));
int sum = 0, cnt = 0;
while (!pq.isEmpty()) {
Edge curE = pq.poll();
int dest = curE.dest, weight = curE.weight;
if (mstV.get(dest)) continue;
mstV.set(dest);
sum += weight;
if (++cnt == vCnt) break;
for (int neigh = 0; neigh < vCnt; neigh++) {
if (adjMatrix[dest][neigh] == 0) continue;
if (adjMatrix[dest][neigh] >= minDists[neigh]) continue;
minDists[neigh] = adjMatrix[dest][neigh];
pq.offer(new Edge(neigh, minDists[neigh]));
}
}
return sum;
}
}