Advertisement
Guest User

dijkstra

a guest
Mar 21st, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.63 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. class Graph<T>{
  4. private List<Node<T>> nodes;
  5. Graph(){
  6. nodes = new ArrayList<Node<T>>();
  7. }
  8. public void addNode(Node<T> n){
  9. nodes.add(n);
  10. }
  11. public String Dijkstra(int pocetok, int kraj){
  12. PriorityQueue<Node<T>> neizminati = new PriorityQueue<Node<T>>();
  13. Node<T> momentalen = nodes.get(pocetok);
  14. momentalen.setDistance(momentalen.getWeigth());
  15. neizminati.addAll(nodes);
  16. while((momentalen=neizminati.poll()) != nodes.get(kraj)){
  17. if(neizminati.isEmpty()) return "nedostapno";
  18. for(Edge<T> e : momentalen.getEdges()){
  19. if((momentalen.getDistance() + e.getWeight() + e.getN().getWeigth()) < e.getN().getDistance()){
  20. e.getN().setDistance(momentalen.getDistance() + e.getWeight() + e.getN().getWeigth());
  21. e.getN().setSteps( momentalen.getSteps() + 1);
  22. }
  23.  
  24. }
  25. PriorityQueue<Node<T>> tmp = new PriorityQueue<Node<T>>();
  26. tmp.addAll(neizminati);
  27. neizminati = tmp;
  28. }
  29. if(Float.compare(momentalen.getDistance(), Float.POSITIVE_INFINITY) == 0) return "nedostapno";
  30. return String.format("%.0f", momentalen.getDistance());
  31. }
  32.  
  33. //auto generated
  34. public List<Node<T>> getNodes() {
  35. return nodes;
  36. }
  37. public void setNodes(List<Node<T>> nodes) {
  38. this.nodes = nodes;
  39. }
  40.  
  41.  
  42. }
  43. class Edge<T>{
  44. private Node<T> n;
  45. private float weight;
  46. Edge(){}
  47. Edge(Node<T> n, float weight){
  48. this.n = n;
  49. this.weight = weight;
  50. }
  51.  
  52. //auto generated
  53. public Node<T> getN() {
  54. return n;
  55. }
  56. public void setN(Node<T> n) {
  57. this.n = n;
  58. }
  59. public float getWeight() {
  60. return weight;
  61. }
  62. public void setWeight(float weight) {
  63. this.weight = weight;
  64. }
  65.  
  66. }
  67. class Node<V> implements Comparable<Node<V>>{
  68. private V value;
  69. private List<Edge<V>> edges;
  70. private float weigth;
  71. private float distance;
  72. private int steps;
  73. Node(){
  74. edges = new ArrayList<Edge<V>>();
  75. distance = Float.POSITIVE_INFINITY;
  76. steps = 0;
  77.  
  78. }
  79. Node(float weigth, V value){
  80. this.weigth = weigth;
  81. edges = new ArrayList<Edge<V>>();
  82. this.value = value;
  83. distance = Float.POSITIVE_INFINITY;
  84. steps = 0;
  85. }
  86. Node(V value){
  87. edges = new ArrayList<Edge<V>>();
  88. this.value = value;
  89. distance = Float.POSITIVE_INFINITY;
  90. steps = 0;
  91. weigth = 0;
  92. }
  93. public void addEdge(Edge<V> e){
  94. edges.add(e);
  95. }
  96.  
  97. //auto generated
  98.  
  99. public List<Edge<V>> getEdges() {
  100. return edges;
  101. }
  102. public int getSteps() {
  103. return steps;
  104. }
  105. public void setSteps(int steps) {
  106. this.steps = steps;
  107. }
  108. public void setEdges(List<Edge<V>> edges) {
  109. this.edges = edges;
  110. }
  111. public float getWeigth() {
  112. return weigth;
  113. }
  114. public void setWeigth(float weigth) {
  115. this.weigth = weigth;
  116. }
  117. public V getValue() {
  118. return value;
  119. }
  120. public void setValue(V value) {
  121. this.value = value;
  122. }
  123. @Override
  124. public int compareTo(Node<V> arg0) {
  125. //System.out.println(this.value + " " + arg0.value);
  126. // System.out.println(this.distance + " " + arg0.distance);
  127. // System.out.println(Float.compare(distance, arg0.distance));
  128. return Float.compare(distance, arg0.distance);
  129. }
  130. public float getDistance() {
  131. return distance;
  132. }
  133. public void setDistance(float distance) {
  134. this.distance = distance;
  135. }
  136.  
  137.  
  138. }
  139.  
  140.  
  141. public class Main {
  142.  
  143. public static void main(String[] args) {
  144. // TODO Auto-generated method stub
  145. Scanner sc = new Scanner(System.in);
  146. int n = sc.nextInt();
  147. int m = sc.nextInt();
  148. int from = sc.nextInt();
  149. int to = sc.nextInt();
  150. Graph<Integer> graf = new Graph<Integer>();
  151. for(int i=0; i<n; i++){
  152. graf.addNode(new Node<Integer>(i));
  153. }
  154. for(int i=0; i<m; i++){
  155. int a = sc.nextInt();
  156. int b = sc.nextInt();
  157. int c = sc.nextInt();
  158. graf.getNodes().get(a).addEdge(new Edge<Integer>(graf.getNodes().get(b), c));;
  159. graf.getNodes().get(b).addEdge(new Edge<Integer>(graf.getNodes().get(a), c));
  160. }
  161. sc.close();
  162. System.out.println(graf.Dijkstra(from, to));
  163.  
  164. }
  165.  
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement