qianheshang 于 2016.03.29 12:40 提问

java中的优先队列PriorityQueue不再维护最小优先是怎么回事

2个回答

wojiushiwo945you      2016.03.29 13:32

`````` <p>The <em>head</em> of this queue is the <em>least</em> element with respect to the specified ordering.
``````
wojiushiwo945you 回复qianheshang: 我看源码了它是根据传入元素实现Comparable接口指定排序操作的。可能跟你的Node的比较实现有关。

qianheshang public static int[] dijkstra(Node[] node, Node n, int[][] e) { //n为起始点 n.d = 0; PriorityQueue<Node> pn = new PriorityQueue<Node>(); for (int i = 0; i < node.length; i++) { if (node[i] == null) { break; } pn.add(node[i]); } int[] s = new int[pn.size()]; while (pn.size() != 0) { Node u = pn.peek(); Node v = u.next; while (v != null) { relax(u, node[v.id], e[u.id][v.id]); s[v.id] = node[v.id].d; v = v.next; } pn.poll(); } return s; }

qianheshang   2016.03.29 13:59

public static int[] dijkstra(Node[] node, Node n, int[][] e) {
//n为起始点
n.d = 0;
PriorityQueue pn = new PriorityQueue();
for (int i = 0; i < node.length; i++) {
if (node[i] == null) {
break;
}
}
int[] s = new int[pn.size()];
while (pn.size() != 0) {
Node u = pn.peek();
Node v = u.next;
while (v != null) {
relax(u, node[v.id], e[u.id][v.id]);
s[v.id] = node[v.id].d;
v = v.next;
}
pn.poll();
}
return s;
}