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

时间:2014-04-29 19:14:27

标签: java integer dijkstra

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(银到金)的最短路径我认为我已经全部工作了。比它只输出最短路径的最大值。我不知道代码在哪里出错了。

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

它是系统中int的大小。这是32位长的带符号int的最大值

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

相关问题