DamSi

Untitled

Jul 25th, 2015
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.78 KB | None | 0 0
  1. public class RoadNetworkTest {
  2.     public static void main(String[] args) {
  3.         Scanner scanner = new Scanner(System.in);
  4.         String country = scanner.nextLine();
  5.         RoadNetwork roadNetwork = new RoadNetwork(country);
  6.         int n = scanner.nextInt();
  7.         scanner.nextLine();
  8.         for (int i = 0; i < n; ++i) {
  9.             String line = scanner.nextLine();
  10.             String[] parts = line.split(" ");
  11.             String cityName = parts[0];
  12.             int population = Integer.parseInt(parts[1]);
  13.             String[] cities = new String[(parts.length - 2) / 2];
  14.             float[] distances = new float[(parts.length - 2) / 2];
  15.             int k = 0;
  16.             for (int j = 2; j < parts.length; j += 2) {
  17.                 cities[k] = parts[j];
  18.                 distances[k++] = Float.parseFloat(parts[j + 1]);
  19.             }
  20.             roadNetwork.addCity(cityName, population, cities, distances);
  21.         }
  22.         System.out.println("SEARCH");
  23.         String cityName = scanner.nextLine();
  24.         scanner.close();
  25.         try {
  26.             roadNetwork.search(cityName);
  27.         } catch(CityNotFoundException e) {
  28.             System.out.println(e.getMessage());
  29.         }
  30.         System.out.println("ROAD NETWORK");
  31.         System.out.printf("%.2f\n", roadNetwork.roadNetwork());
  32.         System.out.println("MAX DENSE");
  33.         roadNetwork.mostDense();
  34.     }
  35. }
  36.  
  37. class RoadNetwork {
  38.     String country;
  39.     Map<String, City> cities;
  40.  
  41.     public RoadNetwork(String country) {
  42.         this.country = country;
  43.         cities = new HashMap<String, City>();
  44.     }
  45.  
  46.     public void addCity(String cityName, int population, String[] cities,
  47.             float[] distances) {
  48.         City city = new City(cityName, population);
  49.         for (int i = 0; i < cities.length; ++i) {
  50.             city.addConnection(cities[i], distances[i]);
  51.         }
  52.         this.cities.put(cityName, city);
  53.     }
  54.  
  55.     public void search(String cityName) throws CityNotFoundException {
  56.         City city = cities.get(cityName);
  57.         if (city != null) {
  58.             System.out.print(city);
  59.         } else {
  60.             throw new CityNotFoundException(String.format(
  61.                     "City '%s' was not found", cityName));
  62.         }
  63.     }
  64.  
  65.     public float roadNetwork() {
  66.         float sum = 0;
  67.         for (City city : cities.values()) {
  68.             sum += city.totalRoads();
  69.         }
  70.         return sum;
  71.     }
  72.  
  73.     public void mostDense() {
  74.         float maxCoef = Float.MIN_VALUE;
  75.         City maxCity1 = null;
  76.         City maxCity2 = null;
  77.         for (City city1 : cities.values()) {
  78.             for (City city2 : cities.values()) {
  79.                 if (!city1.name.equals(city2.name)) {
  80.                     float coef = densityCoef(city1, city2);
  81.                     if (coef > 0&&coef > maxCoef) {
  82.                         maxCoef = coef;
  83.                         maxCity1 = city1;
  84.                         maxCity2 = city2;
  85.                     }
  86.                 }
  87.             }
  88.         }
  89.         System.out.printf("%s - %s : %.2f\n", maxCity1.name, maxCity2.name,
  90.                 maxCoef);
  91.     }
  92.  
  93.     static float densityCoef(City city1, City city2) {
  94.         float distance = city1.distanceTo(city2.name);
  95.         if (distance > 0) {
  96.             return (city1.population + city2.population) / distance;
  97.         }
  98.         return -1;
  99.     }
  100. }
  101.  
  102. class CityNotFoundException extends Exception {
  103.     public CityNotFoundException(String message) {
  104.         super(message);
  105.     }
  106. }
  107.  
  108. class City {
  109.     String name;
  110.     int population;
  111.     Map<String, Float> roads;
  112.  
  113.     public City(String name, int population) {
  114.         this.population = population;
  115.         this.name = name;
  116.         roads = new TreeMap<String, Float>();
  117.     }
  118.  
  119.     public void addConnection(String city, float distance) {
  120.         roads.put(city, distance);
  121.     }
  122.  
  123.     public float totalRoads() {
  124.         float total = 0;
  125.         for (Float f : roads.values()) {
  126.             total += f;
  127.         }
  128.         return total;
  129.     }
  130.  
  131.     public float distanceTo(String city) {
  132.         if (roads.containsKey(city)) {
  133.             return roads.get(city);
  134.         }
  135.         return -1;
  136.     }
  137.  
  138.     @Override
  139.     public String toString() {
  140.         StringBuilder sb = new StringBuilder();
  141.         sb.append(name);
  142.         sb.append("\n");
  143.         sb.append(population);
  144.         sb.append("\n");
  145.         for (Entry<String, Float> entry : roads.entrySet()) {
  146.             sb.append(String.format("%s : %.1f km\n", entry.getKey(),
  147.                     entry.getValue()));
  148.         }
  149.         return sb.toString();
  150.     }
  151.  
  152. }
Advertisement
Add Comment
Please, Sign In to add comment