题意
就是用prim algorithm求出最小生成树的total weight
idea
使用邻接链表储存邻接节点。
1.首先将第一个节点加入最小生成树的节点集合A中,这里第一个加入的节点可以随意,不必是图最短边的节点。用TotalCost储存与集合A相邻的顶点的最小权值,0表示该结点已经在MST中,如果没有边连接的话,就是infinite。
2. 对于剩下的N-1个节点,就是循环N-1次,每一次找到与集合A相邻的但不在集合A中的权重最小的点nVertex,将它加入到集合A中,同时更新totalCoast。
3. 每一次循环找到的最小权值累加起来就是答案
code
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
AReader in = new AReader();
AWriter output = new AWriter();
int n=in.nextInt();
int m=in.nextInt();
// AbstractMap.SimpleEntry<Integer, Integer> entry= new AbstractMap.SimpleEntry<>(1,1);
ArrayList<node>[]adjacency=new ArrayList[n];
for (int i = 0; i < n; i++) {
adjacency[i]=new ArrayList();
}
for (int i = 0; i < m; i++) {
int u=in.nextInt();
int v=in.nextInt();
int w=in.nextInt();
adjacency[u-1].add(new node(v-1,w));
adjacency[v-1].add(new node(u-1,w));
}
int result=Prim(n,adjacency);
output.println(result);
output.close();
}
public static int Prim(int N,ArrayList<node>[] graph){
int nCost = 0;
ArrayList<Integer> MSTSet=new ArrayList();// 储存MST的结点
int[] TotalCost=new int[N];// 储存与集合A相邻的顶点的最小权值,0表示该结点已经在MST中
MSTSet.add(0);// 将结点1加入MST
TotalCost[0] = 0;
for(int i = 1; i <N; i++)// 初始化,切记要将除1以外的都置为INF
{
TotalCost[i] = Integer.MAX_VALUE;
}
for(int i = 0; i < graph[0].size(); i++)// 处理与结点1相连的顶点
{
TotalCost[graph[0].get(i).vertex] = graph[0].get(i).weight;
}
for(int i = 1; i <= N - 1; i++)// 剩余N-1个顶点,循环N-1次
{
int nVertex = 0, nWeight = Integer.MAX_VALUE;// 用于寻找最短的边
for(int j = 0; j < N; j++)
{
if(nWeight > TotalCost[j] && TotalCost[j] != 0)
{
nVertex = j;
nWeight = TotalCost[j];
}
}
TotalCost[nVertex] = 0;
MSTSet.add(nVertex);// 将节点nVertex加入MST
nCost += nWeight;// 计算MST的费用
for(int j = 0; j < graph[nVertex].size(); j++)// 更新pCost数组
{
if(TotalCost[graph[nVertex].get(j).vertex] != 0 &&
TotalCost[graph[nVertex].get(j).vertex] > graph[nVertex].get(j).weight)
{
TotalCost[graph[nVertex].get(j).vertex]= graph[nVertex].get(j).weight;
}
}
}
return nCost;
// System.out.println(nCost);
}
}
class node{
int vertex;
int weight;
public node(int vertex, int weight) {
this.vertex = vertex;
this.weight = weight;
}
}
class AReader {
private BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
private StringTokenizer tokenizer = new StringTokenizer("");
private String innerNextLine() {
try {
return reader.readLine();
} catch (IOException ex) {
return null;
}
}
public boolean hasNext() {
while (!tokenizer.hasMoreTokens()) {
String nextLine = innerNextLine();
if (nextLine == null) {
return false;
}
tokenizer = new StringTokenizer(nextLine);
}
return true;
}
public String nextLine() {
tokenizer = new StringTokenizer("");
return innerNextLine();
}
public String next() {
hasNext();
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
class AWriter implements Closeable {
private BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
public void print(Object object) throws IOException {
writer.write(object.toString());
}
public void println(Object object) throws IOException {
writer.write(object.toString());
writer.write("\n");
}
@Override
public void close() throws IOException {
writer.close();
}
}