关注
第二题:迪杰斯特拉算法,在创建图的时候卡了挺久的。。 import java.util.*;
class Edge implements Comparable<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;
}
@Override
public int compareTo(Edge o) {
return this.weight - o.weight;
}
@Override
public String toString() {
return "Edge{" +
"weight=" + weight +
", from=" + from +
", to=" + to +
'}';
}
}
class Graph {
public HashMap<Integer, Node> nodes;
public HashSet<Edge> edges;
public Graph() {
nodes = new HashMap<>();
edges = new HashSet<>();
}
}
class Node {
public int value;
public int in;
public int out;
public ArrayList<Node> nexts;
public ArrayList<Edge> edges;
public Node(int value) {
this.value = value;
in = 0;
out = 0;
nexts = new ArrayList<>();
edges = new ArrayList<>();
}
@Override
public String toString() {
return "Node{" +
"value=" + value +
'}';
}
}
public class Main1 {
//记录距离树最近边edges对应的权值
static private HashMap<Node, Integer> powers;
//记录横切边
static private PriorityQueue<Edge> pq;
//记录顶点
static private Node origin;
static private Graph graph;
public static void init(Integer[][] matrix, int p, int n) {
graph = createGraph(matrix);
origin = graph.nodes.get(p);
powers = new HashMap<>(n);
pq = new PriorityQueue<>(n);
for (int i = 0; i < n; i++) {
powers.put(graph.nodes.get(i), Integer.MAX_VALUE);
}
find();
}
private static void find() {
//从起点开始
powers.put(origin, 0);
pq.offer(new Edge(-1, origin, origin));
relax(origin);
//不断放松权值最小的边
while (!pq.isEmpty()) {
Edge min = pq.poll();
relax(min.to);
}
}
//对顶点的所有邻接边检查,松弛权值比较大的顶点,修正无效边
private static void relax(Node from) {
for (Edge edge : from.edges) {
Node to = edge.to;
int newPower = powers.get(from) + edge.weight;
//经过顶点from的边的路径权值比原来更小,把to的边的起点修改为from,把到to的权值修改为更小的
if (powers.get(to) > newPower) {
powers.put(to, newPower);
if (pq.contains(edge))
pq.remove(edge);
edge.weight = newPower;
pq.offer(edge);
}
}
}
//获取到某一顶点的权值
public static int getPower(int vertex) {
Node node = graph.nodes.get(vertex);
return powers.get(node);
}
public static Graph createGraph(Integer[][] matrix) {
Graph graph = new Graph();
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
if (matrix[i][j] == -1)
continue;
Integer from = i;
Integer to = j;
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 newEdge = new Edge(matrix[i][j], fromNode, toNode);
fromNode.nexts.add(toNode);
fromNode.out++;
toNode.in++;
fromNode.edges.add(newEdge);
graph.edges.add(newEdge);
}
}
return graph;
}
public static void main(String[] args) {
// Integer[][] matrix = {
// {-1, 1, 4, -1, -1, -1},
// {1, -1, 2, 7, 5, -1},
// {4, 2, -1, -1, 1, -1},
// {-1, 7, -1, -1, 3, 2},
// {-1, 5, 1, 3, -1, 6},
// {-1, -1, -1, 2, 6, -1}
// };
// init(matrix,0,6);
// for (int i = 0; i < 6; i++) {
// if (i == 0)
// continue;
// System.out.print(getPower(i));
// if(i != 6 - 1)
// System.out.print(",");
// }
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()) {
int n = scanner.nextInt();
int p = scanner.nextInt();
Integer[][] matrix = new Integer[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
matrix[i][j] = scanner.nextInt();
}
}
init(matrix, p, n);
for (int i = 0; i < n; i++) {
if (i == p)
continue;
System.out.print(getPower(i)+",");
}
System.out.println();
}
}
}
查看原帖
点赞 评论
相关推荐
07-08 14:54
西南石油大学 电力电子工程师 点赞 评论 收藏
分享
牛客热帖
更多
正在热议
更多
# 国企是理工四大天坑的最好选择吗 #
13249次浏览 94人参与
# 如果可以,你希望哪个公司来捞你 #
99120次浏览 409人参与
# leader认为你工作不认真怎么办 #
30006次浏览 132人参与
# 如果公司给你放一天假,你会怎么度过? #
16569次浏览 127人参与
# 三一重工求职进展汇总 #
14545次浏览 66人参与
# 我的国央企投递进展 #
46041次浏览 287人参与
# 机械人,你被简历秒挂的企业有哪些? #
42497次浏览 280人参与
# 通信/硬件公司求职体验 #
123911次浏览 865人参与
# 总结:哪家公司面试体验感最差 #
60653次浏览 276人参与
# 你认为小厂实习有用吗? #
30780次浏览 349人参与
# 我和mentor的爱恨情仇 #
58201次浏览 350人参与
# 哪一瞬间觉得自己长大了 #
14562次浏览 339人参与
# 说说你知道的学历厂 #
62268次浏览 376人参与
# 你找工作的时候用AI吗? #
28325次浏览 347人参与
# 秋招最大的收获是什么? #
37017次浏览 311人参与
# 技术岗笔试题求解 #
78042次浏览 1012人参与
# OPPO求职进展汇总 #
662410次浏览 5036人参与
# 面试尴尬现场 #
46219次浏览 305人参与
# 计算机有哪些岗位值得去? #
22826次浏览 218人参与
# 下班后的时间你怎么安排 #
14169次浏览 194人参与