Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- class Graph<T>{
- private List<Node<T>> nodes;
- Graph(){
- nodes = new ArrayList<Node<T>>();
- }
- public void addNode(Node<T> n){
- nodes.add(n);
- }
- public String Dijkstra(int pocetok, int kraj){
- PriorityQueue<Node<T>> neizminati = new PriorityQueue<Node<T>>();
- Node<T> momentalen = nodes.get(pocetok);
- momentalen.setDistance(momentalen.getWeigth());
- neizminati.addAll(nodes);
- while((momentalen=neizminati.poll()) != nodes.get(kraj)){
- if(neizminati.isEmpty()) return "nedostapno";
- for(Edge<T> e : momentalen.getEdges()){
- if((momentalen.getDistance() + e.getWeight() + e.getN().getWeigth()) < e.getN().getDistance()){
- e.getN().setDistance(momentalen.getDistance() + e.getWeight() + e.getN().getWeigth());
- e.getN().setSteps( momentalen.getSteps() + 1);
- }
- }
- PriorityQueue<Node<T>> tmp = new PriorityQueue<Node<T>>();
- tmp.addAll(neizminati);
- neizminati = tmp;
- }
- if(Float.compare(momentalen.getDistance(), Float.POSITIVE_INFINITY) == 0) return "nedostapno";
- return String.format("%.0f", momentalen.getDistance());
- }
- //auto generated
- public List<Node<T>> getNodes() {
- return nodes;
- }
- public void setNodes(List<Node<T>> nodes) {
- this.nodes = nodes;
- }
- }
- class Edge<T>{
- private Node<T> n;
- private float weight;
- Edge(){}
- Edge(Node<T> n, float weight){
- this.n = n;
- this.weight = weight;
- }
- //auto generated
- public Node<T> getN() {
- return n;
- }
- public void setN(Node<T> n) {
- this.n = n;
- }
- public float getWeight() {
- return weight;
- }
- public void setWeight(float weight) {
- this.weight = weight;
- }
- }
- class Node<V> implements Comparable<Node<V>>{
- private V value;
- private List<Edge<V>> edges;
- private float weigth;
- private float distance;
- private int steps;
- Node(){
- edges = new ArrayList<Edge<V>>();
- distance = Float.POSITIVE_INFINITY;
- steps = 0;
- }
- Node(float weigth, V value){
- this.weigth = weigth;
- edges = new ArrayList<Edge<V>>();
- this.value = value;
- distance = Float.POSITIVE_INFINITY;
- steps = 0;
- }
- Node(V value){
- edges = new ArrayList<Edge<V>>();
- this.value = value;
- distance = Float.POSITIVE_INFINITY;
- steps = 0;
- weigth = 0;
- }
- public void addEdge(Edge<V> e){
- edges.add(e);
- }
- //auto generated
- public List<Edge<V>> getEdges() {
- return edges;
- }
- public int getSteps() {
- return steps;
- }
- public void setSteps(int steps) {
- this.steps = steps;
- }
- public void setEdges(List<Edge<V>> edges) {
- this.edges = edges;
- }
- public float getWeigth() {
- return weigth;
- }
- public void setWeigth(float weigth) {
- this.weigth = weigth;
- }
- public V getValue() {
- return value;
- }
- public void setValue(V value) {
- this.value = value;
- }
- @Override
- public int compareTo(Node<V> arg0) {
- //System.out.println(this.value + " " + arg0.value);
- // System.out.println(this.distance + " " + arg0.distance);
- // System.out.println(Float.compare(distance, arg0.distance));
- return Float.compare(distance, arg0.distance);
- }
- public float getDistance() {
- return distance;
- }
- public void setDistance(float distance) {
- this.distance = distance;
- }
- }
- public class Main {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Scanner sc = new Scanner(System.in);
- int n = sc.nextInt();
- int m = sc.nextInt();
- int from = sc.nextInt();
- int to = sc.nextInt();
- Graph<Integer> graf = new Graph<Integer>();
- for(int i=0; i<n; i++){
- graf.addNode(new Node<Integer>(i));
- }
- for(int i=0; i<m; i++){
- int a = sc.nextInt();
- int b = sc.nextInt();
- int c = sc.nextInt();
- graf.getNodes().get(a).addEdge(new Edge<Integer>(graf.getNodes().get(b), c));;
- graf.getNodes().get(b).addEdge(new Edge<Integer>(graf.getNodes().get(a), c));
- }
- sc.close();
- System.out.println(graf.Dijkstra(from, to));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement