Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void main(String[] args) throws IOException
- {
- hp = new HashSet<String>();
- Scanner in = new Scanner(System.in);
- String fileName = "input1.txt";
- String split[];
- try {
- FileReader fileReader =
- new FileReader(fileName);
- BufferedReader bufferedReader =
- new BufferedReader(fileReader);
- while((line = bufferedReader.readLine()) != null) {
- if(line.equals("END OF INPUT"))
- break;
- split = line.split(" ");
- hp.add((split[0]));
- hp.add((split[1]));
- }
- bufferedReader.close();
- }
- catch(FileNotFoundException ex) {
- System.out.println(
- "Unable to open file '" +
- fileName + "'");
- }
- catch(IOException ex) {
- System.out.println(
- "Error reading file '"
- + fileName + "'");
- }
- copy = new ArrayList<String>(hp);
- int[][] adjacencyMatrix = new int[hp.size()+1][hp.size()+1];
- try {
- FileReader fileReader =
- new FileReader(fileName);
- BufferedReader bufferedReader =
- new BufferedReader(fileReader);
- while((line = bufferedReader.readLine()) != null) {
- if(line.equals("END OF INPUT"))
- break;
- split = line.split(" ");
- int i = getIndex(split[0]);
- int j = getIndex(split[1]);
- adjacencyMatrix[j][i] = adjacencyMatrix[i][j] = Integer.parseInt(split[2]);
- }
- bufferedReader.close();
- }
- catch(FileNotFoundException ex) {
- System.out.println(
- "Unable to open file '" +
- fileName + "'");
- }
- catch(IOException ex) {
- System.out.println(
- "Error reading file '"
- + fileName + "'");
- }
- for(int i=0;i<adjacencyMatrix.length;i++){
- for(int j=0;j<adjacencyMatrix.length;j++){
- if(adjacencyMatrix[i][j] == 0)
- adjacencyMatrix[i][j] = 9999;
- // System.out.printf("i = %d, j = %d, %d\n",i,j,adjacencyMatrix[i][j]);
- }
- }
- UniformCostSearch ucs = new UniformCostSearch(copy.size());
- String source = in.next();
- String destination = in.next();
- if(hp.contains(source) && hp.contains(destination)){
- int src = getIndex(source);
- int dst = getIndex(destination);
- int distance = ucs.uniformCostSearch(adjacencyMatrix, src, dst);
- if(distance == 99999999){
- System.out.println();System.out.println();System.out.println();
- System.out.println("Distance: INFINITY");
- System.out.println("Route:\nNONE");
- System.out.println();System.out.println();System.out.println();
- }else{
- ucs.printPath();
- Iterator<Integer> iterator = ucs.path.descendingIterator();
- System.out.println();System.out.println();System.out.println();
- System.out.println("Distance: "+distance);
- String[] traverse = new String[ucs.path.size()];
- int pp = 0;
- while(iterator.hasNext()){
- String aa = copy.get(iterator.next()-1);
- traverse[pp++] = aa;
- }
- System.out.println("Route:");
- for(int i=0;i<traverse.length-1;i++){
- int j = i+1;
- System.out.println(traverse[i]+" -> "+traverse[j]+", "+adjacencyMatrix[getIndex(traverse[i])][getIndex(traverse[j])]);
- }
- System.out.println("");System.out.println("");System.out.println("");}
- }else{
- System.out.println();System.out.println();System.out.println();
- System.out.println("Specified Location does not exist");
- System.out.println();System.out.println();System.out.println();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement