简体   繁体   中英

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. an example input is

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

the output on this (for 47 to 79) should be 9(with path 47,28,29,79) but instead i get 0

We are supposed to find the shortest path from 47 to 79 (silver to gold) i thought i had it all working. than it only output max int value for the shortest path. 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. that is the max for a signed int that is 32-bit long

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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