{"id":34118,"date":"2024-11-01T09:24:21","date_gmt":"2024-11-01T09:24:21","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=34118"},"modified":"2024-11-01T10:58:32","modified_gmt":"2024-11-01T10:58:32","slug":"c-coding-test-course-finding-the-kth-shortest-path","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/34118\/","title":{"rendered":"C++ Coding Test Course, Finding the Kth Shortest Path"},"content":{"rendered":"<p>Hello! In this course, we will talk about one of the algorithm problems, &#8220;Finding the K-th Shortest Path&#8221;. This problem frequently appears in real coding tests and requires a deep understanding of graphs and shortest path algorithms. Through this problem, we will learn how to utilize Dijkstra&#8217;s algorithm and priority queues.<\/p>\n<h2>Problem Description<\/h2>\n<p>The problem is to find the length of the K-th shortest path between two vertices A and B in the given graph. The shortest path refers to the path with the shortest length among multiple paths, and the K-th shortest path refers to the shortest path that is longer than the shortest path. The same path can exist multiple times.<\/p>\n<h3>Input<\/h3>\n<ul>\n<li>Number of vertices N (1 \u2264 N \u2264 400)<\/li>\n<li>Number of edges M (1 \u2264 M \u2264 100,000)<\/li>\n<li>K (1 \u2264 K \u2264 3000)<\/li>\n<li>Information of each edge (starting vertex, ending vertex, weight)<\/li>\n<li>Vertices A and B<\/li>\n<\/ul>\n<h3>Output<\/h3>\n<p>Print the length of the K-th shortest path. If the K-th shortest path does not exist, print -1.<\/p>\n<h2>Problem Approach<\/h2>\n<p>To solve this problem, we need to think about how to find the K-th shortest path. The standard Dijkstra&#8217;s algorithm is effective for finding the shortest path, but additional measures are needed to find the K-th path.<\/p>\n<p>The data structures used to solve this problem are as follows:<\/p>\n<ul>\n<li>Priority Queue: Ensures that the smallest value is handled first.<\/li>\n<li>Vector: Used to represent the graph structure.<\/li>\n<\/ul>\n<h3>Algorithm Steps<\/h3>\n<ol>\n<li>Input the graph and represent it as an adjacent list.<\/li>\n<li>Store the path list for each vertex in a priority queue.<\/li>\n<li>Use Dijkstra&#8217;s algorithm from the starting vertex to find the K-th shortest path.<\/li>\n<li>Find and print the K-th smallest value from the path list.<\/li>\n<\/ol>\n<h2>C++ Code Implementation<\/h2>\n<pre>\n<code>\n#include &lt;iostream&gt;\n#include &lt;vector&gt;\n#include &lt;queue&gt;\n#include &lt;tuple&gt;\n#include &lt;algorithm&gt;\n\nusing namespace std;\n\nconst int INF = 1e9;  \/\/ Definition of infinity\n\nstruct Edge {\n    int to, weight;\n};\n\nint N, M, K;\nvector&lt;Edge&gt; graph[401];\nvector&lt;int&gt; dist[401];  \/\/ Length of K-th shortest path for each vertex\n\nvoid findKthShortestPath(int start, int end) {\n    priority_queue&lt;tuple&lt;int, int, int&gt;, vector&lt;tuple&lt;int, int, int&gt;&gt;, greater&lt;tuple&lt;int, int, int&gt;&gt;&gt; pq;\n    pq.push(make_tuple(0, start, 0));  \/\/ (length, current vertex, number of paths)\n    \n    while (!pq.empty()) {\n        auto [length, current, count] = pq.top();\n        pq.pop();\n        \n        if (count &gt;= K) continue; \/\/ No need to explore more than K paths\n        dist[current].push_back(length);\n        \n        for (auto &amp;edge : graph[current]) {\n            int next = edge.to;\n            int weight = edge.weight;\n            pq.push(make_tuple(length + weight, next, count + 1));\n        }\n    }\n}\n\nint main() {\n    cin &gt;&gt; N &gt;&gt; M &gt;&gt; K; \/\/ Getting vertices, edges, and K\n    for (int i = 0; i &lt; M; i++) {\n        int u, v, w;\n        cin &gt;&gt; u &gt;&gt; v &gt;&gt; w;\n        graph[u].push_back({v, w}); \/\/ Adding edge\n    }\n\n    int A, B;\n    cin &gt;&gt; A &gt;&gt; B;\n\n    findKthShortestPath(A, B);\n    \n    if (dist[B].size() &lt; K) {\n        cout &lt;&lt; -1 &lt;&lt; endl; \/\/ Return -1 if K-th shortest path does not exist\n    } else {\n        cout &lt;&lt; dist[B][K-1] &lt;&lt; endl; \/\/ Print K-th shortest path\n    }\n\n    return 0;\n}\n<\/code>\n<\/pre>\n<h2>Conclusion<\/h2>\n<p>In this course, we have explored various methods of graph traversal and efficient pathfinding using priority queues through the problem of finding the K-th shortest path. In the process of solving this problem, we learned how to find the K-th path by modifying Dijkstra&#8217;s algorithm. Understanding such problems will provide opportunities to achieve high scores in actual coding tests.<\/p>\n<p>I hope you also improve your understanding of graph traversal and elevate your coding skills by practicing this problem. Thank you!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello! In this course, we will talk about one of the algorithm problems, &#8220;Finding the K-th Shortest Path&#8221;. This problem frequently appears in real coding tests and requires a deep understanding of graphs and shortest path algorithms. Through this problem, we will learn how to utilize Dijkstra&#8217;s algorithm and priority queues. Problem Description The problem &hellip; <a href=\"https:\/\/atmokpo.com\/w\/34118\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;C++ Coding Test Course, Finding the Kth Shortest Path&#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-34118","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 Kth Shortest Path - \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\/34118\/\" \/>\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 Kth Shortest Path - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Hello! In this course, we will talk about one of the algorithm problems, &#8220;Finding the K-th Shortest Path&#8221;. This problem frequently appears in real coding tests and requires a deep understanding of graphs and shortest path algorithms. Through this problem, we will learn how to utilize Dijkstra&#8217;s algorithm and priority queues. Problem Description The problem &hellip; \ub354 \ubcf4\uae30 &quot;C++ Coding Test Course, Finding the Kth Shortest Path&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/34118\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:24:21+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T10:58:32+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=\"3\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/atmokpo.com\/w\/34118\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34118\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"C++ Coding Test Course, Finding the Kth Shortest Path\",\"datePublished\":\"2024-11-01T09:24:21+00:00\",\"dateModified\":\"2024-11-01T10:58:32+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34118\/\"},\"wordCount\":369,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"C++ Coding Test Tutorials\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/34118\/\",\"url\":\"https:\/\/atmokpo.com\/w\/34118\/\",\"name\":\"C++ Coding Test Course, Finding the Kth Shortest Path - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:24:21+00:00\",\"dateModified\":\"2024-11-01T10:58:32+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34118\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/34118\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/34118\/#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 Kth Shortest Path\"}]},{\"@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 Kth Shortest Path - \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\/34118\/","og_locale":"ko_KR","og_type":"article","og_title":"C++ Coding Test Course, Finding the Kth Shortest Path - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Hello! In this course, we will talk about one of the algorithm problems, &#8220;Finding the K-th Shortest Path&#8221;. This problem frequently appears in real coding tests and requires a deep understanding of graphs and shortest path algorithms. Through this problem, we will learn how to utilize Dijkstra&#8217;s algorithm and priority queues. Problem Description The problem &hellip; \ub354 \ubcf4\uae30 \"C++ Coding Test Course, Finding the Kth Shortest Path\"","og_url":"https:\/\/atmokpo.com\/w\/34118\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:24:21+00:00","article_modified_time":"2024-11-01T10:58:32+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":"3\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/atmokpo.com\/w\/34118\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/34118\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"C++ Coding Test Course, Finding the Kth Shortest Path","datePublished":"2024-11-01T09:24:21+00:00","dateModified":"2024-11-01T10:58:32+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/34118\/"},"wordCount":369,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["C++ Coding Test Tutorials"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/34118\/","url":"https:\/\/atmokpo.com\/w\/34118\/","name":"C++ Coding Test Course, Finding the Kth Shortest Path - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:24:21+00:00","dateModified":"2024-11-01T10:58:32+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/34118\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/34118\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/34118\/#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 Kth Shortest Path"}]},{"@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\/34118","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=34118"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34118\/revisions"}],"predecessor-version":[{"id":34119,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34118\/revisions\/34119"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=34118"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=34118"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=34118"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}