(UVA) Shipping Routes - Solução 1

import java.io.*;
import java.util.*;

class Main  {
    public static HashMap<String, ArrayList<String>> adjList = new HashMap<String, ArrayList<String>>();
   
    public static int bfs(String start, String goal) {
        HashSet<String> visited = new HashSet<String>();
       
        Queue<String> queue = new LinkedList<String>();
        queue.add(start);
       
        int count = 0;
        Queue<Integer> distance = new LinkedList<Integer>(); // it will keep the distance between the current warehouse and the start
        distance.add(count);
       
        while (queue.size() > 0) {
            String current = queue.poll();

            if (visited.contains(current)) {
                continue;
            }   
           
            if (current.equals(goal)) {
                return distance.element();
            }
           
            visited.add(current);
            distance.remove();
            count++;
           
            ArrayList<String> neighbors = adjList.get(current);
            for (int i = 0; i < neighbors.size(); i++) {
                if (!visited.contains(neighbors.get(i))) {
                    queue.add(neighbors.get(i));
                    distance.add(count);
                }
            }
        }   
       
        return -1;
    }
   
    public static void readRequests(Scanner sc, int numRequests, int numLegsBtwWarehouses) {
        for (int j = 0; j < numRequests; j++) {
            int sizeOfShipment = sc.nextInt();
            String source = sc.next();
            String destination = sc.next();
           
            int steps = -1;
            if (numLegsBtwWarehouses > 0) {
                steps = bfs(source, destination);               
            }
                   
            if (steps == -1) {
                System.out.println("NO SHIPMENT POSSIBLE");
            }
            else {
                System.out.println("$" + sizeOfShipment*steps*100);
            }
        }
    }
           
    public static void checkAdjList(String w1, String w2) {
        if (!adjList.containsKey(w1)) {
            ArrayList<String> list = new ArrayList<String>();
            list.add(w2);
            adjList.put(w1, list);
        }
        else {
            adjList.get(w1).add(w2);
        }
    }
   
    public static void readEdges(Scanner sc, int numLegsBtwWarehouses) {
        for (int j = 0; j < numLegsBtwWarehouses; j++) {
            String w1 = sc.next();
            String w2 = sc.next();
           
            checkAdjList(w1, w2);
            checkAdjList(w2, w1);       
        }
    }
   
    public static void  readWarehouses(Scanner sc, String[] warehouses, int numWarehouses) {
        for (int j = 0; j < numWarehouses; j++) {
            warehouses[j] = sc.next();
        }
    }
   
    public static void process() throws NumberFormatException, IOException {   
        Scanner sc = new Scanner(System.in);
       
        int numDataSets = sc.nextInt();
       
        System.out.println("SHIPPING ROUTES OUTPUT");
        for (int i = 0; i < numDataSets; i++) {
            int numWarehouses = sc.nextInt();
            int numLegsBtwWarehouses = sc.nextInt();
            int numRequests = sc.nextInt();
           
            // read warehouses
            String[] warehouses = new String[numWarehouses];
            readWarehouses(sc, warehouses, numWarehouses);
           
            // read the edges between the warehouses
            readEdges(sc, numLegsBtwWarehouses);
              
            // read the requests
            System.out.println("\nDATA SET  " + (i+1) + "\n");           
            readRequests(sc, numRequests, numLegsBtwWarehouses);
           
            adjList.clear();
        }
        System.out.println("\nEND OF OUTPUT");
                                       
        return;
    }
   
    public static void main(String[] args) throws NumberFormatException, IOException {
        Main m = new Main();
        m.process();

        System.exit(0);
    }
}

Comments

Popular posts from this blog

(Coderbyte) Powers of Two - Solução

(Coderbyte) Dash Insert II - Solução

(CoderByte) Number Search - Solução