{"id":34320,"date":"2024-11-01T09:26:47","date_gmt":"2024-11-01T09:26:47","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=34320"},"modified":"2024-11-01T10:57:43","modified_gmt":"2024-11-01T10:57:43","slug":"c-coding-test-course-minimum-cost-calculation","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/34320\/","title":{"rendered":"C++ Coding Test Course, Minimum Cost Calculation"},"content":{"rendered":"<p><body><\/p>\n<p>Hello! In this post, we will discuss a C++ algorithm problem titled <strong>Finding Minimum Cost<\/strong>. This problem presents a good opportunity to explore algorithmic concepts that are useful for finding optimal solutions in various situations. Through this article, we will delve into problem analysis, algorithm selection, code implementation, testing, and optimization methods in detail.<\/p>\n<h2>Problem Description<\/h2>\n<p>We have several cities and roads between them. Each road has a specific cost. Our goal when traveling between the given cities is to find the minimum travel cost from the starting city to the target city. This can be solved using Dijkstra&#8217;s algorithm.<\/p>\n<h3>Problem Definition<\/h3>\n<p>Here is the mathematical definition of the problem:<\/p>\n<ul>\n<li>The number of given cities <code>n<\/code> (between 1 and 10,000)<\/li>\n<li>The number of paths <code>m<\/code> (between 1 and 100,000)<\/li>\n<li>Each path is given in the form of <code>u<\/code>, <code>v<\/code>, <code>cost<\/code>. This means that the cost to travel from city <code>u<\/code> to city <code>v<\/code> is <code>cost<\/code>.<\/li>\n<li>The starting city <code>start<\/code> and the target city <code>end<\/code> are provided.<\/li>\n<\/ul>\n<h3>Input<\/h3>\n<pre><code>\n    n m\n    u1 v1 cost1\n    u2 v2 cost2\n    ...\n    um vm costm\n    start end\n    <\/code><\/pre>\n<h3>Output<\/h3>\n<p>Output the minimum cost to reach the target city. If it is not possible to reach, output <code>-1<\/code>.<\/p>\n<h2>Algorithm Selection<\/h2>\n<p>This problem involves finding the shortest path in a weighted graph, which can be effectively solved using the representative algorithm <strong>Dijkstra&#8217;s algorithm<\/strong>. Dijkstra&#8217;s algorithm has a time complexity of <code>O((n + m) log n)<\/code>, making it efficient even under maximum input conditions.<\/p>\n<h3>Dijkstra&#8217;s Algorithm Overview<\/h3>\n<p>The basic idea of Dijkstra&#8217;s algorithm is as follows:<\/p>\n<ol>\n<li>Select the starting node and initialize the distance to this node as 0.<\/li>\n<li>Initialize the distance to all other nodes as infinity.<\/li>\n<li>Calculate the distance to the nodes adjacent to the current node, updating the distance if a shorter distance is found.<\/li>\n<li>Repeat this process until all nodes have been visited.<\/li>\n<\/ol>\n<h2>Problem Solving Process<\/h2>\n<h3>Step 1: Problem Analysis<\/h3>\n<p>We need to construct a graph from the given input and a data structure to store the cost information for each city. We will use an adjacency list.<\/p>\n<h3>Step 2: Code Implementation<\/h3>\n<pre><code>\n#include &lt;iostream&gt;\n#include &lt;vector&gt;\n#include &lt;queue&gt;\n#include &lt;utility&gt;\n#include &lt;limits&gt;\n\nusing namespace std;\n\nconst int INF = numeric_limits&lt;int&gt;::max();\n\nvector&lt;pair&lt;int, int&gt;&gt; graph[10001];\n\nvoid dijkstra(int start, int n) {\n    vector&lt;int&gt; cost(n + 1, INF);\n    priority_queue&lt;pair&lt;int, int&gt;, vector&lt;pair&lt;int, int&gt;&gt;, greater&lt;pair&lt;int, int&gt;&gt;&gt; pq;\n\n    cost[start] = 0;\n    pq.push(make_pair(0, start));\n\n    while (!pq.empty()) {\n        int currentCost = pq.top().first;\n        int currentNode = pq.top().second;\n        pq.pop();\n\n        if (currentCost &gt; cost[currentNode]) continue;\n\n        for (auto &amp;edge : graph[currentNode]) {\n            int nextNode = edge.first;\n            int nextCost = edge.second;\n\n            if (cost[currentNode] + nextCost &lt; cost[nextNode]) {\n                cost[nextNode] = cost[currentNode] + nextCost;\n                pq.push(make_pair(cost[nextNode], nextNode));\n            }\n        }\n    }\n\n    for (int i = 1; i &lt;= n; ++i) {\n        if (cost[i] == INF) {\n            cout &lt;&lt; -1 &lt;&lt; endl;  \/\/ unreachable case\n        } else {\n            cout &lt;&lt; cost[i] &lt;&lt; endl;\n        }\n    }\n}\n\nint main() {\n    int n, m;\n    cin &gt;&gt; n &gt;&gt; m;\n\n    for (int i = 0; i &lt; m; ++i) {\n        int u, v, cost;\n        cin &gt;&gt; u &gt;&gt; v &gt;&gt; cost;\n        graph[u].push_back(make_pair(v, cost));\n        graph[v].push_back(make_pair(u, cost));  \/\/ assuming bidirectional roads\n    }\n\n    int start, end;\n    cin &gt;&gt; start &gt;&gt; end;\n\n    dijkstra(start, n);\n    \n    return 0;\n}\n    <\/code><\/pre>\n<h3>Step 3: Code Testing<\/h3>\n<p>Test the input through the code above. For example, you can test using the following input:<\/p>\n<pre><code>\n5 6\n1 2 1\n1 3 4\n2 3 2\n2 4 5\n3 4 1\n4 5 3\n1 5\n    <\/code><\/pre>\n<p>The above input represents a case with 5 cities and 6 roads, where the starting city is 1 and the target city is 5. In this case, the algorithm should output the method to reach from city 1 to city 5 with the minimum cost.<\/p>\n<h3>Step 4: Optimization<\/h3>\n<p>Dijkstra\u2019s algorithm is typically implemented using a Priority Queue; however, in limited cases, a Fibonacci Heap might be used. In this article, we demonstrated a simple and efficient implementation using a standard Priority Queue.<\/p>\n<h2>Conclusion<\/h2>\n<p>In this session, we examined how to solve the minimum cost problem using C++. We understood the principles of Dijkstra&#8217;s algorithm and how to apply it to real problems. This will be helpful in preparing for coding tests.<\/p>\n<p>Continue learning and practicing how to solve more algorithm problems. Thank you!<\/p>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello! In this post, we will discuss a C++ algorithm problem titled Finding Minimum Cost. This problem presents a good opportunity to explore algorithmic concepts that are useful for finding optimal solutions in various situations. Through this article, we will delve into problem analysis, algorithm selection, code implementation, testing, and optimization methods in detail. Problem &hellip; <a href=\"https:\/\/atmokpo.com\/w\/34320\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;C++ Coding Test Course, Minimum Cost Calculation&#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-34320","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, Minimum Cost Calculation - \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\/34320\/\" \/>\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, Minimum Cost Calculation - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Hello! In this post, we will discuss a C++ algorithm problem titled Finding Minimum Cost. This problem presents a good opportunity to explore algorithmic concepts that are useful for finding optimal solutions in various situations. Through this article, we will delve into problem analysis, algorithm selection, code implementation, testing, and optimization methods in detail. Problem &hellip; \ub354 \ubcf4\uae30 &quot;C++ Coding Test Course, Minimum Cost Calculation&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/34320\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:26:47+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T10:57:43+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\/34320\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34320\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"C++ Coding Test Course, Minimum Cost Calculation\",\"datePublished\":\"2024-11-01T09:26:47+00:00\",\"dateModified\":\"2024-11-01T10:57:43+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34320\/\"},\"wordCount\":468,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"C++ Coding Test Tutorials\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/34320\/\",\"url\":\"https:\/\/atmokpo.com\/w\/34320\/\",\"name\":\"C++ Coding Test Course, Minimum Cost Calculation - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:26:47+00:00\",\"dateModified\":\"2024-11-01T10:57:43+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34320\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/34320\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/34320\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"C++ Coding Test Course, Minimum Cost Calculation\"}]},{\"@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, Minimum Cost Calculation - \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\/34320\/","og_locale":"ko_KR","og_type":"article","og_title":"C++ Coding Test Course, Minimum Cost Calculation - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Hello! In this post, we will discuss a C++ algorithm problem titled Finding Minimum Cost. This problem presents a good opportunity to explore algorithmic concepts that are useful for finding optimal solutions in various situations. Through this article, we will delve into problem analysis, algorithm selection, code implementation, testing, and optimization methods in detail. Problem &hellip; \ub354 \ubcf4\uae30 \"C++ Coding Test Course, Minimum Cost Calculation\"","og_url":"https:\/\/atmokpo.com\/w\/34320\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:26:47+00:00","article_modified_time":"2024-11-01T10:57:43+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\/34320\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/34320\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"C++ Coding Test Course, Minimum Cost Calculation","datePublished":"2024-11-01T09:26:47+00:00","dateModified":"2024-11-01T10:57:43+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/34320\/"},"wordCount":468,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["C++ Coding Test Tutorials"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/34320\/","url":"https:\/\/atmokpo.com\/w\/34320\/","name":"C++ Coding Test Course, Minimum Cost Calculation - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:26:47+00:00","dateModified":"2024-11-01T10:57:43+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/34320\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/34320\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/34320\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"C++ Coding Test Course, Minimum Cost Calculation"}]},{"@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\/34320","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=34320"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34320\/revisions"}],"predecessor-version":[{"id":34321,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34320\/revisions\/34321"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=34320"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=34320"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=34320"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}