简体   繁体   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);
      }
}
}

we are supposed to use input in the form (v1,v2,w1) where v1 is primary vertex v2 is target vertex w1 is weight. 我们应该使用(v1,v2,w1)形式的输入,其中v1是主要顶点v2是目标顶点w1是权重。 an example input is 一个示例输入是

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

the output on this (for 47 to 79) should be 9(with path 47,28,29,79) but instead i get 0 在此(对于47到79)的输出应为9(具有路径47、28、29、79),但我却得到0

We are supposed to find the shortest path from 47 to 79 (silver to gold) i thought i had it all working. 我们应该找到一条从47到79(从银到金)的最短路径,我以为我都工作了。 than it only output max int value for the shortest path. 比它只输出最短路径的最大int值。 I dont know where i went wrong in the code though. 我不知道我在代码中哪里出错了。

Thanks for any/all help. 感谢您提供的所有帮助。

It is do to the size of the int in the system. 这取决于系统中int的大小。 that is the max for a signed int that is 32-bit long 这是32位长的有符号整数的最大值

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

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

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