{"id":34124,"date":"2024-11-01T09:24:27","date_gmt":"2024-11-01T09:24:27","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=34124"},"modified":"2024-11-01T10:58:31","modified_gmt":"2024-11-01T10:58:31","slug":"c-coding-test-course-finding-the-fastest-bus-route-2","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/34124\/","title":{"rendered":"C++ Coding Test Course, Finding the Fastest Bus Route"},"content":{"rendered":"<p><body><\/p>\n<h2>Problem Description<\/h2>\n<p>\n        In a city, multiple bus routes are operated, and each route circulates through designated stops.<br \/>\n        You need to find the fastest route from stop A to stop B.<br \/>\n        The bus departs at scheduled times, and you need to consider the time spent at each stop and the travel times.<br \/>\n        Given the data below, write an algorithm to find the fastest bus route.\n    <\/p>\n<h2>Input Format<\/h2>\n<ul>\n<li>The first line contains the number of stops N (1 \u2264 N \u2264 1000) and the number of bus routes M (1 \u2264 M \u2264 10000).<\/li>\n<li>From the second line to M lines, information about each bus route is provided in the following format:<br \/>\n            <br \/><strong>Starting Stop, Ending Stop, Travel Time<\/strong>\n<\/li>\n<li>The last line contains the numbers of stop A and stop B.<\/li>\n<\/ul>\n<h2>Output Format<\/h2>\n<p>On the first line, print the shortest time (in minutes) to get from stop A to stop B.<br \/>\n       If it is not possible to reach, print -1.<\/p>\n<h2>Example Input<\/h2>\n<pre>\n    5 7\n    1 2 10\n    1 3 20\n    2 3 5\n    2 4 10\n    3 4 10\n    4 5 15\n    1 5\n    <\/pre>\n<h2>Example Output<\/h2>\n<pre>\n    25\n    <\/pre>\n<h2>Solution Process<\/h2>\n<p>\n        To solve this problem, we need to use a shortest path algorithm.<br \/>\n        The given graph is a weighted directed graph, so Dijkstra&#8217;s algorithm is appropriate.<br \/>\n        Dijkstra&#8217;s algorithm is a method to find the shortest path from a single starting point to all other vertices,<br \/>\n        and can be efficiently applied even when weights are present.\n    <\/p>\n<h3>Basic Principle of Dijkstra&#8217;s Algorithm<\/h3>\n<p>\n        Dijkstra&#8217;s algorithm operates on the following basic principles:<\/p>\n<ol>\n<li>Set the starting vertex to initialize the shortest path to 0, and initialize other vertices to infinity.<\/li>\n<li>Select the vertex with the shortest path among the unprocessed vertices.<\/li>\n<li>Update the paths of adjacent vertices of the selected vertex.<\/li>\n<li>Repeat this process until all vertices have been processed.<\/li>\n<\/ol>\n<h3>C++ Code Implementation<\/h3>\n<p>Here is a C++ code implementation based on the above algorithm.<\/p>\n<pre>\n    #include <iostream>\n    #include <vector>\n    #include <queue>\n    #include <limits>\n\n    using namespace std;\n\n    const int INF = numeric_limits<int>::max();\n\n    void dijkstra(int start, int n, vector<vector<pair<int, int>>&gt;&gt; &amp;graph, vector<int> &amp;dist) {\n        priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;\n        dist[start] = 0;\n        pq.push({0, start});\n\n        while (!pq.empty()) {\n            int u = pq.top().second;\n            pq.pop();\n\n            for (auto &amp;neighbor : graph[u]) {\n                int v = neighbor.first;\n                int weight = neighbor.second;\n                \n                if (dist[u] + weight < dist[v]) {\n                    dist[v] = dist[u] + weight;\n                    pq.push({dist[v], v});\n                }\n            }\n        }\n    }\n\n    int main() {\n        int N, M;\n        cin >> N >> M;\n        vector<vector<pair<int, int>>> graph(N + 1); \/\/ N stops\n\n        for (int i = 0; i < M; i++) {\n            int u, v, w;\n            cin >> u >> v >> w;\n            graph[u].push_back({v, w});\n        }\n\n        int A, B;\n        cin >> A >> B;\n\n        vector<int> dist(N + 1, INF);\n        dijkstra(A, N, graph, dist);\n\n        if (dist[B] == INF) {\n            cout << -1 << endl;\n        } else {\n            cout << dist[B] << endl;\n        }\n\n        return 0;\n    }\n    <\/int><\/vector<pair<int,><\/pair<int,><\/pair<int,><\/pair<int,><\/int><\/vector<pair<int,><\/int><\/limits><\/queue><\/vector><\/iostream><\/pre>\n<h2>Code Explanation<\/h2>\n<p>\n        The above code implements a method to find the shortest path from the given stop using Dijkstra&#8217;s algorithm.<br \/>\n        The main steps are as follows:<\/p>\n<ol>\n<li>Store the graph in the adjacency list format based on the given input.<\/li>\n<li>Call the Dijkstra function to find the shortest path from the starting stop to other stops.<\/li>\n<li>As a result, if the shortest path to the destination stop is infinity, it means it cannot be reached, so print -1;<br \/>\n            otherwise, print the time taken for the shortest path.<\/li>\n<\/ol>\n<h2>Conclusion<\/h2>\n<p>\n        In this lecture, we addressed the problem of finding the shortest path between the starting point and destination in a graph with multiple bus routes and stops.<br \/>\n        We learned the concept of finding the shortest path using Dijkstra&#8217;s algorithm and how to implement it.<br \/>\n        Based on this, we have laid the foundation to solve other similar problems.\n    <\/p>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem Description In a city, multiple bus routes are operated, and each route circulates through designated stops. You need to find the fastest route from stop A to stop B. The bus departs at scheduled times, and you need to consider the time spent at each stop and the travel times. Given the data below, &hellip; <a href=\"https:\/\/atmokpo.com\/w\/34124\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;C++ 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":[111],"tags":[],"class_list":["post-34124","post","type-post","status-publish","format-standard","hentry","category-c-coding-test-tutorials-2"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>C++ 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\/34124\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"C++ Coding Test Course, Finding the Fastest Bus Route - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Problem Description In a city, multiple bus routes are operated, and each route circulates through designated stops. You need to find the fastest route from stop A to stop B. The bus departs at scheduled times, and you need to consider the time spent at each stop and the travel times. Given the data below, &hellip; \ub354 \ubcf4\uae30 &quot;C++ Coding Test Course, Finding the Fastest Bus Route&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/34124\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:24:27+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T10:58:31+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=\"2\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/atmokpo.com\/w\/34124\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34124\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"C++ Coding Test Course, Finding the Fastest Bus Route\",\"datePublished\":\"2024-11-01T09:24:27+00:00\",\"dateModified\":\"2024-11-01T10:58:31+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34124\/\"},\"wordCount\":441,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"C++ Coding Test Tutorials\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/34124\/\",\"url\":\"https:\/\/atmokpo.com\/w\/34124\/\",\"name\":\"C++ 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:24:27+00:00\",\"dateModified\":\"2024-11-01T10:58:31+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34124\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/34124\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/34124\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"C++ 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":"C++ 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\/34124\/","og_locale":"ko_KR","og_type":"article","og_title":"C++ Coding Test Course, Finding the Fastest Bus Route - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Problem Description In a city, multiple bus routes are operated, and each route circulates through designated stops. You need to find the fastest route from stop A to stop B. The bus departs at scheduled times, and you need to consider the time spent at each stop and the travel times. Given the data below, &hellip; \ub354 \ubcf4\uae30 \"C++ Coding Test Course, Finding the Fastest Bus Route\"","og_url":"https:\/\/atmokpo.com\/w\/34124\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:24:27+00:00","article_modified_time":"2024-11-01T10:58:31+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":"2\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/atmokpo.com\/w\/34124\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/34124\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"C++ Coding Test Course, Finding the Fastest Bus Route","datePublished":"2024-11-01T09:24:27+00:00","dateModified":"2024-11-01T10:58:31+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/34124\/"},"wordCount":441,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["C++ Coding Test Tutorials"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/34124\/","url":"https:\/\/atmokpo.com\/w\/34124\/","name":"C++ 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:24:27+00:00","dateModified":"2024-11-01T10:58:31+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/34124\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/34124\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/34124\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"C++ 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\/34124","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=34124"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34124\/revisions"}],"predecessor-version":[{"id":34125,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34124\/revisions\/34125"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=34124"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=34124"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=34124"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}