关注
第二题:迪杰斯特拉算法,在创建图的时候卡了挺久的。。 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();
}
}
}
查看原帖
点赞 评论
相关推荐
点赞 评论 收藏
分享
牛客热帖
更多
正在热议
更多
# 哪些公司在招寒假实习? #
11124次浏览 139人参与
# 你怎么看待AI面试 #
133079次浏览 742人参与
# MiniMax求职进展汇总 #
586次浏览 23人参与
# 26年哪些行业会变好/更差 #
16420次浏览 223人参与
# 找工作时的取与舍 #
114958次浏览 847人参与
# 去年的flag与今年的小目标 #
8093次浏览 175人参与
# 卷__卷不过你们,只能卷__了 #
9687次浏览 225人参与
# 写论文的崩溃时刻 #
4966次浏览 128人参与
# 腾讯音乐求职进展汇总 #
147486次浏览 1048人参与
# 关于春招你都做了哪些准备? #
122019次浏览 704人参与
# 晒一晒你收到的礼盒 #
95070次浏览 461人参与
# 你不能接受的企业文化有哪些 #
9922次浏览 153人参与
# 有深度的简历长什么样? #
14792次浏览 312人参与
# 求职你最看重什么? #
150726次浏览 875人参与
# 入职第一天 #
8894次浏览 196人参与
# 你都用AI做什么 #
5869次浏览 143人参与
# 你觉得第一学历对求职有影响吗? #
219740次浏览 1226人参与
# 机械人求职现状 #
31631次浏览 292人参与
# 现在前端的就业环境真的很差吗 #
491354次浏览 5957人参与
# 聊聊你的职场新体验 #
310576次浏览 1838人参与
# 工作丧失热情的瞬间 #
346776次浏览 2518人参与
京东公司氛围 301人发布