为什么我的代码输出0作为dijkstras中的距离? import java.util.*; 公共级迪杰斯特拉酒店 { 公共静态类 { 公共字符串名称; 公开名单; 公众心理距离; 公共场所; 公共顶点(字符串argName){ name=argName; adj=新的ArrayList(); } 公共无效补遗(边缘e){ 增加(e); } 公共字符串toString(){ 返回(名称+“”); } 公共整数比较(顶点其他) { 返回Double.compare(minDistance,other.minDistance); } } 公共静态类边缘 { 公共顶点目标; 公共顶点来自; 公共权重; 公共边(顶点tar,int argWeight) {target=tar; 权重=argWeight;} } 公共静态扫描仪in=新扫描仪(System.in); 公共交通线; 公共静态无效路径(顶点源) { source.minDistance=0; PriorityQueue vertexQueue=新建PriorityQueue(); 添加(源); 而(!vertexQueue.isEmpty()){ Vertex u=vertexQueue.poll(); 用于(边缘e:u.adj) { 顶点v=e.目标; int-weight=e.weight; int distance throughu=u.minDistance+重量; if(通过HU的距离
而言,它是对系统中int的大小所做的。这是32位长的有符号int的最大值为什么我的代码输出0作为dijkstras中的距离? import java.util.*; 公共级迪杰斯特拉酒店 { 公共静态类 { 公共字符串名称; 公开名单; 公众心理距离; 公共场所; 公共顶点(字符串argName){ name=argName; adj=新的ArrayList(); } 公共无效补遗(边缘e){ 增加(e); } 公共字符串toString(){ 返回(名称+“”); } 公共整数比较(顶点其他) { 返回Double.compare(minDistance,other.minDistance); } } 公共静态类边缘 { 公共顶点目标; 公共顶点来自; 公共权重; 公共边(顶点tar,int argWeight) {target=tar; 权重=argWeight;} } 公共静态扫描仪in=新扫描仪(System.in); 公共交通线; 公共静态无效路径(顶点源) { source.minDistance=0; PriorityQueue vertexQueue=新建PriorityQueue(); 添加(源); 而(!vertexQueue.isEmpty()){ Vertex u=vertexQueue.poll(); 用于(边缘e:u.adj) { 顶点v=e.目标; int-weight=e.weight; int distance throughu=u.minDistance+重量; if(通过HU的距离,java,integer,dijkstra,Java,Integer,Dijkstra,而言,它是对系统中int的大小所做的。这是32位长的有符号int的最大值 请参见注意,2147483647是0x7FFFFFFF是Integer.MAX_VALUE。请给出您的问题示例。例如,如果问题不是关于从文件加载数据,那么与此相关的任何代码都不应该在您的问题中。 import java.util.*; public class Dijkstra { public static class Vertex implements Comparable<Vertex> {
请参见注意,
2147483647
是0x7FFFFFFF
是Integer.MAX_VALUE
。请给出您的问题示例。例如,如果问题不是关于从文件加载数据,那么与此相关的任何代码都不应该在您的问题中。
import java.util.*;
public class Dijkstra
{
public static class Vertex implements Comparable<Vertex>
{
public String name;
public List<Edge> adj;
public int minDistance;
public Vertex previous;
public Vertex(String argName) {
name = argName;
adj = new ArrayList<Edge>();
}
public void addEdge(Edge e) {
adj.add(e);
}
public String toString() {
return (name+"");
}
public int compareTo(Vertex other)
{
return Double.compare(minDistance, other.minDistance);
}
}
public static class Edge
{
public Vertex target;
public Vertex from;
public int weight;
public Edge(Vertex tar, int argWeight)
{ target = tar;
weight = argWeight; }
}
public static Scanner in = new Scanner(System.in);
public int numOfLines;
public static void path(Vertex source)
{
source.minDistance = 0;
PriorityQueue<Vertex> vertexQueue = new PriorityQueue<Vertex>();
vertexQueue.add(source);
while (!vertexQueue.isEmpty()) {
Vertex u = vertexQueue.poll();
for (Edge e : u.adj)
{
Vertex v = e.target;
int weight = e.weight;
int distanceThroughU = u.minDistance + weight;
if (distanceThroughU < v.minDistance) {
vertexQueue.remove(v);
v.minDistance = distanceThroughU ;
v.previous = u;
vertexQueue.add(v);
}
}
}
}
public static Vertex from,target;
public static void main(String[] args)
{
int numOfLines = Integer.parseInt(in.nextLine());
Map<String, Vertex> vertexMap = new HashMap<String, Vertex>();
boolean inVertex=true;
String line;
for(int count=0; count<numOfLines; count++){
line=in.nextLine();
String[] parts = line.split(" ");
if(!vertexMap.containsKey(parts[0]))
{
Vertex v = new Vertex(parts[0]);
vertexMap.put(parts[0], v);
}
else if(!vertexMap.containsKey(parts[1]))
{
Vertex v = new Vertex(parts[1]);
vertexMap.put(parts[1], v);
}
int weight = Integer.parseInt(parts[2]);
Vertex v = vertexMap.get(parts[0]);
if (v != null) {
v.addEdge(new Edge(vertexMap.get(parts[1]),weight));
}
}
Collection<Vertex> vertices = vertexMap.values();
for(Vertex v: vertices)
{
System.out.println(v.name+" "+(int)v.minDistance);
}
}
}