fork download
  1. package Dijkstra;
  2.  
  3. //import static java.awt.geom.Point2D.distance;
  4. import java.util.Scanner;
  5. import java.lang.ArrayIndexOutOfBoundsException;
  6. import java.io.File;
  7. import java.io.FileNotFoundException;
  8. import java.io.IOException;
  9. import java.util.Formatter;
  10. import java.util.InputMismatchException;
  11.  
  12. public class Traffic {
  13. //private
  14. static void dijkstra(int[][] array, int[] distance) {
  15. int size = array.length;
  16. boolean[] visited = new boolean[size];
  17. for (int i = 0; i < size; i++) {
  18. distance[i] = Integer.MAX_VALUE;
  19. }
  20.  
  21. Scanner value = new Scanner(System.in);
  22. System.out.println("Please Select your From destination::");
  23.  
  24. try{
  25. int value1 = value.nextInt();
  26. distance[value1] = 0;
  27. }catch(InputMismatchException e){
  28. System.out.println("Invalid.Please try to type Integer value..");
  29. System.out.println(e);
  30. }
  31.  
  32. for (int i = 0; i < size - 1; i++) {
  33. int mindis = minimumDistance(distance, visited);
  34. visited[mindis] = true;
  35. for (int j = 0; j < size; j++) {
  36. if (!visited[j] && array[mindis][j] != 0 && distance[mindis] != Integer.MAX_VALUE) {
  37. int newdis = distance[mindis] + array[mindis][j];
  38. if (newdis < distance[j]) {
  39. distance[j] = newdis;
  40. }
  41. }
  42. }
  43. }
  44.  
  45. }
  46.  
  47. public static int minimumDistance(int[] distance, boolean[] visited) {
  48. int minimum = -1;
  49. int minDistance = Integer.MAX_VALUE;
  50. for (int i = 0; i < distance.length; i++) {
  51. if (!visited[i] && distance[i] < minDistance) {
  52. minDistance = distance[i];
  53. minimum = i;
  54. }
  55. }
  56. return minimum;
  57. }
  58.  
  59. public static void main(String[] args) {
  60.  
  61. File dir = new File("person");
  62. // System.out.println(dir.getAbsoluteFile());
  63. File file1 = new File("D:/java/OOP/person/Student.txt");
  64.  
  65. while (true) {
  66. try {
  67. file1.createNewFile();
  68. System.out.println("....................Welcome to our traffic Management System......................\n");
  69.  
  70. System.out.println("1.Start the Program");
  71. System.out.println("2.Exit \n");
  72. System.out.println("Plase Choose Your Option..");
  73.  
  74. } catch (IOException e) {
  75. System.out.println(e);
  76. }
  77.  
  78. try {
  79. Scanner result = new Scanner(System.in);
  80. int option;
  81. option = result.nextInt();
  82. switch (option) {
  83. case 1 -> {
  84. try {
  85. int a, b, weight;
  86. int n, v;
  87. int[] distance;
  88. int[][] array;
  89. try {
  90. Formatter f1 = new Formatter("D:/java/OOP/person/Student.txt");
  91. Scanner input = new Scanner(System.in);
  92. System.out.println("Thank Your Sir or Ma'am for opening our Traffic management Service.\n");
  93. System.out.println("Please Provide How many Location: ");
  94. n = input.nextInt();
  95. System.out.println("Please Provide How many Roads: ");
  96.  
  97. v = input.nextInt();
  98. f1.format("%d %d \r \n", n, v);
  99. distance = new int[n];
  100. array = new int[n][n]; // 2D array used in Java
  101. System.out.println("Please Provide your Inputs::");
  102. for (int i = 0; i < v; i++) {
  103. a = input.nextInt();
  104. b = input.nextInt();
  105. weight = input.nextInt();
  106. array[a][b] = weight;
  107. array[b][a] = weight;
  108. f1.format("%d %d %d \r \n", a, b, weight);
  109. }
  110.  
  111. f1.close();
  112.  
  113. Traffic.dijkstra(array, distance);
  114. for (int i = 0; i < n; i++) {
  115. System.out.println(i + " --> " + i + "");
  116. }
  117. System.out.println("Please Select your To destination::");
  118. int destination;
  119. try{
  120.  
  121. destination = input.nextInt();
  122. System.out.println("There are " + distance[destination] + " Cars on this road And the road is less congested..");
  123. System.out.println("You can go this Way..");
  124. }catch(InputMismatchException e){
  125. System.out.println("Invalid.Please try to type Integer value..");
  126. System.out.println(e);
  127. }
  128.  
  129. } catch (FileNotFoundException e) {
  130. System.out.println(e);
  131. }
  132.  
  133. } catch (ArrayIndexOutOfBoundsException e) {
  134. System.out.println(e);
  135. }
  136. }
  137. case 2 -> {
  138. System.out.println("Thank Your our Using the service.");
  139. System.out.println("Exit the program");
  140. }
  141.  
  142. default -> System.out.println("Invalid Option.Please choose again..");
  143.  
  144. }
  145. } catch (InputMismatchException e) {
  146. System.out.println("Invalid.Please try to type Integer value..");
  147. System.out.println("");
  148. }
  149.  
  150. }
  151.  
  152. }
  153.  
  154. }
  155.  
Success #stdin #stdout 0.02s 26080KB
stdin
Standard input is empty
stdout
package Dijkstra;

//import static java.awt.geom.Point2D.distance;
import java.util.Scanner;
import java.lang.ArrayIndexOutOfBoundsException;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Formatter;
import java.util.InputMismatchException;

public class Traffic {
//private 
    static  void dijkstra(int[][] array, int[] distance) {
        int size = array.length;
        boolean[] visited = new boolean[size];
        for (int i = 0; i < size; i++) {
            distance[i] = Integer.MAX_VALUE;
        }
 
        Scanner value = new Scanner(System.in);
        System.out.println("Please Select your From destination::");
       
        try{
             int value1 = value.nextInt();
            distance[value1] = 0;
        }catch(InputMismatchException e){
            System.out.println("Invalid.Please try to type Integer value..");
            System.out.println(e);
        }

        for (int i = 0; i < size - 1; i++) {
            int mindis = minimumDistance(distance, visited);
            visited[mindis] = true;
            for (int j = 0; j < size; j++) {
                if (!visited[j] && array[mindis][j] != 0 && distance[mindis] != Integer.MAX_VALUE) {
                    int newdis = distance[mindis] + array[mindis][j];
                    if (newdis < distance[j]) {
                        distance[j] = newdis;
                    }
                }
            }
        }

    }

    public static int minimumDistance(int[] distance, boolean[] visited) {
        int minimum = -1;
        int minDistance = Integer.MAX_VALUE;
        for (int i = 0; i < distance.length; i++) {
            if (!visited[i] && distance[i] < minDistance) {
                minDistance = distance[i];
                minimum = i;
            }
        }
        return minimum;
    }

    public static void main(String[] args) {

        File dir = new File("person");
        dir.mkdir();
//        System.out.println(dir.getAbsoluteFile());
        File file1 = new File("D:/java/OOP/person/Student.txt");

        while (true) {
            try {
                file1.createNewFile();
                System.out.println("....................Welcome to our traffic Management System......................\n");

                System.out.println("1.Start the Program");
                System.out.println("2.Exit \n");
                System.out.println("Plase Choose Your Option..");

            } catch (IOException e) {
                System.out.println(e);
            }

            try {
                Scanner result = new Scanner(System.in);
                int option;
                option = result.nextInt();
                switch (option) {
                    case 1 -> {
                        try {
                            int a, b, weight;
                            int n, v;
                            int[] distance;
                            int[][] array;
                            try {
                                Formatter f1 = new Formatter("D:/java/OOP/person/Student.txt");
                                Scanner input = new Scanner(System.in);
                                System.out.println("Thank Your Sir or Ma'am for opening our Traffic management Service.\n");
                                System.out.println("Please Provide How many Location: ");
                                n = input.nextInt();
                                System.out.println("Please Provide How many Roads: ");

                                v = input.nextInt();
                                f1.format("%d %d \r \n", n, v);
                                distance = new int[n];
                                array = new int[n][n]; // 2D array used in Java
                                System.out.println("Please Provide your Inputs::");
                                for (int i = 0; i < v; i++) {
                                    a = input.nextInt();
                                    b = input.nextInt();
                                    weight = input.nextInt();
                                    array[a][b] = weight;
                                    array[b][a] = weight;
                                    f1.format("%d %d %d \r \n", a, b, weight);
                                }

                                f1.close();
                               
                                Traffic.dijkstra(array, distance);
                                for (int i = 0; i < n; i++) {
                                    System.out.println(i + "  -->  " + i + "");
                                }
                                System.out.println("Please Select your To destination::");
                                int destination;
                                try{
                                    
                                    destination = input.nextInt();
                                    System.out.println("There are " + distance[destination] + " Cars on this road And the road is less congested..");
                                    System.out.println("You can go this Way..");
                                }catch(InputMismatchException e){
                                    System.out.println("Invalid.Please try to type Integer value..");
                                    System.out.println(e);
                                }
                                
                            } catch (FileNotFoundException e) {
                                System.out.println(e);
                            }

                        } catch (ArrayIndexOutOfBoundsException e) {
                            System.out.println(e);
                        }
                    }
                    case 2 -> {
                        System.out.println("Thank Your our Using the service.");
                        System.out.println("Exit the program");
                        System.exit(0);
                    }

                    default -> System.out.println("Invalid Option.Please choose again..");

                }
            } catch (InputMismatchException e) {
                System.out.println("Invalid.Please try to type Integer value..");
                System.out.println("");
            }

        }

    }

}