繁体   English   中英

为什么我的代码在dijkstras中打印出0作为距离?

[英]Why does my code print out 0 as the distance in dijkstras?

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);
      }
}
}

我们应该使用(v1,v2,w1)形式的输入,其中v1是主要顶点v2是目标顶点w1是权重。 一个示例输入是

10
47 28 5
28 29 3
26 79 6
28 26 2
79 26 4
28 79 9
26 47 7
29 28 2
47 29 10
29 79 1

在此(对于47到79)的输出应为9(具有路径47、28、29、79),但我却得到0

我们应该找到一条从47到79(从银到金)的最短路径,我以为我都工作了。 比它只输出最短路径的最大int值。 我不知道我在代码中哪里出错了。

感谢您提供的所有帮助。

这取决于系统中int的大小。 这是32位长的有符号整数的最大值

参见http://en.wikipedia.org/wiki/2147483647#In_computing

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM