Advertisement
SilvanM

Untitled

Dec 10th, 2022
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.63 KB | None | 0 0
  1. // Graph class representing the towns of Switzerland
  2. class Graph {
  3.   // Map from vertex to list of edges
  4.   Map<Vertex, List<Edge>> adjacencyList;
  5.  
  6.   // Add an edge to the graph
  7.   void addEdge(Vertex v1, Vertex v2, int weight) {
  8.     // Add the edge to the adjacency list of v1
  9.     List<Edge> edges = adjacencyList.get(v1);
  10.     if (edges == null) {
  11.       edges = new ArrayList<>();
  12.       adjacencyList.put(v1, edges);
  13.     }
  14.     edges.add(new Edge(v2, weight));
  15.  
  16.     // Add the edge to the adjacency list of v2
  17.     edges = adjacencyList.get(v2);
  18.     if (edges == null) {
  19.       edges = new ArrayList<>();
  20.       adjacencyList.put(v2, edges);
  21.     }
  22.     edges.add(new Edge(v1, weight));
  23.   }
  24. }
  25.  
  26. // Vertex class representing a town in Switzerland
  27. class Vertex {
  28.   // The language spoken in the town
  29.   Language language;
  30.  
  31.   // Constructor
  32.   Vertex(Language language) {
  33.     this.language = language;
  34.   }
  35. }
  36.  
  37. // Edge class representing a road between two towns in Switzerland
  38. class Edge {
  39.   // The destination vertex and the weight of the edge
  40.   Vertex destination;
  41.   int weight;
  42.  
  43.   // Constructor
  44.   Edge(Vertex destination, int weight) {
  45.     this.destination = destination;
  46.     this.weight = weight;
  47.   }
  48. }
  49.  
  50. // Enum class representing the four languages spoken in Switzerland
  51. enum Language {
  52.   GERMAN, FRENCH, ITALIAN, ROMANSH
  53. }
  54.  
  55. // Find the shortest hike that goes through at least one town speaking each of the four languages
  56. int findShortestHike(Graph graph) {
  57.   int shortestHike = Integer.MAX_VALUE; // Initialize the shortest hike to infinity
  58.  
  59.   // For each vertex in the graph
  60.   for (Vertex v : graph.adjacencyList.keySet()) {
  61.     // If the language spoken in town v is not one of the four languages we are looking for, continue to the next vertex
  62.     if (v.language != Language.GERMAN && v.language != Language.FRENCH && v.language != Language.ITALIAN && v.language != Language.ROMANSH) {
  63.       continue;
  64.     }
  65.  
  66.     // For each of the other three languages we are looking for, perform a breadth-first search starting at vertex v to find the shortest path to a town speaking that language
  67.     int germanPathLength = findShortestPath(graph, v, Language.GERMAN);
  68.     int frenchPathLength = findShortestPath(graph, v, Language.FRENCH);
  69.     int italianPathLength = findShortestPath(graph, v, Language.ITALIAN);
  70.  
  71.     // Let hikeLength be the sum of the lengths of the three shortest paths found in the previous step
  72.     int hikeLength = germanPathLength + frenchPathLength + italianPathLength;
  73.  
  74.     // If hikeLength is less than shortestHike, set shortestHike to hikeLength
  75.     if (hikeLength < shortestHike) {
  76.       shortestHike =
  77.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement