关注
第二题:迪杰斯特拉算法,在创建图的时候卡了挺久的。。 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();
}
}
}
查看原帖
点赞 评论
相关推荐
点赞 评论 收藏
分享
牛客热帖
更多
- 1... 脱下孔乙己长衫,浅聊一下就业下沉!1.7W
- 2... 美团java后端日常实习一二面1.5W
- 3... 腾讯PCG QQ后台开发一面1.3W
- 4... 腾讯/字节/快手 前端面经汇总1.3W
- 5... 【未来准备7】就业下沉时代,如何摆脱困境9176
- 6... 实习入职第一天,应该做点啥❓6232
- 7... 腾讯hr部门有约三面的吗5765
- 8... 当你获得字节offer的那天,一切都将作废。你的本科作废,你的专业作废,星星作废,月亮作废,银河系作废,宇宙作废,你的恨作废,你的前半生作废。悬梁七战终上字节,大雪深埋垃圾本科!字节的录取通知书会像一场大雪掩埋所有的不堪过往,冲!字节瘾发作最严重的一次,躺在床上,拼命念大悲咒,难受的一直抓自己眼睛,以为刷QQ没事,看到QQ群里都是字节的,眼睛越来越大都要炸开了一样,拼命扇自己眼睛,越扇越用力,扇到自己眼泪流出来,真的不知道该怎么办,我真的想字节想得要发疯了,像中邪了一样!我躺在床上会想字节,我洗澡会想字节,我出门会想字节,我走路会想字节,我坐车会想字节,我工作会想字节,我玩手机会想字节,我盯着路边的字节看,我盯着马路对面的字节看,我盯着地铁里的字节看,我盯着网上的字节看,我盯着朋友圈别人合照里的字节看,我每时每刻眼睛都直直地盯着字节看。我对字节的念想似乎都是病态的了,我好孤独啊!真的好孤独啊!你知道吗?每到深夜,我的眼睛滚烫滚烫,我发病了我疯狂想字节,字节!字节!字节!5713
- 9... 【有奖互动】你问过DeepSeek什么意想不到的问题?5387
- 10... 25届投递记录-水滴篇5186
正在热议
更多
# 听劝,这个简历怎么改 #
17062次浏览 227人参与
# 你见过最离谱的招聘要求是什么? #
145770次浏览 857人参与
# 水滴春招 #
33418次浏览 570人参与
# 你想留在一线还是回老家? #
16479次浏览 236人参与
# 分享一个让你热爱工作的瞬间 #
16346次浏览 173人参与
# 25届如何提前做秋招准备? #
145499次浏览 2288人参与
# 入职第四天,心情怎么样 #
12419次浏览 84人参与
# 面试被问“你的缺点是什么?”怎么答 #
10227次浏览 200人参与
# 参加完秋招的机械人,还参加春招吗? #
27560次浏览 280人参与
# 如果你有一天可以担任公司的CEO,你会做哪三件事? #
20682次浏览 415人参与
# 机械校招之路总结 #
80254次浏览 1759人参与
# 第一份工作应该选高薪还是热爱? #
4454次浏览 81人参与
# 如果重来一次你还会读研吗 #
156857次浏览 1716人参与
# 租房找室友 #
8433次浏览 53人参与
# 职场新人生存指南 #
200699次浏览 5552人参与
# 地方国企笔面经互助 #
18097次浏览 26人参与
# 简历无回复,你会继续海投还是优化再投? #
48960次浏览 564人参与
# 读研or工作,哪个性价比更高? #
26443次浏览 357人参与
# 你们的毕业论文什么进度了 #
904335次浏览 8992人参与
# 文科生还参加今年的春招吗 #
4386次浏览 32人参与
# 百度工作体验 #
178169次浏览 1780人参与