Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.72 KB | None | 0 0
  1. package cv9;
  2.  
  3. import java.util.PriorityQueue;
  4. import java.util.Vector;
  5.  
  6. public class Cesta {
  7.  
  8.     private Graf graf;
  9.     private Uzel lastNode = null;
  10.     private Vector<Uzel> nodes = new Vector<Uzel>();
  11.     private int cost;
  12.    
  13.    
  14.  
  15.     public Cesta(Graf graf) {
  16.         super();
  17.         this.graf = graf;
  18.    
  19.     }
  20.  
  21.     public void addNode(String nazevuzlu) {
  22.         Uzel u = graf.getUzel(nazevuzlu);
  23.        
  24.         if (lastNode == null) {
  25.             cost = 0;
  26.         } else {
  27.             cost += lastNode.getCost(u);
  28.         }
  29.         lastNode = u;
  30.         nodes.add(u);
  31.     }
  32.  
  33.     public int getCost() {
  34.         return cost;
  35.     }
  36.  
  37.     public void setCost(int cost) {
  38.         this.cost = cost;
  39.     }
  40.    
  41.     public boolean jeVCieli(String ciel) {
  42.         if (lastNode == null){
  43.             return false;
  44.         } else {
  45.             return ciel.equals(lastNode.getData());
  46.         }
  47.        
  48.     }
  49.        
  50.         public Cesta kopirujCestuPridajUzel(String nazevuzlu) {
  51.             Cesta kopia = new Cesta(graf);
  52.             kopia.lastNode = lastNode;
  53.             kopia.nodes.addAll(nodes);
  54.             kopia.addNode(nazevuzlu);
  55.             return kopia;
  56.         }
  57.        
  58.         public Cesta getCesta(String odkial, String kam) {
  59.             PriorityQueue<Cesta> prioFronta = new PriorityQueue<Cesta>();
  60.            
  61.             Cesta c = new Cesta(graf);
  62.             c.addNode(odkial);
  63.             prioFronta.add(c);
  64.            
  65.             while (!prioFronta.isEmpty()) {
  66.                 Cesta tmp = prioFronta.remove();
  67.                 if (tmp.jeVCieli(kam)) {
  68.                     return tmp;
  69.                    
  70.                 }
  71.                 for (Uzel u : tmp.getLast().getSousede()) {
  72.                     Cesta kopia = tmp.kopirujCestuPridajUzel(u.getData());
  73.                     String nazevuzlu = kopia.getLast().getData();
  74.                     System.out.println("Kopia:  A-E-B" + nazevuzlu + "   cost=" + kopia.getCost());
  75.                     prioFronta.add(kopia);
  76.                    
  77.                 }
  78.             }
  79.             return null;
  80.         }
  81.  
  82.         /*public Uzel getLast() {
  83.             // TODO Auto-generated method stub
  84.             return null;
  85.         }*/
  86.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement