P 算法与 K 算法( 二 )

<>();edges = new ArrayList<>();}}public static class Edge {public int weight;public Node from;public Node to;public Edge(int weight, Node from, Node to) {this.weight = weight;this.from = from;this.to = to;}}public static void main(String[] args) {Scanner in = new Scanner(System.in);int n = in.nextInt();int m = in.nextInt();Graph graph = new Graph(n);for (int i = 0; i < m; i++) {int from = in.nextInt();int to = in.nextInt();int weight = in.nextInt();if (!graph.nodes.containsKey(from)) {graph.nodes.put(from, new Node(from));}if (!graph.nodes.containsKey(to)) {graph.nodes.put(to, new Node(to));}Node fromNode = graph.nodes.get(from);Node toNode = graph.nodes.get(to);Edge fromToEdge = new Edge(weight, fromNode, toNode);Edge toFromEdge = new Edge(weight, toNode, fromNode);fromNode.nexts.add(toNode);fromNode.out++;fromNode.in++;toNode.out++;toNode.in++;fromNode.edges.add(fromToEdge);toNode.edges.add(toFromEdge);graph.edges.add(fromToEdge);graph.edges.add(toFromEdge);}Set result = P(graph);int sum = 0;for (Edge edge : result) {sum += edge.weight;}System.out.println(sum);in.close();}}更多算法和数据结构笔记
参考资料算法和数据结构体系班-左程云

经验总结扩展阅读