{"id":33299,"date":"2024-11-01T09:15:18","date_gmt":"2024-11-01T09:15:18","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=33299"},"modified":"2024-11-01T11:39:17","modified_gmt":"2024-11-01T11:39:17","slug":"java-coding-test-course-finding-the-fastest-bus-route","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/33299\/","title":{"rendered":"Java Coding Test Course, Finding the Fastest Bus Route"},"content":{"rendered":"<p><body><\/p>\n<p>In this article, we will address the problem of &#8220;Finding the Fastest Bus Route,&#8221; which may appear in coding tests using Java. The shortest path problem using buses is an excellent example for grasping the basic concepts of greedy algorithms and graph theory. Through this, we will enhance our algorithm problem-solving skills and improve our Java coding abilities.<\/p>\n<h2>Problem Description<\/h2>\n<p>There are several bus routes between the given city A and city B, each with information about specific departure and arrival times. The bus routes have different travel times and fares, and we need to choose the fastest route among many.<\/p>\n<h3>Input<\/h3>\n<p>The input is provided in the following format:<\/p>\n<ul>\n<li>The first line receives information about city A and city B.<\/li>\n<li>The second line gives the number of bus routes N.<\/li>\n<li>The following N lines each provide the starting city, ending city, travel time, and fare for each bus route.<\/li>\n<\/ul>\n<h3>Output<\/h3>\n<p>Print the travel time for the fastest bus. If it is not possible to reach, print &#8220;Cannot arrive.&#8221;<\/p>\n<h2>Example<\/h2>\n<pre>\n    Input:\n    A B\n    3\n    A B 5 3000\n    A B 3 2500\n    B A 2 1500\n\n    Output:\n    3\n    <\/pre>\n<h2>Problem Solving Process<\/h2>\n<h3>1. Understanding the Problem<\/h3>\n<p>The first thing to do is to thoroughly understand the problem. It is important to note that there are multiple routes, and each route has different starting points, endpoints, travel times, and fares. This problem can be summarized as finding the shortest path. We need to find the shortest path from the starting city A to the destination city B.<\/p>\n<h3>2. Algorithm Selection<\/h3>\n<p>The most suitable algorithm to solve this problem is Dijkstra&#8217;s shortest path algorithm. This is an efficient algorithm that finds the shortest path between two vertices in a weighted graph where the weights represent travel times. Additionally, it can be implemented using Java.<\/p>\n<h3>3. Implementing Dijkstra&#8217;s Algorithm<\/h3>\n<p>Next is the process of implementing Dijkstra&#8217;s algorithm in Java. First, we will define the necessary classes and set up the data structures to store information about each city and route.<\/p>\n<pre>\n    import java.util.*;\n\n    public class BusRoute {\n        static class Route {\n            String destination;\n            int time;\n            int price;\n\n            public Route(String destination, int time, int price) {\n                this.destination = destination;\n                this.time = time;\n                this.price = price;\n            }\n        }\n\n        public static void main(String[] args) {\n            Scanner sc = new Scanner(System.in);\n            String start = sc.nextLine(); \/\/ Starting city\n            String end = sc.nextLine();   \/\/ Destination city\n            int N = sc.nextInt();         \/\/ Number of routes\n            Map<String, List<Route>> graph = new HashMap<>();\n\n            for (int i = 0; i < N; i++) {\n                String from = sc.next();\n                String to = sc.next();\n                int time = sc.nextInt();\n                int price = sc.nextInt();\n                graph.computeIfAbsent(from, k -> new ArrayList<>()).add(new Route(to, time, price));\n            }\n\n            \/\/ Call Dijkstra's algorithm\n            int shortestTime = dijkstra(start, end, graph);\n\n            \/\/ Output result\n            if (shortestTime == Integer.MAX_VALUE) {\n                System.out.println(\"Cannot arrive.\");\n            } else {\n                System.out.println(shortestTime);\n            }\n        }\n    }\n    <\/pre>\n<h3>4. Core Logic of Dijkstra&#8217;s Algorithm<\/h3>\n<p>To implement Dijkstra&#8217;s algorithm, we first use a priority queue to store the shortest time information and city information. While exploring the connected cities from each current city, we update based on the array that records the current travel time. Below is the implementation of the Dijkstra function.<\/p>\n<pre>\n    public static int dijkstra(String start, String end, Map<String, List<Route>> graph) {\n        PriorityQueue<Route> pq = new PriorityQueue<>(Comparator.comparingInt(r -> r.time));\n        Map<String, Integer> minTime = new HashMap<>();\n\n        pq.add(new Route(start, 0, 0)); \/\/ Add starting city\n        minTime.put(start, 0);\n\n        while (!pq.isEmpty()) {\n            Route current = pq.poll();\n\n            \/\/ Reached the destination\n            if (current.destination.equals(end)) {\n                return current.time;\n            }\n\n            \/\/ Explore routes connected to the current city\n            for (Route route : graph.getOrDefault(current.destination, Collections.emptyList())) {\n                int newTime = current.time + route.time;\n\n                if (newTime < minTime.getOrDefault(route.destination, Integer.MAX_VALUE)) {\n                    minTime.put(route.destination, newTime);\n                    pq.add(new Route(route.destination, newTime, route.price));\n                }\n            }\n        }\n\n        return Integer.MAX_VALUE; \/\/ Cannot arrive\n    }\n    <\/pre>\n<h3>5. Code Explanation<\/h3>\n<p>In the code above, Dijkstra's algorithm uses a priority queue to prioritize reviewing the fastest routes. It selects the city with the lowest travel time and proceeds to travel to other cities connected to that city. If the new travel time is shorter than the recorded time, it updates the information and continues exploring ahead.<\/p>\n<h2>Conclusion<\/h2>\n<p>In this post, we examined the process of implementing Dijkstra's algorithm in Java through the problem of \"Finding the Fastest Bus Route.\" When solving algorithm problems, it is essential to understand the essence of the problem and choose an effective algorithm. This example is very useful for preparing for coding tests and is a skill that can be applied to other similar problems.<\/p>\n<p>I hope you can cultivate algorithmic thinking by breaking down complex problems into manageable steps. Next time, I will explore more diverse algorithms and problem-solving methods!<\/p>\n<footer>\n<p>\u00a9 2023 Blog Author<\/p>\n<\/footer>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we will address the problem of &#8220;Finding the Fastest Bus Route,&#8221; which may appear in coding tests using Java. The shortest path problem using buses is an excellent example for grasping the basic concepts of greedy algorithms and graph theory. Through this, we will enhance our algorithm problem-solving skills and improve our &hellip; <a href=\"https:\/\/atmokpo.com\/w\/33299\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;Java Coding Test Course, Finding the Fastest Bus Route&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[139],"tags":[],"class_list":["post-33299","post","type-post","status-publish","format-standard","hentry","category-java-coding-test"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Java Coding Test Course, Finding the Fastest Bus Route - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/atmokpo.com\/w\/33299\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Java Coding Test Course, Finding the Fastest Bus Route - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"In this article, we will address the problem of &#8220;Finding the Fastest Bus Route,&#8221; which may appear in coding tests using Java. The shortest path problem using buses is an excellent example for grasping the basic concepts of greedy algorithms and graph theory. Through this, we will enhance our algorithm problem-solving skills and improve our &hellip; \ub354 \ubcf4\uae30 &quot;Java Coding Test Course, Finding the Fastest Bus Route&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/33299\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:15:18+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:39:17+00:00\" \/>\n<meta name=\"author\" content=\"root\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:site\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:label1\" content=\"\uae00\uc4f4\uc774\" \/>\n\t<meta name=\"twitter:data1\" content=\"root\" \/>\n\t<meta name=\"twitter:label2\" content=\"\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04\" \/>\n\t<meta name=\"twitter:data2\" content=\"4\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/atmokpo.com\/w\/33299\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33299\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"Java Coding Test Course, Finding the Fastest Bus Route\",\"datePublished\":\"2024-11-01T09:15:18+00:00\",\"dateModified\":\"2024-11-01T11:39:17+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33299\/\"},\"wordCount\":533,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Java Coding Test\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/33299\/\",\"url\":\"https:\/\/atmokpo.com\/w\/33299\/\",\"name\":\"Java Coding Test Course, Finding the Fastest Bus Route - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:15:18+00:00\",\"dateModified\":\"2024-11-01T11:39:17+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33299\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/33299\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/33299\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java Coding Test Course, Finding the Fastest Bus Route\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/atmokpo.com\/w\/#website\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/atmokpo.com\/w\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"ko-KR\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"contentUrl\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"width\":400,\"height\":400,\"caption\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\"},\"image\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/bebubo4\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\",\"name\":\"root\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"caption\":\"root\"},\"sameAs\":[\"http:\/\/atmokpo.com\/w\"],\"url\":\"https:\/\/atmokpo.com\/w\/author\/root\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Java Coding Test Course, Finding the Fastest Bus Route - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/atmokpo.com\/w\/33299\/","og_locale":"ko_KR","og_type":"article","og_title":"Java Coding Test Course, Finding the Fastest Bus Route - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"In this article, we will address the problem of &#8220;Finding the Fastest Bus Route,&#8221; which may appear in coding tests using Java. The shortest path problem using buses is an excellent example for grasping the basic concepts of greedy algorithms and graph theory. Through this, we will enhance our algorithm problem-solving skills and improve our &hellip; \ub354 \ubcf4\uae30 \"Java Coding Test Course, Finding the Fastest Bus Route\"","og_url":"https:\/\/atmokpo.com\/w\/33299\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:15:18+00:00","article_modified_time":"2024-11-01T11:39:17+00:00","author":"root","twitter_card":"summary_large_image","twitter_creator":"@bebubo4","twitter_site":"@bebubo4","twitter_misc":{"\uae00\uc4f4\uc774":"root","\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04":"4\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/atmokpo.com\/w\/33299\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/33299\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"Java Coding Test Course, Finding the Fastest Bus Route","datePublished":"2024-11-01T09:15:18+00:00","dateModified":"2024-11-01T11:39:17+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/33299\/"},"wordCount":533,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Java Coding Test"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/33299\/","url":"https:\/\/atmokpo.com\/w\/33299\/","name":"Java Coding Test Course, Finding the Fastest Bus Route - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:15:18+00:00","dateModified":"2024-11-01T11:39:17+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/33299\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/33299\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/33299\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"Java Coding Test Course, Finding the Fastest Bus Route"}]},{"@type":"WebSite","@id":"https:\/\/atmokpo.com\/w\/#website","url":"https:\/\/atmokpo.com\/w\/","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","description":"","publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/atmokpo.com\/w\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"ko-KR"},{"@type":"Organization","@id":"https:\/\/atmokpo.com\/w\/#organization","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","url":"https:\/\/atmokpo.com\/w\/","logo":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/","url":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","contentUrl":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","width":400,"height":400,"caption":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8"},"image":{"@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/bebubo4"]},{"@type":"Person","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7","name":"root","image":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","caption":"root"},"sameAs":["http:\/\/atmokpo.com\/w"],"url":"https:\/\/atmokpo.com\/w\/author\/root\/"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/33299","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/comments?post=33299"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/33299\/revisions"}],"predecessor-version":[{"id":33300,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/33299\/revisions\/33300"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=33299"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=33299"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=33299"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}