<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>다익스트라 Archives - 어제와 내일의 나 그 사이의 이야기</title>
	<atom:link href="https://lycos7560.com/tag/%EB%8B%A4%EC%9D%B5%EC%8A%A4%ED%8A%B8%EB%9D%BC/feed/" rel="self" type="application/rss+xml" />
	<link></link>
	<description>생각의 흐름을 타고 다니며 만드는 블로그</description>
	<lastBuildDate>Wed, 15 Oct 2025 05:32:31 +0000</lastBuildDate>
	<language>ko-KR</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://lycos7560.com/wp-content/uploads/2022/11/cropped-cropped-cropped-log-1-150x150-1-80x80.png</url>
	<title>다익스트라 Archives - 어제와 내일의 나 그 사이의 이야기</title>
	<link></link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>알고리즘 &#8211; 벨만-포드 (Bellman–Ford Algorithm) 알고리즘 정리</title>
		<link>https://lycos7560.com/etc/%ec%95%8c%ea%b3%a0%eb%a6%ac%ec%a6%98-%eb%b2%a8%eb%a7%8c-%ed%8f%ac%eb%93%9c-bellman-ford-algorithm-%ec%95%8c%ea%b3%a0%eb%a6%ac%ec%a6%98-%ec%a0%95%eb%a6%ac/33044/</link>
					<comments>https://lycos7560.com/etc/%ec%95%8c%ea%b3%a0%eb%a6%ac%ec%a6%98-%eb%b2%a8%eb%a7%8c-%ed%8f%ac%eb%93%9c-bellman-ford-algorithm-%ec%95%8c%ea%b3%a0%eb%a6%ac%ec%a6%98-%ec%a0%95%eb%a6%ac/33044/#respond</comments>
		
		<dc:creator><![CDATA[lycos7560]]></dc:creator>
		<pubDate>Fri, 10 Feb 2023 18:17:44 +0000</pubDate>
				<category><![CDATA[기타]]></category>
		<category><![CDATA[algorithm]]></category>
		<category><![CDATA[Baekjoon]]></category>
		<category><![CDATA[Bellman–Ford]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[cpp]]></category>
		<category><![CDATA[Dijkstra]]></category>
		<category><![CDATA[DP]]></category>
		<category><![CDATA[DynamicProgramming]]></category>
		<category><![CDATA[study]]></category>
		<category><![CDATA[공부]]></category>
		<category><![CDATA[기본]]></category>
		<category><![CDATA[기초]]></category>
		<category><![CDATA[다이나믹 프로그래밍]]></category>
		<category><![CDATA[다익스트라]]></category>
		<category><![CDATA[백준]]></category>
		<category><![CDATA[벨만-포드]]></category>
		<category><![CDATA[알고리즘]]></category>
		<category><![CDATA[코딩테스트]]></category>
		<category><![CDATA[코테]]></category>
		<guid isPermaLink="false">https://lycos7560.com/?p=33044</guid>

					<description><![CDATA[<p>벨만-포드 (Bellman–Ford Algorithm) 알고리즘을 정리한 내용의 글입니다. (This article summarizes Bellman-Ford algorithm.)</p>
<p>The post <a href="https://lycos7560.com/etc/%ec%95%8c%ea%b3%a0%eb%a6%ac%ec%a6%98-%eb%b2%a8%eb%a7%8c-%ed%8f%ac%eb%93%9c-bellman-ford-algorithm-%ec%95%8c%ea%b3%a0%eb%a6%ac%ec%a6%98-%ec%a0%95%eb%a6%ac/33044/">알고리즘 &#8211; 벨만-포드 (Bellman–Ford Algorithm) 알고리즘 정리</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-5698326622209671" crossorigin="anonymous"></script>
<!-- HorizontalAD -->
<ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-5698326622209671" data-ad-slot="6908948342" data-ad-format="auto" data-full-width-responsive="true"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>



<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>


				<div class="wp-block-uagb-table-of-contents uagb-toc__align-left uagb-toc__columns-1  uagb-block-0138b7d4      "
					data-scroll= "1"
					data-offset= "30"
					style=""
				>
				<div class="uagb-toc__wrap">
						<div class="uagb-toc__title">
							목차 테이블						</div>
																						<div class="uagb-toc__list-wrap ">
						<ol class="uagb-toc__list"><li class="uagb-toc__list"><a href="#벨만-포드-bellmanford-algorithm-알고리즘" class="uagb-toc-link__trigger">벨만-포드 (Bellman–Ford Algorithm) 알고리즘</a><ul class="uagb-toc__list"><li class="uagb-toc__list"><a href="#작동하는-과정" class="uagb-toc-link__trigger">작동하는 과정</a><li class="uagb-toc__list"><li class="uagb-toc__list"><a href="#주의-사항" class="uagb-toc-link__trigger">주의 사항</a><li class="uagb-toc__list"><li class="uagb-toc__list"><a href="#구현-코드" class="uagb-toc-link__trigger">구현 코드</a><ul class="uagb-toc__list"><li class="uagb-toc__list"><a href="#코드-c" class="uagb-toc-link__trigger">코드 C++</a></li></ul><li class="uagb-toc__list"><a href="#dijkstra-알고리즘-vs-bellmanford-알고리즘" class="uagb-toc-link__trigger">Dijkstra 알고리즘 vs Bellman–Ford 알고리즘</a><ul class="uagb-toc__list"><li class="uagb-toc__list"><a href="#1-가중치의-차이" class="uagb-toc-link__trigger">1. 가중치의 차이</a><li class="uagb-toc__list"><li class="uagb-toc__list"><a href="#2-시간-복잡도의-차이" class="uagb-toc-link__trigger">2. 시간 복잡도의 차이</a></ul></ul></ol>					</div>
									</div>
				</div>
			


<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>



<h1 class="has-large-font-size wp-block-heading">벨만-포드 (Bellman–Ford Algorithm) 알고리즘</h1>



<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>



<p class="has-medium-font-size">Bellman-Ford 알고리즘은 (Richard Bellman과 Lester Ford의 이름)</p>



<p class="has-medium-font-size">가중 그래프에서 정점과 다른 모든 정점 사이의 최단 경로를 찾는 데 사용되는 최단 경로 알고리즘입니다.</p>



<p class="has-medium-font-size">간단하게 말하면<strong> <mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-ast-global-color-8-color">한 정점에서 다른 정점까지의 최단 거리를 구하는 알고리즘</mark></strong>입니다.</p>



<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">작동하는 과정</h2>



<p><a href="https://www.geeksforgeeks.org/bellman-ford-algorithm-dp-23/" target="_blank" rel="noreferrer noopener">https://www.geeksforgeeks.org/bellman-ford-algorithm-dp-23/</a></p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<figure class="wp-block-image size-full"><img fetchpriority="high" decoding="async" width="551" height="327" src="https://lycos7560.com/wp-content/uploads/2023/02/image-57.png" alt="" class="wp-image-33065" srcset="https://lycos7560.com/wp-content/uploads/2023/02/image-57.png 551w, https://lycos7560.com/wp-content/uploads/2023/02/image-57-300x178.png 300w" sizes="(max-width: 551px) 100vw, 551px" /></figure>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<p class="has-medium-font-size"><strong>1단계:</strong>&nbsp;출발 정점을 0으로 둡니다. </p>



<p class="has-medium-font-size">출발 정점의 거리를 제외하고 모든 거리를 무한으로 초기화합니다.&nbsp;</p>



<hr class="wp-block-separator has-alpha-channel-opacity is-style-dots" style="margin-top:var(--wp--preset--spacing--80);margin-bottom:var(--wp--preset--spacing--80)"/>



<figure class="wp-block-image size-full"><img decoding="async" width="548" height="330" src="https://lycos7560.com/wp-content/uploads/2023/02/image-60.png" alt="" class="wp-image-33068" srcset="https://lycos7560.com/wp-content/uploads/2023/02/image-60.png 548w, https://lycos7560.com/wp-content/uploads/2023/02/image-60-300x181.png 300w" sizes="(max-width: 548px) 100vw, 548px" /></figure>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<p class="has-medium-font-size"><strong>2단계:</strong>&nbsp;모든 정점을 다음 순서로 처리합니다</p>



<p class="has-medium-font-size">(B, E), (D, B), (B, D), (A, B), (A, C), (D, C), (B, C), (E, D) </p>



<p class="has-medium-font-size">모든 모서리가 처음으로 처리될 때 다음과 같은 거리를 얻습니다.&nbsp;</p>



<p class="has-medium-font-size">첫 번째 행은 초기의 거리를 보여줍니다.&nbsp;</p>



<p class="has-medium-font-size">두 번째 행은 가장자리 (B, E), (D, B), (B, D) 및 (A, B)가 처리될 때 거리를 보여줍니다.&nbsp;</p>



<p class="has-medium-font-size">세 번째 행은 (A, C)가 처리될 때의 거리를 보여줍니다.&nbsp;</p>



<p class="has-medium-font-size">네 번째 행은 (D, C), (B, C) 및 (E, D) 를 처리한 거리를 보여줍니다.</p>



<hr class="wp-block-separator has-alpha-channel-opacity is-style-dots" style="margin-top:var(--wp--preset--spacing--80);margin-bottom:var(--wp--preset--spacing--80)"/>



<figure class="wp-block-image size-full"><img decoding="async" width="658" height="321" src="https://lycos7560.com/wp-content/uploads/2023/02/image-61.png" alt="" class="wp-image-33075" srcset="https://lycos7560.com/wp-content/uploads/2023/02/image-61.png 658w, https://lycos7560.com/wp-content/uploads/2023/02/image-61-300x146.png 300w" sizes="(max-width: 658px) 100vw, 658px" /></figure>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<p class="has-medium-font-size"><strong>3단계:</strong>&nbsp;첫 번째 반복은 최대 1 정점 길이의 모든 최단 경로를 제공합니다.</p>



<p class="has-medium-font-size">모든 정점이 두 번째로 처리 될 때 다음과 같은 거리를 얻습니다 (마지막 행은 최종 값을 나타냅니다).</p>



<hr class="wp-block-separator has-alpha-channel-opacity is-style-dots" style="margin-top:var(--wp--preset--spacing--80);margin-bottom:var(--wp--preset--spacing--80)"/>



<p class="has-medium-font-size"><strong>4단계:</strong>&nbsp;두 번째 반복은 최대 2 개의 가장자리 길이인 모든 최단 경로를 제공합니다.</p>



<p class="has-medium-font-size">알고리즘은 모든 정점을 2번 더 처리합니다.</p>



<p class="has-medium-font-size">두 번째 반복 후에는 거리가 최소화되므로 </p>



<p class="has-medium-font-size">세 번째 및 네 번째 반복에서는 거리가 업데이트되지 않습니다.</p>



<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">주의 사항</h2>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<p class="has-medium-font-size">Bellman-Ford 알고리즘을 사용할 경우 아래와 같은 음의 가중치 주기를 주의해야 합니다.</p>



<figure class="wp-block-image size-full"><img decoding="async" width="364" height="346" src="https://lycos7560.com/wp-content/uploads/2023/02/image-63.png" alt="" class="wp-image-33094" srcset="https://lycos7560.com/wp-content/uploads/2023/02/image-63.png 364w, https://lycos7560.com/wp-content/uploads/2023/02/image-63-300x285.png 300w" sizes="(max-width: 364px) 100vw, 364px" /><figcaption class="wp-element-caption"><a href="https://www.techiedelight.com/determine-negative-weight-cycle-graph/" target="_blank" rel="noreferrer noopener">https://www.techiedelight.com/determine-negative-weight-cycle-graph/</a></figcaption></figure>



<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>



<p class="has-medium-font-size">위의 예제는 1 -&gt; 2 -&gt; 3 순서를 계속 반복하면 가중치가  계속 줄어듭니다. (잘못된 결과)</p>



<p class="has-medium-font-size">위와 같은 상황에서는 Bellman-Ford 알고리즘으로 답을 구할 수 없습니다.</p>



<p class="has-medium-font-size">그래프에 음의 가중치 주기가 포함되어 있는지 확인하려면 </p>



<p class="has-medium-font-size">Bellman-Ford 알고리즘 이후에 모든 간선을 다시 한번 확인합니다.</p>



<p class="has-medium-font-size"><strong>만약 업데이트가 일어난다면 음의 가중치가 포함되어있다는 의미</strong> 입니다.</p>



<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">구현 코드</h2>



<p class="has-medium-font-size">백준 11657번 &#8216;타임머신&#8217; 문제를  Bellman-Ford 알고리즘 코드 해결</p>



<figure class="wp-block-embed is-type-wp-embed is-provider-어제와-내일의-나-그-사이의-이야기 wp-block-embed-어제와-내일의-나-그-사이의-이야기"><div class="wp-block-embed__wrapper">
<blockquote class="wp-embedded-content" data-secret="Baal9ouENg"><a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-11657%eb%b2%88-%ed%83%80%ec%9e%84%eb%a8%b8%ec%8b%a0-c-bellman-ford-baekjoon/33077/">백준 11657번 (타임머신, C++, Bellman–Ford) [BAEKJOON]</a></blockquote><iframe loading="lazy" class="wp-embedded-content" sandbox="allow-scripts" security="restricted"  title="&#8220;백준 11657번 (타임머신, C++, Bellman–Ford) [BAEKJOON]&#8221; &#8212; 어제와 내일의 나 그 사이의 이야기" src="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-11657%eb%b2%88-%ed%83%80%ec%9e%84%eb%a8%b8%ec%8b%a0-c-bellman-ford-baekjoon/33077/embed/#?secret=AeoFCex2O2#?secret=Baal9ouENg" data-secret="Baal9ouENg" width="600" height="338" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe>
</div></figure>



<figure class="wp-block-image size-full"><img decoding="async" width="1465" height="1101" src="https://lycos7560.com/wp-content/uploads/2023/02/image-64.png" alt="" class="wp-image-33098" srcset="https://lycos7560.com/wp-content/uploads/2023/02/image-64.png 1465w, https://lycos7560.com/wp-content/uploads/2023/02/image-64-300x225.png 300w, https://lycos7560.com/wp-content/uploads/2023/02/image-64-768x577.png 768w" sizes="(max-width: 1465px) 100vw, 1465px" /></figure>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading">코드 C++</h3>



<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>



<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">#include &lt;iostream>
#include &lt;vector>

using namespace std;

constexpr long long int INF = INT64_MAX;

constexpr int MAXN = 501;

long long int disArr[MAXN];

bool check = false; // 음의 순환이 있는지 확인

// N : 도시의 개수, M : 버스 노선의 개수
// A : 시작 도시, B : 도착 도시, C : 걸리는 시간
int N, M, A, B, C;

vector&lt;pair&lt;int, int>> graph[MAXN];

int main()
{
	ios_base::sync_with_stdio(false); // scanf와 동기화를 비활성화
	// cin.tie(null); 코드는 cin과 cout의 묶음을 풀어줍니다.
	cin.tie(NULL);
	std::cout.tie(NULL);

	cin >> N >> M;

	for (int i = 0; i &lt; M; i++) {
		cin >> A >> B >> C;
		// 단방향
		graph[A].push_back(make_pair(B, C));
	}

	// 배열 초기화
	fill(disArr, disArr + MAXN, INF);

	disArr[1] = 0;


	for (int k = 1; k &lt;= N; k++) {// (모든 정점의 수 - 1) 번 확인한다.
		// 모든 노선을 확인한다.
		for (int i = 1; i &lt;= N; i++) {
			for (int j = 0; j &lt; graph[i].size(); j++) {

				int u = i; // 시작점
				int v = graph[i][j].first; // 도착점
				int weight = graph[i][j].second; // 가중치

				// 만약 임시 배열이 무한대가 아니고 &amp;&amp;
				// 시작 임시 배열의 가중치가 도착지의 가중치보다 작다면
				if (disArr[u] != INF &amp;&amp; disArr[u] + weight &lt; disArr[v]) {

					disArr[v] = disArr[u] + weight; // 임시배열을 업데이트 해준다.

					// K가 N-1일때 모든 정점을 확인한 후
					// K가 N일때 업데이트가 있다면 음의 순환이 있다는 이야기
					if (k == N) { 
						cout &lt;&lt; "-1";
						return 0;
					}
				}
			}
		}
	}

	for (int i = 2; i &lt;= N; i++) {
		if (disArr[i] == INF) cout &lt;&lt; "-1" &lt;&lt; "\n";
		else cout &lt;&lt; disArr[i] &lt;&lt; "\n";
	}

	return 0;
}</pre>



<div style="height:150px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">Dijkstra 알고리즘 vs Bellman–Ford 알고리즘 </h2>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading">1. 가중치의 차이</h3>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<figure class="wp-block-embed is-type-wp-embed is-provider-어제와-내일의-나-그-사이의-이야기 wp-block-embed-어제와-내일의-나-그-사이의-이야기"><div class="wp-block-embed__wrapper">
<blockquote class="wp-embedded-content" data-secret="OjdsQtMLPm"><a href="https://lycos7560.com/etc/%ec%95%8c%ea%b3%a0%eb%a6%ac%ec%a6%98-%eb%8b%a4%ec%9d%b5%ec%8a%a4%ed%8a%b8%eb%9d%bc-%eb%8d%b0%ec%9d%b4%ed%81%ac%ec%8a%a4%ed%8a%b8%eb%9d%bcdijkstra/5223/">알고리즘 &#8211; 다익스트라/데이크스트라(Dijkstra) 알고리즘 정리</a></blockquote><iframe loading="lazy" class="wp-embedded-content" sandbox="allow-scripts" security="restricted"  title="&#8220;알고리즘 &#8211; 다익스트라/데이크스트라(Dijkstra) 알고리즘 정리&#8221; &#8212; 어제와 내일의 나 그 사이의 이야기" src="https://lycos7560.com/etc/%ec%95%8c%ea%b3%a0%eb%a6%ac%ec%a6%98-%eb%8b%a4%ec%9d%b5%ec%8a%a4%ed%8a%b8%eb%9d%bc-%eb%8d%b0%ec%9d%b4%ed%81%ac%ec%8a%a4%ed%8a%b8%eb%9d%bcdijkstra/5223/embed/#?secret=64mzTRITI9#?secret=OjdsQtMLPm" data-secret="OjdsQtMLPm" width="600" height="338" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe>
</div></figure>



<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>



<p class="has-medium-font-size">예전에 정리한 Dijkstra 도 Bellman–Ford 와 같은 최단 경로 알고리즘입니다.</p>



<p class="has-medium-font-size">하지만 Dijkstra 알고리즘은 <strong>가중치가 양수일 때만 사용 가능하다는 중요한 특징</strong>을 가지고 있습니다.</p>



<p class="has-medium-font-size">따라서 가중치가 전부 양수라면 Dijkstra 알고리즘을 사용하고 </p>



<p class="has-medium-font-size">하나라도 음수의 가중치가 있다면 </p>



<p class="has-medium-font-size">Bellman–Ford 알고리즘을 사용해야 합니다.</p>



<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>



<p class="has-medium-font-size">위 : Dijkstra / 아래 Bellman–Ford (.gif)</p>



<figure class="wp-block-image size-full"><img decoding="async" width="560" height="600" src="https://lycos7560.com/wp-content/uploads/2023/02/560px-Shortest_path_Dijkstra_vs_BellmanFord.gif" alt="" class="wp-image-33052"/><figcaption class="wp-element-caption"><a href="https://commons.wikimedia.org/wiki/File:Shortest_path_Dijkstra_vs_BellmanFord.gif" target="_blank" rel="noreferrer noopener">https://commons.wikimedia.org/wiki/File:Shortest_path_Dijkstra_vs_BellmanFord.gif</a></figcaption></figure>



<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading">2. 시간 복잡도의 차이</h3>



<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>



<p class="has-medium-font-size">V는 꼭짓점의 수이고 E는 간선의 개수라고 한다면</p>



<p class="has-medium-font-size">Dijkstra 알고리즘은 O(E*logV)의 시간 복잡도를 가지고</p>



<p class="has-medium-font-size">Bellman-Ford 알고리즘은 O(V*E)의 시간 복잡도를 가집니다.</p>



<p class="has-medium-font-size">따라서 음의 가중치가 없다면 Bellman-Ford 알고리즘보다는 </p>



<p class="has-medium-font-size">Dijkstra 알고리즘을 이용하여 해결하는 것이 더 좋은 방법입니다.</p>



<p class="has-medium-font-size">(Dijkstra 알고리즘은 우선순위 대기열을 사용하여 처리해야 하는 정점을 유지하는 반면, </p>



<p class="has-medium-font-size">Bellman-Ford 알고리즘은 가장 짧은 경로를 찾기 위해 모든 정점에 대해 반복합니다.)</p>



<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>



			<div class="wp-block-uagb-post-carousel uagb-post-grid  uagb-post__image-position-top uagb-post__image-enabled uagb-block-37106eb0     uagb-post__arrow-outside uagb-post__items uagb-post__columns-5 is-carousel uagb-post__columns-tablet-2 uagb-post__columns-mobile-1" data-total="1" style="">

												<article class="uagb-post__inner-wrap">								<div class='uagb-post__image'>
									<a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-1865%eb%b2%88-%ec%9b%9c%ed%99%80-c-bellman-ford-%ec%b6%94%ea%b0%80-%eb%b0%98%eb%a1%80-baekjoon/33135/" target="_self" rel="bookmark noopener noreferrer"><img decoding="async" width="1920" height="1003" src="https://lycos7560.com/wp-content/uploads/boj-og.png" class="attachment-large size-large" alt="" srcset="https://lycos7560.com/wp-content/uploads/boj-og.png 1920w, https://lycos7560.com/wp-content/uploads/boj-og-300x157.png 300w, https://lycos7560.com/wp-content/uploads/boj-og-768x401.png 768w, https://lycos7560.com/wp-content/uploads/boj-og-1536x802.png 1536w, https://lycos7560.com/wp-content/uploads/boj-og-2048x1070.png 2048w" sizes="(max-width: 1920px) 100vw, 1920px" />					</a>
									</div>
						<h4 class="uagb-post__title uagb-post__text">
				<a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-1865%eb%b2%88-%ec%9b%9c%ed%99%80-c-bellman-ford-%ec%b6%94%ea%b0%80-%eb%b0%98%eb%a1%80-baekjoon/33135/" target="_self" rel="bookmark noopener noreferrer">백준 1865번 (웜홀, C++, Bellman–Ford) [BAEKJOON]</a>
			</h4>
						<div class='uagb-post__text uagb-post-grid-byline'>
							</div>
						<div class="uagb-post__text uagb-post__cta wp-block-button">
				<a class="wp-block-button__link uagb-text-link" style="" href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-1865%eb%b2%88-%ec%9b%9c%ed%99%80-c-bellman-ford-%ec%b6%94%ea%b0%80-%eb%b0%98%eb%a1%80-baekjoon/33135/" target="_self" rel="bookmark noopener noreferrer">더 읽기</a>
			</div>
			
									</article>
																<article class="uagb-post__inner-wrap">								<div class='uagb-post__image'>
									<a href="https://lycos7560.com/etc/%ec%95%8c%ea%b3%a0%eb%a6%ac%ec%a6%98-%eb%b2%a8%eb%a7%8c-%ed%8f%ac%eb%93%9c-bellman-ford-algorithm-%ec%95%8c%ea%b3%a0%eb%a6%ac%ec%a6%98-%ec%a0%95%eb%a6%ac/33044/" target="_self" rel="bookmark noopener noreferrer"><img decoding="async" width="1024" height="640" src="https://lycos7560.com/wp-content/uploads/algorithm-wallpaper.jpg" class="attachment-large size-large" alt="" srcset="https://lycos7560.com/wp-content/uploads/algorithm-wallpaper.jpg 1024w, https://lycos7560.com/wp-content/uploads/algorithm-wallpaper-300x188.jpg 300w, https://lycos7560.com/wp-content/uploads/algorithm-wallpaper-768x480.jpg 768w" sizes="(max-width: 1024px) 100vw, 1024px" />					</a>
									</div>
						<h4 class="uagb-post__title uagb-post__text">
				<a href="https://lycos7560.com/etc/%ec%95%8c%ea%b3%a0%eb%a6%ac%ec%a6%98-%eb%b2%a8%eb%a7%8c-%ed%8f%ac%eb%93%9c-bellman-ford-algorithm-%ec%95%8c%ea%b3%a0%eb%a6%ac%ec%a6%98-%ec%a0%95%eb%a6%ac/33044/" target="_self" rel="bookmark noopener noreferrer">알고리즘 &#8211; 벨만-포드 (Bellman–Ford Algorithm) 알고리즘 정리</a>
			</h4>
						<div class='uagb-post__text uagb-post-grid-byline'>
							</div>
						<div class="uagb-post__text uagb-post__cta wp-block-button">
				<a class="wp-block-button__link uagb-text-link" style="" href="https://lycos7560.com/etc/%ec%95%8c%ea%b3%a0%eb%a6%ac%ec%a6%98-%eb%b2%a8%eb%a7%8c-%ed%8f%ac%eb%93%9c-bellman-ford-algorithm-%ec%95%8c%ea%b3%a0%eb%a6%ac%ec%a6%98-%ec%a0%95%eb%a6%ac/33044/" target="_self" rel="bookmark noopener noreferrer">더 읽기</a>
			</div>
			
									</article>
																<article class="uagb-post__inner-wrap">								<div class='uagb-post__image'>
									<a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-11657%eb%b2%88-%ed%83%80%ec%9e%84%eb%a8%b8%ec%8b%a0-c-bellman-ford-baekjoon/33077/" target="_self" rel="bookmark noopener noreferrer"><img decoding="async" width="1920" height="1003" src="https://lycos7560.com/wp-content/uploads/boj-og.png" class="attachment-large size-large" alt="" srcset="https://lycos7560.com/wp-content/uploads/boj-og.png 1920w, https://lycos7560.com/wp-content/uploads/boj-og-300x157.png 300w, https://lycos7560.com/wp-content/uploads/boj-og-768x401.png 768w, https://lycos7560.com/wp-content/uploads/boj-og-1536x802.png 1536w, https://lycos7560.com/wp-content/uploads/boj-og-2048x1070.png 2048w" sizes="(max-width: 1920px) 100vw, 1920px" />					</a>
									</div>
						<h4 class="uagb-post__title uagb-post__text">
				<a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-11657%eb%b2%88-%ed%83%80%ec%9e%84%eb%a8%b8%ec%8b%a0-c-bellman-ford-baekjoon/33077/" target="_self" rel="bookmark noopener noreferrer">백준 11657번 (타임머신, C++, Bellman–Ford) [BAEKJOON]</a>
			</h4>
						<div class='uagb-post__text uagb-post-grid-byline'>
							</div>
						<div class="uagb-post__text uagb-post__cta wp-block-button">
				<a class="wp-block-button__link uagb-text-link" style="" href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-11657%eb%b2%88-%ed%83%80%ec%9e%84%eb%a8%b8%ec%8b%a0-c-bellman-ford-baekjoon/33077/" target="_self" rel="bookmark noopener noreferrer">더 읽기</a>
			</div>
			
									</article>
																<article class="uagb-post__inner-wrap">								<div class='uagb-post__image'>
									<a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-1219%eb%b2%88-%ec%98%a4%eb%af%bc%ec%8b%9d%ec%9d%98-%ea%b3%a0%eb%af%bc-c-bellman-ford-baekjoon/33149/" target="_self" rel="bookmark noopener noreferrer"><img decoding="async" width="1920" height="1003" src="https://lycos7560.com/wp-content/uploads/boj-og-1.png" class="attachment-large size-large" alt="" srcset="https://lycos7560.com/wp-content/uploads/boj-og-1.png 1920w, https://lycos7560.com/wp-content/uploads/boj-og-1-300x157.png 300w, https://lycos7560.com/wp-content/uploads/boj-og-1-768x401.png 768w, https://lycos7560.com/wp-content/uploads/boj-og-1-1536x802.png 1536w, https://lycos7560.com/wp-content/uploads/boj-og-1-2048x1070.png 2048w" sizes="(max-width: 1920px) 100vw, 1920px" />					</a>
									</div>
						<h4 class="uagb-post__title uagb-post__text">
				<a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-1219%eb%b2%88-%ec%98%a4%eb%af%bc%ec%8b%9d%ec%9d%98-%ea%b3%a0%eb%af%bc-c-bellman-ford-baekjoon/33149/" target="_self" rel="bookmark noopener noreferrer">백준 1219번 (오민식의 고민, C++, Bellman–Ford) [BAEKJOON]</a>
			</h4>
						<div class='uagb-post__text uagb-post-grid-byline'>
							</div>
						<div class="uagb-post__text uagb-post__cta wp-block-button">
				<a class="wp-block-button__link uagb-text-link" style="" href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-1219%eb%b2%88-%ec%98%a4%eb%af%bc%ec%8b%9d%ec%9d%98-%ea%b3%a0%eb%af%bc-c-bellman-ford-baekjoon/33149/" target="_self" rel="bookmark noopener noreferrer">더 읽기</a>
			</div>
			
									</article>
																<article class="uagb-post__inner-wrap">								<div class='uagb-post__image'>
									<a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-1738%eb%b2%88-%ea%b3%a8%eb%aa%a9%ea%b8%b8-c-bellman-ford-%ec%b6%94%ea%b0%80-%eb%b0%98%eb%a1%80-baekjoon/33119/" target="_self" rel="bookmark noopener noreferrer"><img decoding="async" width="1024" height="535" src="https://lycos7560.com/wp-content/uploads/2022/10/boj-og-1-2048x1070-1-1024x535.png" class="attachment-large size-large" alt="" srcset="https://lycos7560.com/wp-content/uploads/2022/10/boj-og-1-2048x1070-1-1024x535.png 1024w, https://lycos7560.com/wp-content/uploads/2022/10/boj-og-1-2048x1070-1-300x157.png 300w, https://lycos7560.com/wp-content/uploads/2022/10/boj-og-1-2048x1070-1-768x401.png 768w, https://lycos7560.com/wp-content/uploads/2022/10/boj-og-1-2048x1070-1-1536x803.png 1536w, https://lycos7560.com/wp-content/uploads/2022/10/boj-og-1-2048x1070-1.png 2048w" sizes="(max-width: 1024px) 100vw, 1024px" />					</a>
									</div>
						<h4 class="uagb-post__title uagb-post__text">
				<a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-1738%eb%b2%88-%ea%b3%a8%eb%aa%a9%ea%b8%b8-c-bellman-ford-%ec%b6%94%ea%b0%80-%eb%b0%98%eb%a1%80-baekjoon/33119/" target="_self" rel="bookmark noopener noreferrer">백준 1738번 (골목길, C++, Bellman–Ford) / 추가 반례 [BAEKJOON]</a>
			</h4>
						<div class='uagb-post__text uagb-post-grid-byline'>
							</div>
						<div class="uagb-post__text uagb-post__cta wp-block-button">
				<a class="wp-block-button__link uagb-text-link" style="" href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-1738%eb%b2%88-%ea%b3%a8%eb%aa%a9%ea%b8%b8-c-bellman-ford-%ec%b6%94%ea%b0%80-%eb%b0%98%eb%a1%80-baekjoon/33119/" target="_self" rel="bookmark noopener noreferrer">더 읽기</a>
			</div>
			
									</article>
											</div>
			


<p class="has-medium-font-size"><a href="https://www.acmicpc.net/problemset?sort=ac_desc&amp;algo=10" target="_blank" rel="noreferrer noopener">https://www.acmicpc.net/problemset?sort=ac_desc&amp;algo=10</a> &lt;- 백준 Bellman-Ford 알고리즘 문제</p>



<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>



<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-5698326622209671" crossorigin="anonymous"></script>
<ins class="adsbygoogle" style="display:block" data-ad-format="autorelaxed" data-ad-client="ca-pub-5698326622209671" data-ad-slot="4245812909"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>
<p>The post <a href="https://lycos7560.com/etc/%ec%95%8c%ea%b3%a0%eb%a6%ac%ec%a6%98-%eb%b2%a8%eb%a7%8c-%ed%8f%ac%eb%93%9c-bellman-ford-algorithm-%ec%95%8c%ea%b3%a0%eb%a6%ac%ec%a6%98-%ec%a0%95%eb%a6%ac/33044/">알고리즘 &#8211; 벨만-포드 (Bellman–Ford Algorithm) 알고리즘 정리</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://lycos7560.com/etc/%ec%95%8c%ea%b3%a0%eb%a6%ac%ec%a6%98-%eb%b2%a8%eb%a7%8c-%ed%8f%ac%eb%93%9c-bellman-ford-algorithm-%ec%95%8c%ea%b3%a0%eb%a6%ac%ec%a6%98-%ec%a0%95%eb%a6%ac/33044/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>백준 5719번 (거의 최단 경로, C++, Dijkstra) / 추가 반례 [BAEKJOON]</title>
		<link>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-5719%eb%b2%88-%ea%b1%b0%ec%9d%98-%ec%b5%9c%eb%8b%a8-%ea%b2%bd%eb%a1%9c-c-dijkstra-%ec%b6%94%ea%b0%80-%eb%b0%98%eb%a1%80-baekjoon/6006/</link>
					<comments>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-5719%eb%b2%88-%ea%b1%b0%ec%9d%98-%ec%b5%9c%eb%8b%a8-%ea%b2%bd%eb%a1%9c-c-dijkstra-%ec%b6%94%ea%b0%80-%eb%b0%98%eb%a1%80-baekjoon/6006/#respond</comments>
		
		<dc:creator><![CDATA[lycos7560]]></dc:creator>
		<pubDate>Sun, 05 Feb 2023 13:45:54 +0000</pubDate>
				<category><![CDATA[BaekjoonOnlineJudge]]></category>
		<category><![CDATA[C++/CPP]]></category>
		<category><![CDATA[5719]]></category>
		<category><![CDATA[5719번]]></category>
		<category><![CDATA[algorithm]]></category>
		<category><![CDATA[Baekjoon]]></category>
		<category><![CDATA[BFS]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[cin.tie(NULL)]]></category>
		<category><![CDATA[cout.tie(NULL)]]></category>
		<category><![CDATA[cpp]]></category>
		<category><![CDATA[Dijkstra]]></category>
		<category><![CDATA[DP]]></category>
		<category><![CDATA[DynamicProgramming]]></category>
		<category><![CDATA[priority_queue]]></category>
		<category><![CDATA[PriorityQueue]]></category>
		<category><![CDATA[Queue]]></category>
		<category><![CDATA[study]]></category>
		<category><![CDATA[vector]]></category>
		<category><![CDATA[거의 최단 경로]]></category>
		<category><![CDATA[공부]]></category>
		<category><![CDATA[그래프 이론]]></category>
		<category><![CDATA[그래프 탐색]]></category>
		<category><![CDATA[기본]]></category>
		<category><![CDATA[기초]]></category>
		<category><![CDATA[길찾기]]></category>
		<category><![CDATA[너비]]></category>
		<category><![CDATA[너비 우선 탐색]]></category>
		<category><![CDATA[다이나믹 프로그래밍]]></category>
		<category><![CDATA[다익스트라]]></category>
		<category><![CDATA[데이크스트라]]></category>
		<category><![CDATA[반례]]></category>
		<category><![CDATA[백준]]></category>
		<category><![CDATA[백준 5719]]></category>
		<category><![CDATA[백준 5719번]]></category>
		<category><![CDATA[시간]]></category>
		<category><![CDATA[알고리즘]]></category>
		<category><![CDATA[예제]]></category>
		<category><![CDATA[추가 반례]]></category>
		<category><![CDATA[추가 예제]]></category>
		<category><![CDATA[추가반례]]></category>
		<category><![CDATA[추가예제]]></category>
		<category><![CDATA[컴파일 에러]]></category>
		<category><![CDATA[코딩테스트]]></category>
		<category><![CDATA[코테]]></category>
		<guid isPermaLink="false">https://lycos7560.com/?p=6006</guid>

					<description><![CDATA[<p>백준(BAEKJOON) 5719번 "거의 최단 경로" 문제에 대한 글입니다. Dijkstra 알고리즘을 이용하여 해결하였습니다. (This article is about the "near shortest path" problem in BAEKJOON No. 5719. It was solved using Dijkstra algorithm.)</p>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-5719%eb%b2%88-%ea%b1%b0%ec%9d%98-%ec%b5%9c%eb%8b%a8-%ea%b2%bd%eb%a1%9c-c-dijkstra-%ec%b6%94%ea%b0%80-%eb%b0%98%eb%a1%80-baekjoon/6006/">백준 5719번 (거의 최단 경로, C++, Dijkstra) / 추가 반례 [BAEKJOON]</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-5698326622209671" crossorigin="anonymous"></script>
<!-- HorizontalAD -->
<ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-5698326622209671" data-ad-slot="6908948342" data-ad-format="auto" data-full-width-responsive="true"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>



<div style="height:62px" aria-hidden="true" class="wp-block-spacer"></div>


				<div class="wp-block-uagb-table-of-contents uagb-toc__align-left uagb-toc__columns-1  uagb-block-539bed31      "
					data-scroll= "1"
					data-offset= "30"
					style=""
				>
				<div class="uagb-toc__wrap">
						<div class="uagb-toc__title">
							목차 테이블						</div>
																						<div class="uagb-toc__list-wrap ">
						<ol class="uagb-toc__list"><li class="uagb-toc__list"><a href="#거의-최단-경로" class="uagb-toc-link__trigger">거의 최단 경로</a><li class="uagb-toc__list"><a href="#추가-예제" class="uagb-toc-link__trigger">추가 예제</a><li class="uagb-toc__list"><a href="#틀린-코드-메모리-초과" class="uagb-toc-link__trigger">틀린 코드 (메모리 초과)</a><li class="uagb-toc__list"><a href="#통과된-코드" class="uagb-toc-link__trigger">통과된 코드</a></ol>					</div>
									</div>
				</div>
			


<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h1 class="wp-block-heading">거의 최단 경로</h1>



<p class="has-medium-font-size"><a href="https://www.acmicpc.net/problem/5719" target="_blank" rel="noreferrer noopener">https://www.acmicpc.net/problem/5719</a></p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<figure id="problem-info" class="wp-block-table"><table class="has-fixed-layout"><thead><tr><th class="has-text-align-left" data-align="left">시간 제한</th><th class="has-text-align-left" data-align="left">메모리 제한</th><th class="has-text-align-left" data-align="left">제출</th><th class="has-text-align-left" data-align="left">정답</th><th class="has-text-align-left" data-align="left">맞힌 사람</th><th class="has-text-align-left" data-align="left">정답 비율</th></tr></thead><tbody><tr><td class="has-text-align-left" data-align="left">1 초</td><td class="has-text-align-left" data-align="left">256 MB</td><td class="has-text-align-left" data-align="left">23040</td><td class="has-text-align-left" data-align="left">3945</td><td class="has-text-align-left" data-align="left">2400</td><td class="has-text-align-left" data-align="left">19.151%</td></tr></tbody></table></figure>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">문제</h2>



<p class="has-medium-font-size">요즘 많은 자동차에서는 GPS 네비게이션 장비가 설치되어 있다. </p>



<p class="has-medium-font-size">네비게이션은 사용자가 입력한 출발점과 도착점 사이의 최단 경로를 검색해 준다. </p>



<p class="has-medium-font-size">하지만, 교통 상황을 고려하지 않고 최단 경로를 검색하는 경우에는 극심한 교통 정체를 경험할 수 있다.</p>



<p class="has-medium-font-size">상근이는 오직 자기 자신만 사용 가능한 네비게이션을 만들고 있다. </p>



<p class="has-medium-font-size">이 네비게이션은 절대로 최단 경로를 찾아주지 않는다. 항상 거의 최단 경로를 찾아준다.</p>



<p class="has-medium-font-size">거의 최단 경로란 최단 경로에 포함되지 않는 도로로만 이루어진 경로 중 가장 짧은 것을 말한다.&nbsp;</p>



<p class="has-medium-font-size">예를 들어, 도로 지도가 아래와 같을 때를 생각해보자. </p>



<p class="has-medium-font-size">원은 장소를 의미하고, 선은 단방향 도로를 나타낸다. </p>



<p class="has-medium-font-size">시작점은 S, 도착점은 D로 표시되어 있다. 굵은 선은 최단 경로를 나타낸다. (아래 그림에 최단 경로는 두 개가 있다)</p>



<p class="has-medium-font-size">거의 최단 경로는 점선으로 표시된 경로이다. </p>



<p class="has-medium-font-size">이 경로는 최단 경로에 포함되지 않은 도로로 이루어진 경로 중 가장 짧은 경로이다. </p>



<p class="has-medium-font-size">거의 최단 경로는 여러 개 존재할 수도 있다. </p>



<p class="has-medium-font-size">예를 들어, 아래 그림의 길이가 3인 도로의 길이가 1이라면, 거의 최단 경로는 두 개가 된다. </p>



<p class="has-medium-font-size">또, 거의 최단 경로가 없는 경우도 있다.</p>



<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>



<figure class="wp-block-image size-full"><img decoding="async" width="499" height="335" src="https://lycos7560.com/wp-content/uploads/2023/02/image-34.png" alt="" class="wp-image-6008" srcset="https://lycos7560.com/wp-content/uploads/2023/02/image-34.png 499w, https://lycos7560.com/wp-content/uploads/2023/02/image-34-300x201.png 300w" sizes="(max-width: 499px) 100vw, 499px" /></figure>



<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">입력</h2>



<p class="has-medium-font-size">입력은 여러 개의 테스트 케이스로 이루어져 있다. </p>



<p class="has-medium-font-size">각 테스트 케이스의 첫째 줄에는 장소의 수 N (2 ≤ N ≤ 500)과 도로의 수 M (1 ≤ M ≤ 10<sup>4</sup>)가 주어진다. </p>



<p class="has-medium-font-size">장소는 0부터 N-1번까지 번호가 매겨져 있다. 둘째 줄에는 시작점 S와 도착점 D가 주어진다. (S ≠ D; 0 ≤ S, D &lt; N) </p>



<p class="has-medium-font-size">다음 M개 줄에는 도로의 정보 U, V, P가 주어진다. (U ≠ V ; 0 ≤ U, V &lt; N; 1 ≤ P ≤ 10<sup>3</sup>) </p>



<p class="has-medium-font-size">이 뜻은 U에서 V로 가는 도로의 길이가 P라는 뜻이다. </p>



<p class="has-medium-font-size">U에서 V로 가는 도로는 최대 한 개이다. </p>



<p class="has-medium-font-size">또, U에서 V로 가는 도로와 V에서 U로 가는 도로는 다른 도로이다.&nbsp;</p>



<p class="has-medium-font-size">입력의 마지막 줄에는 0이 두 개 주어진다.</p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">출력</h2>



<p class="has-medium-font-size">각 테스트 케이스에 대해서, 거의 최단 경로의 길이를 출력한다. </p>



<p class="has-medium-font-size">만약, 거의 최단 경로가 없는 경우에는 -1을 출력한다.</p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 출력 1</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">7 9
0 6
0 1 1
0 2 1
0 3 2
0 4 3
1 5 2
2 6 4
3 6 2
4 6 4
5 6 1
4 6
0 2
0 1 1
1 2 1
1 3 1
3 2 1
2 0 3
3 0 2
6 8
0 1
0 1 1
0 2 2
0 3 3
2 5 3
3 4 2
4 1 1
5 1 1
3 0 1
0 0</pre>



<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 입력 1</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">5
-1
6</pre>



<div style="height:50px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">출처</h2>



<p><a href="https://www.acmicpc.net/category/1" target="_blank" rel="noreferrer noopener">ICPC</a>&nbsp;&gt;&nbsp;<a href="https://www.acmicpc.net/category/7" target="_blank" rel="noreferrer noopener">Regionals</a>&nbsp;&gt;&nbsp;<a href="https://www.acmicpc.net/category/103" target="_blank" rel="noreferrer noopener">Latin America</a>&nbsp;&gt;&nbsp;<a href="https://www.acmicpc.net/category/detail/568" target="_blank" rel="noreferrer noopener">South America Regional Contests 2008</a>&nbsp;A번</p>



<ul class="wp-block-list">
<li>문제를 번역한 사람:&nbsp;<a href="https://www.acmicpc.net/user/baekjoon" target="_blank" rel="noreferrer noopener">baekjoon</a></li>



<li>데이터를 추가한 사람:&nbsp;<a href="https://www.acmicpc.net/user/gkswjdzz" target="_blank" rel="noreferrer noopener">gkswjdzz</a>,&nbsp;<a href="https://www.acmicpc.net/user/sait2000" target="_blank" rel="noreferrer noopener">sait2000</a>,&nbsp;<a href="https://www.acmicpc.net/user/smsm8111" target="_blank" rel="noreferrer noopener">smsm8111</a></li>



<li>잘못된 데이터를 찾은 사람:&nbsp;<a href="https://www.acmicpc.net/user/tncks0121" target="_blank" rel="noreferrer noopener">tncks0121</a></li>
</ul>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">알고리즘 분류</h2>



<ul class="wp-block-list">
<li><a href="https://www.acmicpc.net/problem/tag/7" target="_blank" rel="noreferrer noopener">그래프 이론</a></li>



<li><a href="https://www.acmicpc.net/problem/tag/11" target="_blank" rel="noreferrer noopener">그래프 탐색</a></li>



<li><a href="https://www.acmicpc.net/problem/tag/22" target="_blank" rel="noreferrer noopener">데이크스트라</a></li>
</ul>



<hr class="wp-block-separator has-alpha-channel-opacity is-style-wide" style="margin-top:var(--wp--preset--spacing--80);margin-bottom:var(--wp--preset--spacing--80)"/>



<h1 class="wp-block-heading">추가 예제</h1>



<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 출력 A</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">100 765
44 52
0 11 939
0 18 635
0 26 757
0 39 558
0 41 669
0 43 855
0 45 951
0 46 755
0 51 785
0 52 218
0 78 557
0 94 918
0 96 300
1 19 752
1 22 157
1 31 797
1 37 871
1 78 834
2 0 275
2 30 73
2 64 32
2 67 552
2 78 7
2 81 883
2 96 315
2 99 865
3 11 299
3 13 196
3 37 247
3 45 523
3 47 745
3 54 474
3 60 63
3 94 244
3 97 699
3 99 855
4 10 516
4 17 506
4 24 985
4 28 598
4 35 598
4 39 744
4 44 749
4 46 8
4 77 363
4 78 618
4 92 988
4 97 591
5 0 918
5 9 8
5 14 405
5 23 448
5 84 354
6 0 404
6 10 209
6 20 352
6 57 651
6 71 490
6 77 934
6 94 188
6 96 638
6 99 138
7 25 815
7 27 55
7 42 606
7 56 643
7 65 966
7 81 755
8 0 684
8 5 959
8 13 998
8 19 757
8 22 172
8 33 638
8 38 777
8 51 185
8 59 321
8 67 403
8 69 475
8 71 204
8 74 567
8 97 138
9 15 965
9 17 611
9 64 712
9 66 743
9 84 971
10 5 486
11 3 678
11 54 838
11 86 226
11 95 623
12 5 427
12 28 19
12 33 879
12 35 876
12 37 907
12 50 437
12 51 853
12 52 407
12 58 756
12 68 480
12 75 867
12 85 833
13 6 356
13 37 954
13 53 479
13 68 448
13 74 707
13 79 369
13 80 735
13 84 17
13 96 355
14 6 185
14 12 121
14 17 925
14 32 276
14 36 559
14 39 343
14 44 90
14 86 513
14 87 952
14 96 912
15 13 296
15 20 726
15 36 835
15 41 458
15 49 17
15 57 47
15 61 583
15 65 25
15 82 703
16 9 289
16 19 289
16 26 935
16 46 122
16 58 611
16 59 625
16 60 89
16 75 674
16 81 147
16 85 806
16 86 89
17 7 848
17 39 490
17 53 980
17 58 372
17 62 357
17 78 885
17 83 406
17 87 873
17 95 32
18 41 232
18 43 394
18 47 35
18 50 697
18 83 464
18 87 281
19 1 857
19 12 589
19 14 819
19 37 470
19 40 520
19 42 276
19 43 27
19 66 706
19 71 426
19 83 858
19 94 353
20 13 523
20 17 128
20 51 90
20 53 411
20 84 903
20 91 747
20 96 503
21 23 721
21 27 724
21 28 34
21 30 396
21 34 86
21 37 371
21 53 371
21 54 672
22 4 325
22 11 407
22 44 631
22 55 480
22 68 861
22 90 338
23 1 109
23 15 850
23 34 645
23 48 582
23 52 264
23 58 330
23 64 796
23 65 118
23 69 133
23 89 389
24 11 220
24 26 5
24 38 951
24 44 990
24 45 123
24 49 216
24 51 505
24 90 388
24 93 475
25 24 633
25 41 489
25 83 434
26 4 577
26 17 977
26 19 558
26 31 863
26 33 284
26 43 751
26 46 576
26 48 224
26 79 497
27 14 26
27 18 949
27 21 102
27 38 357
27 45 528
27 75 614
27 76 289
27 80 803
27 81 343
28 3 458
28 15 893
28 24 579
28 44 649
28 62 934
28 65 964
28 67 177
28 87 678
29 0 191
29 39 112
29 45 184
29 61 778
29 87 810
30 4 335
30 11 829
30 50 996
30 54 470
30 60 972
30 63 147
30 98 559
30 99 24
31 15 30
31 29 250
31 46 53
31 63 488
31 71 561
32 35 812
32 50 781
32 67 540
33 34 611
33 56 12
33 61 631
33 64 842
33 89 697
34 4 208
34 19 621
34 23 343
34 35 129
34 40 489
34 42 323
34 56 762
34 64 522
34 79 412
35 1 64
35 3 799
35 5 360
35 6 488
35 11 879
35 27 403
35 28 320
35 43 802
35 71 639
35 75 147
35 94 310
36 3 471
36 18 920
36 37 260
36 51 987
36 68 888
36 77 573
37 5 79
37 24 306
37 51 731
37 54 451
38 34 359
38 35 854
38 48 903
38 71 322
38 99 262
39 5 40
39 6 830
39 8 58
39 22 757
39 23 539
39 36 984
39 58 75
39 60 780
39 63 223
39 65 365
39 66 86
39 67 953
39 78 705
39 79 505
39 87 29
39 94 31
39 97 865
40 11 60
40 49 145
40 57 211
40 60 371
40 81 144
40 82 874
40 97 994
41 37 142
41 99 976
42 4 376
42 8 56
42 12 372
42 28 634
42 30 71
42 35 766
42 43 653
42 45 283
42 47 674
42 52 807
42 57 189
42 84 279
42 86 366
42 91 728
42 93 702
43 6 149
43 14 39
43 40 796
43 87 35
43 89 946
43 94 259
43 95 375
44 8 35
44 21 605
44 25 809
44 73 418
44 85 914
44 93 368
45 34 90
45 41 172
45 48 253
45 53 829
45 65 31
45 86 994
45 92 826
46 18 346
46 37 161
46 44 505
46 76 992
46 80 576
46 94 984
47 8 295
47 35 330
47 56 954
47 76 158
47 77 39
47 80 313
47 82 217
47 87 688
48 17 214
48 31 760
48 36 989
48 37 922
48 84 886
49 12 757
49 14 724
49 16 948
49 34 854
49 38 552
49 40 361
49 50 990
49 53 148
49 74 959
49 82 226
49 84 153
49 87 134
50 14 838
50 33 260
50 37 624
50 44 381
50 47 868
50 55 616
50 90 135
50 92 148
50 97 510
51 56 459
51 84 726
51 91 543
51 99 855
52 2 872
52 23 771
52 35 380
52 76 773
52 81 769
53 10 314
53 48 745
53 49 567
53 50 99
53 58 180
53 67 297
53 80 107
54 17 458
54 21 310
54 44 68
54 70 896
54 83 986
54 87 605
54 90 422
55 0 101
55 7 868
55 31 316
55 36 208
55 46 151
55 87 612
56 0 40
56 16 247
56 35 492
56 59 609
56 73 796
56 78 225
57 2 419
57 17 785
57 23 523
57 37 730
57 42 290
57 47 503
57 59 909
57 81 319
57 84 782
57 95 250
58 2 842
58 26 886
58 52 168
58 56 386
58 59 253
58 70 174
58 79 608
58 81 764
58 89 215
59 8 105
59 9 876
59 18 305
59 20 397
59 31 278
59 35 379
59 57 702
60 18 785
60 59 603
60 62 224
60 63 300
60 69 111
60 70 437
60 86 129
60 88 862
60 90 603
61 13 185
61 14 521
61 26 23
61 33 645
61 45 907
61 67 269
61 75 717
61 82 919
61 90 554
61 91 865
61 98 182
62 13 135
62 24 96
62 39 625
62 63 901
62 81 816
62 97 592
63 7 518
63 8 46
63 16 724
63 52 912
63 97 725
64 15 554
64 29 395
64 35 177
64 38 340
64 58 100
64 66 570
64 76 754
65 24 370
65 29 726
65 33 348
65 41 388
65 44 931
65 59 293
65 92 255
65 95 937
66 7 435
66 17 260
66 19 510
66 23 571
66 25 875
66 39 671
66 77 97
66 80 52
66 82 939
67 2 110
67 40 346
67 41 641
67 46 881
67 52 703
67 58 770
67 62 974
67 81 199
67 90 396
67 95 558
67 96 375
67 99 597
68 2 395
68 17 863
68 20 8
68 22 682
68 43 444
68 74 870
68 79 739
68 98 158
69 10 763
69 13 933
69 25 738
69 28 755
69 33 745
69 53 998
69 58 961
69 78 410
69 89 621
69 90 271
69 91 362
69 98 574
70 17 788
70 38 695
70 44 383
70 55 843
70 60 373
70 61 529
71 7 932
71 9 184
71 25 341
71 27 360
71 53 832
71 60 135
71 64 245
71 69 980
71 72 238
71 74 210
72 14 63
72 42 346
72 60 204
72 71 324
72 73 970
72 88 547
72 90 618
73 9 20
73 17 980
73 18 845
73 27 728
73 37 577
73 46 472
73 56 825
73 58 111
73 61 962
73 75 69
73 86 389
74 10 720
74 17 31
74 67 163
74 69 621
74 79 831
74 84 479
74 88 579
74 91 43
75 18 763
75 24 669
75 33 618
75 61 689
75 78 430
75 83 422
76 1 768
76 31 972
76 69 445
76 73 111
76 75 841
76 78 529
76 86 614
76 91 841
77 5 158
77 14 396
77 20 358
77 37 436
77 51 954
77 52 316
77 55 782
77 88 65
77 99 451
78 9 753
78 38 838
78 91 373
79 23 788
79 30 901
79 48 428
79 84 193
79 85 396
79 88 456
80 6 442
80 10 869
80 29 590
80 32 894
80 45 429
80 67 2
80 74 800
80 90 257
81 12 6
81 27 306
81 35 908
81 68 610
81 69 629
81 78 320
81 88 385
81 90 337
81 96 483
82 30 690
82 53 871
82 61 713
82 97 228
82 99 199
83 34 654
83 39 786
83 46 529
83 53 429
83 76 995
83 82 802
83 84 571
84 0 362
84 6 696
84 19 298
84 30 741
84 34 286
84 54 31
84 58 194
84 61 383
84 81 487
84 88 656
84 95 750
84 98 916
84 99 819
85 6 716
85 38 723
85 43 889
85 44 775
85 57 115
85 68 548
85 77 480
86 23 988
86 41 963
86 49 512
86 62 210
86 89 363
86 92 738
86 96 942
87 7 666
87 22 689
87 26 53
87 50 675
87 54 297
87 82 997
87 83 883
88 23 942
88 43 622
88 52 24
88 61 777
88 63 58
88 79 708
88 92 637
89 19 853
89 29 912
89 42 95
90 16 517
90 18 18
90 19 115
90 40 979
90 56 780
90 84 13
90 86 926
91 15 816
91 17 998
91 32 554
91 39 131
91 44 928
91 59 707
91 70 288
91 75 906
92 16 936
92 35 45
92 46 541
92 51 871
92 55 982
92 63 362
92 70 453
92 72 376
92 75 370
92 79 973
92 96 986
93 3 705
93 15 801
93 23 894
93 49 26
93 50 974
93 56 925
93 94 558
94 0 893
94 33 130
94 34 520
94 35 382
94 47 352
94 50 1
94 56 922
94 57 844
94 62 854
94 79 87
95 33 836
95 36 728
95 46 558
95 57 134
95 73 232
95 75 191
96 17 846
96 72 506
97 11 623
97 46 668
97 60 739
97 63 937
97 86 2
97 95 87
98 22 60
98 25 409
98 53 509
98 61 96
98 83 660
98 86 889
99 45 624
99 47 589
99 51 300
99 58 248
99 77 867
99 78 769
99 80 566
0 0</pre>



<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 출력 A</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">726</pre>



<div style="height:50px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 입력 B</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">4 5
0 2
0 1 1
0 3 5
1 2 1
1 3 1
3 2 1
4 5
0 2
0 1 1
0 3 5
1 2 2
1 3 1
3 2 1
4 5
0 2
0 1 5
0 3 1
1 2 1
3 1 1
3 2 2
5 6
0 1
0 1 1
0 2 1
0 4 1
2 3 1
3 1 1
4 1 2
0 0</pre>



<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 출력 B</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">6
-1
-1
3</pre>



<div style="height:50px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 입력 C</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">5 6
0 1
0 1 1
0 2 1
0 4 1
2 3 1
3 1 1
4 1 1
5 6
0 1
0 1 3
0 2 1
0 4 1
2 3 1
3 1 1
4 1 2
4 6
0 3
0 1 1
0 2 1
1 0 1
1 2 1
2 0 1
2 1 1
3 6
0 2
0 1 1
0 2 1
1 0 1
1 2 1
2 0 1
2 1 1
0 0</pre>



<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 출력 C</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">2
-1
-1
2</pre>



<div style="height:50px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 입력 D</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">3 2
0 2
0 1 1
1 2 1
5 6
0 1
0 1 1
0 2 1
2 3 1
2 4 1
3 1 1
4 1 1
4 6
0 3
0 1 1
0 2 1
0 3 1
1 2 1
1 3 1
2 3 1
4 6
0 3
0 1 1
0 2 2
0 3 3
1 2 1
1 3 2
2 3 1
0 0</pre>



<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 출력 D</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">-1
3
2
-1</pre>



<div style="height:50px" aria-hidden="true" class="wp-block-spacer"></div>



<hr class="wp-block-separator has-alpha-channel-opacity is-style-wide" style="margin-top:var(--wp--preset--spacing--80);margin-bottom:var(--wp--preset--spacing--80)"/>



<h1 class="wp-block-heading">틀린 코드 (메모리 초과)</h1>



<p class="has-medium-font-size">다익스트라의 경로를 저장하기 위하여 우선 순위 큐에 지나온 정점들을  list&lt;pair&lt;int, int&gt;&gt;에 담아 보았다. </p>



<p class="has-medium-font-size">당연하게 메모리 초과 (정답은 잘나온다&#8230; ㅜㅜ)</p>



<p class="has-medium-font-size">원리는 list에 있는 정점들을 방문처리하여 다음 다익스트라를 사용할 때 넘기는 방법을 사용했다.</p>



<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>



<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">#include &lt;iostream>
#include &lt;queue>
#include &lt;vector>
#include &lt;list>
#include &lt;tuple>
#include &lt;map>

using namespace std;

constexpr int INF = INT32_MAX;

int disArr[501];

bool check[501][501];

int N, M, S, D, U, V, P;

int Dijstra()
{
    int result = INF;

    for (int i = 0; i &lt;= N; i++) {
        for (int j = 0; j &lt;= N; j++) {
            check[i][j] = false;
        }
    }

    // 임시배열 초기화
    for (int i = 0; i &lt;= N; i++) disArr[i] = INF;

    priority_queue&lt;tuple&lt;int, int, list&lt;pair&lt;int, int>>>> firstPQ;

    vector&lt;pair&lt;int, int>> graph[10000];

    list&lt;pair&lt;int, int>> myList;

    multimap&lt;int, list&lt;pair&lt;int, int>>> myMap;

    for (int i = 0; i &lt; M; i++) {
        cin >> U >> V >> P;
        graph[U].push_back(make_pair(V, P));
    }

    // 우선순위 큐에 삽입.
    firstPQ.push({ 0, S, myList }); // &lt; first : 거리 , second : 노드 인덱스 >
    disArr[S] = 0;
    while (!firstPQ.empty()) {
        // -를 붙이는 이유는 우선순위 큐를 이용하여 정렬하기 위함이다.
        // (최소힙으로 구현)
        int nCost = -get&lt;0>(firstPQ.top());
        int now = get&lt;1>(firstPQ.top());
        myList = get&lt;2>(firstPQ.top());
        firstPQ.pop();

        // 이미 담겨 있는 것보다 거리가 크면 넘어간다.
        if (disArr[now] &lt; nCost) continue;

        // 해당 노드에서 연결된 모든 경로를 확인
        for (int i = 0; i &lt; graph[now].size(); i++) {
            list&lt;pair&lt;int, int>> tempList = myList;
            // disSum = 임시 노드 + 현재 노드에서 i로가는 비용
            int disSum = nCost + graph[now][i].second;

            // 비용이 더 작다면 최단경로 테이블 값을 갱신.
            if (disSum &lt;= disArr[graph[now][i].first]) {
                tempList.push_back(make_pair(now, graph[now][i].first));

                if (graph[now][i].first == D &amp;&amp; result >= disSum) {
                    result = disSum;
                    myMap.insert(make_pair(disSum, tempList));
                }

                // 임시 노드 업데이트
                disArr[graph[now][i].first] = disSum;
                // 우선순위 큐에 (거리, 노드 인덱스) 푸시
                firstPQ.push(make_tuple(-disSum, graph[now][i].first, tempList));
            }
        }
    }

    result = disArr[D];

    for (auto mit = myMap.begin(); mit != myMap.end(); mit++) {
        if (result == mit->first) {
            for (auto it = mit->second.begin(); it != mit->second.end(); it++) {
                check[it->first][it->second] = true;
            }
        }

        else break;
    }

    priority_queue&lt;pair&lt;int, int>> myPQ;

    // 임시배열 초기화
    for (int i = 0; i &lt;= N; i++) disArr[i] = INF;

    // 우선순위 큐에 삽입.
    myPQ.push({ 0, S }); // &lt; first : 거리 , second : 노드 인덱스 >
    disArr[S] = 0;

    while (!myPQ.empty()) {
        // -를 붙이는 이유는 우선순위 큐를 이용하여 정렬하기 위함이다.
        // (최소힙으로 구현)
        int nCost = -myPQ.top().first;
        int now = myPQ.top().second;
        myPQ.pop();

        // 이미 담겨 있는 것보다 거리가 크면 넘어간다.
        if (disArr[now] &lt; nCost) continue;
        // 해당 노드에서 연결된 모든 경로를 확인
        for (int i = 0; i &lt; graph[now].size(); i++) {
            if (check[now][graph[now][i].first] == true) continue;
            // disSum = 임시 노드 + 현재 노드에서 i로가는 비용
            int disSum = nCost + graph[now][i].second;
            // 비용이 더 작다면 최단경로 테이블 값을 갱신.
            if (disSum &lt;= disArr[graph[now][i].first]) {
                // 임시 노드 업데이트
                disArr[graph[now][i].first] = disSum;
                // 우선순위 큐에 (거리, 노드 인덱스) 푸시
                myPQ.push(make_pair(-disSum, graph[now][i].first));
            }
        }
    }

    return disArr[D];
}


int main()
{
    ios_base::sync_with_stdio(false); // scanf와 동기화를 비활성화
    //cin.tie(null); 코드는 cin과 cout의 묶음을 풀어줍니다.
    cin.tie(NULL);
    std::cout.tie(NULL);

    while (true) {

        cin >> N >> M;

        if (N == 0 &amp;&amp; M == 0) break;

        cin >> S >> D;

        int result = Dijstra();

        if (result == INF) cout &lt;&lt; -1 &lt;&lt; "\n";
        else cout &lt;&lt; result &lt;&lt; "\n";
    }



    return 0;
}</pre>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<hr class="wp-block-separator has-alpha-channel-opacity is-style-wide" style="margin-top:var(--wp--preset--spacing--80);margin-bottom:var(--wp--preset--spacing--80)"/>



<h1 class="wp-block-heading">통과된 코드</h1>



<p class="has-medium-font-size">vector&lt;int&gt; trace[501]; 를 이용하여 시작과 끝의 정점들을 넣어준다.</p>



<p class="has-medium-font-size">주의할 점은 도착지에서 출발지 방향으로 탐색을 하기 위해서 반대로 넣어주어야 한다.</p>



<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>



<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">#include &lt;iostream>
#include &lt;queue>
#include &lt;vector>

using namespace std;

constexpr int INF = INT32_MAX;

int disArr[501];

priority_queue&lt;pair&lt;int, int>> mypq;

vector&lt;pair&lt;int, int>> graph[10001];

vector&lt;int> trace[501];

int N, M, S, D, U, V, P;

void MyDijstra()
{  
    for (int i = 0; i &lt;= N; i++) disArr[i] = INF;

    for (int i = 0; i &lt; M; i++) {
        cin >> U >> V >> P;
        graph[U].push_back(make_pair(V, P));
    }

    // 우선순위 큐에 삽입.
    mypq.push({ 0, S }); // &lt; first : 거리 , second : 노드 인덱스 >

    disArr[S] = 0;

    while (!mypq.empty()) {
        // -를 붙이는 이유는 우선순위 큐를 이용하여 정렬하기 위함이다.
        // (최소힙으로 구현)
        int ncost = -mypq.top().first;
        int now = mypq.top().second;

        mypq.pop();

        // 이미 담겨 있는 것보다 거리가 크면 넘어간다.
        if (disArr[now] &lt; ncost) continue;

        // 해당 노드에서 연결된 모든 경로를 확인
        for (int i = 0; i &lt; graph[now].size(); i++) {
            // dissum = 임시 노드 + 현재 노드에서 i로가는 비용
            int dissum = ncost + graph[now][i].second;
            // 비용이 더 작다면 최단경로 테이블 값을 갱신.
            // 같아도 확인이 필요하다.
            // 최소 거리가 여러개 있을 수 있기 떄문
            if (dissum &lt;= disArr[graph[now][i].first]) {

                if (dissum &lt; disArr[graph[now][i].first]) {
                
                    // 임시 노드 업데이트
                    disArr[graph[now][i].first] = dissum;

                    trace[graph[now][i].first].clear(); // 새로 갱신한다면 전에 있던 것이 필요가 없다.
                    trace[graph[now][i].first].push_back(now); // 경로를 저장

                    // 우선순위 큐에 (거리, 노드 인덱스) 푸시
                    mypq.push(make_pair(-dissum, graph[now][i].first));
                }
                else trace[graph[now][i].first].push_back(now); // 경로를 저장 역방향으로 넣어준다.
            }
        }
    }

    // 최단 거리의 경로를 삭제해 줍니다.
    queue&lt;int> delQ;
    delQ.push(D);
    while (!delQ.empty()) {
        int nowDel = delQ.front();
        delQ.pop();
        for (int i = 0; i &lt; trace[nowDel].size(); i++) {
            for (auto it = graph[trace[nowDel][i]].begin(); it != graph[trace[nowDel][i]].end(); it++) {
                if ((*it).first == nowDel) {
                    delQ.push(trace[nowDel][i]);
                    graph[trace[nowDel][i]].erase(it);
                    break;
                }
            }
        }
    }


    // 새로운 데이크스트라를 진행하여 거의 최단 경로를 찾는다.
    for (int i = 0; i &lt;= N; i++) disArr[i] = INF;
    // 우선순위 큐에 삽입.
    mypq.push({ 0, S }); // first : 거리 , second : 노드 인덱스
    disArr[S] = 0;
    while (!mypq.empty()) {
        // -를 붙이는 이유는 우선순위 큐를 이용하여 정렬하기 위함이다.
        // (최소힙으로 구현)
        int ncost = -mypq.top().first;
        int now = mypq.top().second;

        mypq.pop();

        // 이미 담겨 있는 것보다 거리가 크면 넘어간다.
        if (disArr[now] &lt; ncost) continue;

        // 해당 노드에서 연결된 모든 경로를 확인
        for (int i = 0; i &lt; graph[now].size(); i++) {
            // dissum = 임시 노드 + 현재 노드에서 i로가는 비용
            int dissum = ncost + graph[now][i].second;
            // 비용이 더 작다면 최단경로 테이블 값을 갱신.
            if (dissum &lt; disArr[graph[now][i].first]) {

                disArr[graph[now][i].first] = dissum;
                // 우선순위 큐에 (거리, 노드 인덱스) 푸시
                mypq.push(make_pair(-dissum, graph[now][i].first));
            }
        }
    }

    for (int i = 0; i &lt;= N; i++) trace[i].clear();
    for (int i = 0; i &lt;= M; i++) graph[i].clear();
}


int main()
{
    ios_base::sync_with_stdio(false); // scanf와 동기화를 비활성화
    //cin.tie(null); 코드는 cin과 cout의 묶음을 풀어줍니다.
    cin.tie(NULL);
    std::cout.tie(NULL);

    while (true) {

        cin >> N >> M;

        if (N == 0 &amp;&amp; M == 0) break;

        cin >> S >> D;

        MyDijstra();

        if (disArr[D] == INF) cout &lt;&lt; -1 &lt;&lt; "\n";
        else cout &lt;&lt; disArr[D] &lt;&lt; "\n";
    }

    return 0;
}</pre>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<p class="has-medium-font-size">틀린 코드를 포기할 수 없어 여러가지 방법으로 접근하여 메모리를 줄여보았지만 의미는 없었다.</p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<figure class="wp-block-image size-full"><img decoding="async" width="1026" height="758" src="https://lycos7560.com/wp-content/uploads/2023/02/image-35.png" alt="" class="wp-image-6012" srcset="https://lycos7560.com/wp-content/uploads/2023/02/image-35.png 1026w, https://lycos7560.com/wp-content/uploads/2023/02/image-35-300x222.png 300w, https://lycos7560.com/wp-content/uploads/2023/02/image-35-768x567.png 768w" sizes="(max-width: 1026px) 100vw, 1026px" /></figure>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-5698326622209671" crossorigin="anonymous"></script>
<ins class="adsbygoogle" style="display:block" data-ad-format="autorelaxed" data-ad-client="ca-pub-5698326622209671" data-ad-slot="4245812909"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>



<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-5719%eb%b2%88-%ea%b1%b0%ec%9d%98-%ec%b5%9c%eb%8b%a8-%ea%b2%bd%eb%a1%9c-c-dijkstra-%ec%b6%94%ea%b0%80-%eb%b0%98%eb%a1%80-baekjoon/6006/">백준 5719번 (거의 최단 경로, C++, Dijkstra) / 추가 반례 [BAEKJOON]</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-5719%eb%b2%88-%ea%b1%b0%ec%9d%98-%ec%b5%9c%eb%8b%a8-%ea%b2%bd%eb%a1%9c-c-dijkstra-%ec%b6%94%ea%b0%80-%eb%b0%98%eb%a1%80-baekjoon/6006/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>백준 10217번 (KCM Travel, C++, Dijkstra) [BAEKJOON]</title>
		<link>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-10217%eb%b2%88-kcm-travel-c-dijkstra-baekjoon/5982/</link>
					<comments>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-10217%eb%b2%88-kcm-travel-c-dijkstra-baekjoon/5982/#respond</comments>
		
		<dc:creator><![CDATA[lycos7560]]></dc:creator>
		<pubDate>Fri, 03 Feb 2023 05:07:07 +0000</pubDate>
				<category><![CDATA[BaekjoonOnlineJudge]]></category>
		<category><![CDATA[C++/CPP]]></category>
		<category><![CDATA[10217]]></category>
		<category><![CDATA[10217번]]></category>
		<category><![CDATA[algorithm]]></category>
		<category><![CDATA[Baekjoon]]></category>
		<category><![CDATA[BFS]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[cin.tie(NULL)]]></category>
		<category><![CDATA[cout.tie(NULL)]]></category>
		<category><![CDATA[cpp]]></category>
		<category><![CDATA[Dijkstra]]></category>
		<category><![CDATA[DP]]></category>
		<category><![CDATA[DynamicProgramming]]></category>
		<category><![CDATA[KCM Travel]]></category>
		<category><![CDATA[priority_queue]]></category>
		<category><![CDATA[PriorityQueue]]></category>
		<category><![CDATA[Queue]]></category>
		<category><![CDATA[study]]></category>
		<category><![CDATA[공부]]></category>
		<category><![CDATA[그래프 이론]]></category>
		<category><![CDATA[그래프 탐색]]></category>
		<category><![CDATA[기본]]></category>
		<category><![CDATA[기초]]></category>
		<category><![CDATA[길찾기]]></category>
		<category><![CDATA[너비]]></category>
		<category><![CDATA[너비 우선 탐색]]></category>
		<category><![CDATA[다이나믹 프로그래밍]]></category>
		<category><![CDATA[다익스트라]]></category>
		<category><![CDATA[데이크스트라]]></category>
		<category><![CDATA[도로포장]]></category>
		<category><![CDATA[반례]]></category>
		<category><![CDATA[백준]]></category>
		<category><![CDATA[백준 10217]]></category>
		<category><![CDATA[백준 10217번]]></category>
		<category><![CDATA[시간]]></category>
		<category><![CDATA[알고리즘]]></category>
		<category><![CDATA[예제]]></category>
		<category><![CDATA[추가 반례]]></category>
		<category><![CDATA[추가 예제]]></category>
		<category><![CDATA[추가반례]]></category>
		<category><![CDATA[추가예제]]></category>
		<category><![CDATA[컴파일 에러]]></category>
		<category><![CDATA[코딩테스트]]></category>
		<category><![CDATA[코테]]></category>
		<guid isPermaLink="false">https://lycos7560.com/?p=5982</guid>

					<description><![CDATA[<p>백준(BAEKJOON) 10217번 'KCM Travel' 문제에 대한 글입니다. Dijkstra 알고리즘을 이용하여 해결하였습니다. (This is an article about the 'KCM Travel' problem in BAEKJOON 10217. It was solved using Dijkstra algorithm.)</p>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-10217%eb%b2%88-kcm-travel-c-dijkstra-baekjoon/5982/">백준 10217번 (KCM Travel, C++, Dijkstra) [BAEKJOON]</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-5698326622209671" crossorigin="anonymous"></script>
<!-- HorizontalAD -->
<ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-5698326622209671" data-ad-slot="6908948342" data-ad-format="auto" data-full-width-responsive="true"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>



<div style="height:62px" aria-hidden="true" class="wp-block-spacer"></div>


				<div class="wp-block-uagb-table-of-contents uagb-toc__align-left uagb-toc__columns-1  uagb-block-0e7c2a87      "
					data-scroll= "1"
					data-offset= "30"
					style=""
				>
				<div class="uagb-toc__wrap">
						<div class="uagb-toc__title">
							목차 테이블						</div>
																						<div class="uagb-toc__list-wrap ">
						<ol class="uagb-toc__list"><li class="uagb-toc__list"><a href="#kcm-travel" class="uagb-toc-link__trigger">KCM Travel</a><ul class="uagb-toc__list"><li class="uagb-toc__list"><a href="#문제" class="uagb-toc-link__trigger">문제</a><li class="uagb-toc__list"><li class="uagb-toc__list"><a href="#입력" class="uagb-toc-link__trigger">입력</a><li class="uagb-toc__list"><li class="uagb-toc__list"><a href="#출력" class="uagb-toc-link__trigger">출력</a><li class="uagb-toc__list"><li class="uagb-toc__list"><a href="#예제-입력-1" class="uagb-toc-link__trigger">예제 입력 1</a><li class="uagb-toc__list"><li class="uagb-toc__list"><a href="#예제-출력-1" class="uagb-toc-link__trigger">예제 출력 1</a><li class="uagb-toc__list"><li class="uagb-toc__list"><a href="#출처" class="uagb-toc-link__trigger">출처</a><li class="uagb-toc__list"><li class="uagb-toc__list"><a href="#알고리즘-분류" class="uagb-toc-link__trigger">알고리즘 분류</a><li class="uagb-toc__list"><li class="uagb-toc__list"><a href="#해결-방법" class="uagb-toc-link__trigger">해결 방법</a><li class="uagb-toc__list"><li class="uagb-toc__list"><a href="#통과된-코드-재채점-이후-시간초과" class="uagb-toc-link__trigger">통과된 코드 (재채점 이후 시간초과)</a><li class="uagb-toc__list"><li class="uagb-toc__list"><a href="#재채점-이후" class="uagb-toc-link__trigger">재채점 이후</a></ul></ol>					</div>
									</div>
				</div>
			


<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h1 class="wp-block-heading">KCM Travel</h1>



<p class="has-medium-font-size"><a href="https://www.acmicpc.net/problem/10217" target="_blank" rel="noreferrer noopener">https://www.acmicpc.net/problem/10217</a></p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<figure id="problem-info" class="wp-block-table"><table class="has-fixed-layout"><thead><tr><th class="has-text-align-left" data-align="left">시간 제한</th><th class="has-text-align-left" data-align="left">메모리 제한</th><th class="has-text-align-left" data-align="left">제출</th><th class="has-text-align-left" data-align="left">정답</th><th class="has-text-align-left" data-align="left">맞힌 사람</th><th class="has-text-align-left" data-align="left">정답 비율</th></tr></thead><tbody><tr><td class="has-text-align-left" data-align="left">10 초</td><td class="has-text-align-left" data-align="left">256 MB</td><td class="has-text-align-left" data-align="left">21759</td><td class="has-text-align-left" data-align="left">4834</td><td class="has-text-align-left" data-align="left">2300</td><td class="has-text-align-left" data-align="left">17.114%</td></tr></tbody></table></figure>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">문제</h2>



<p class="has-medium-font-size">각고의 노력 끝에 찬민이는 2014 Google Code Jam World Finals에 진출하게 되었다.&nbsp;</p>



<p class="has-medium-font-size">구글에서 온 초대장을 받고 기뻐했던 것도 잠시, 찬찬히 읽어보던 찬민이는 중요한 사실을 알아차렸다.&nbsp;</p>



<p class="has-medium-font-size">최근의 대세에 힘입어 구글 역시 대기업답게 비용 감축에 열을 내고 있었던 것이다.</p>



<p class="has-medium-font-size">초대장 내용에 의하면 구글은 찬민에게 최대 M원까지의 비용만을 여행비로써 부담해주겠다고 한다.&nbsp;</p>



<p class="has-medium-font-size">인천에서 LA행 직항 한 번 끊어주는게 그렇게 힘드냐고 따지고도 싶었지만, </p>



<p class="has-medium-font-size">다가올 결승 대회를 생각하면 대회 외적인 곳에 마음을 쓰고 싶지 않은 것 또한 사실이었다.&nbsp;</p>



<p class="has-medium-font-size">그래서 찬민은 인천에서 LA까지 M원 이하로 사용하면서 도착할 수 있는 가장 빠른 길을 차선책으로 택하기로 하였다.</p>



<p class="has-medium-font-size">각 공항들간 티켓가격과 이동시간이 주어질 때, </p>



<p class="has-medium-font-size">찬민이 인천에서 LA로 갈 수 있는 가장 빠른 길을 찾아 찬민의 대회 참가를 도와주자.</p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">입력</h2>



<p class="has-medium-font-size">입력 파일의 첫 번째 줄에 테스트 케이스의 수를 의미하는 자연수 T가 주어진다. </p>



<p class="has-medium-font-size">그 다음에는 T개의 테스트 케이스가 주어진다.</p>



<p class="has-medium-font-size">각 테스트 케이스의 첫 줄에는 공항의 수 N (2 ≤ N ≤ 100), 총 지원비용 M (0 ≤ M ≤ 10,000), </p>



<p class="has-medium-font-size">티켓정보의 수 K (0 ≤ K ≤ 10,000)가 공백으로 구분되어 주어진다.&nbsp;</p>



<p class="has-medium-font-size">이어서&nbsp;K개의 줄에 걸쳐 각 티켓의 출발공항 u, 도착공항 v (1 ≤ u, v ≤ N, u&nbsp;≠ v), 비용 c&nbsp;(1 ≤ c ≤ M, 정수), </p>



<p class="has-medium-font-size">그리고 소요시간 d (1 ≤ d ≤ 1,000) 가 공백으로&nbsp;구분되어 주어진다.&nbsp;</p>



<p class="has-medium-font-size">인천은 언제나 1번 도시이고, LA는 언제나 N번 도시이다</p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">출력</h2>



<p class="has-medium-font-size">각 테스트 케이스당 한 줄에 찬민이 LA에 도착하는 데 걸리는&nbsp;가장 짧은 소요시간을 출력한다.</p>



<p class="has-medium-font-size">만약, LA에 도착할 수 없는 경우 &#8220;Poor KCM&#8221;을 출력한다.</p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 입력 1</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">2
3 100 3
1 2 1 1
2 3 1 1
1 3 3 30
4 10 4
1 2 5 3
2 3 5 4
3 4 1 5
1 3 10 6</pre>



<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 출력 1</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">2
Poor KCM</pre>



<div style="height:50px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">출처</h2>



<p><a href="https://www.acmicpc.net/category/45" target="_blank" rel="noreferrer noopener">Contest</a>&nbsp;&gt;&nbsp;<a href="https://www.acmicpc.net/category/215" target="_blank" rel="noreferrer noopener">Coder&#8217;s High</a>&nbsp;&gt;&nbsp;<a href="https://www.acmicpc.net/category/detail/1280">Coder</a><a href="https://www.acmicpc.net/category/detail/1280" target="_blank" rel="noreferrer noopener">&#8216;</a><a href="https://www.acmicpc.net/category/detail/1280">s High 2014</a>&nbsp;D번</p>



<ul class="wp-block-list">
<li>데이터를 추가한 사람:&nbsp;<a href="https://www.acmicpc.net/user/doju" target="_blank" rel="noreferrer noopener">doju</a>,&nbsp;<a href="https://www.acmicpc.net/user/pichulia" target="_blank" rel="noreferrer noopener">pichulia</a></li>



<li>문제의 오타를 찾은 사람:&nbsp;<a href="https://www.acmicpc.net/user/kjho1e3" target="_blank" rel="noreferrer noopener">kjho1e3</a></li>



<li>문제를 만든 사람:&nbsp;<a href="https://www.acmicpc.net/user/tae" target="_blank" rel="noreferrer noopener">tae</a></li>
</ul>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">알고리즘 분류</h2>



<ul class="wp-block-list">
<li><a href="https://www.acmicpc.net/problem/tag/25" target="_blank" rel="noreferrer noopener">다이나믹 프로그래밍</a></li>



<li><a href="https://www.acmicpc.net/problem/tag/7" target="_blank" rel="noreferrer noopener">그래프 이론</a></li>



<li><a href="https://www.acmicpc.net/problem/tag/22" target="_blank" rel="noreferrer noopener">데이크스트라</a></li>
</ul>



<hr class="wp-block-separator has-alpha-channel-opacity is-style-wide" style="margin-top:var(--wp--preset--spacing--80);margin-bottom:var(--wp--preset--spacing--80)"/>



<h2 class="wp-block-heading">해결 방법</h2>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<figure class="wp-block-embed is-type-wp-embed"><div class="wp-block-embed__wrapper">
<blockquote class="wp-embedded-content" data-secret="XUKUyEZE81"><a href="https://lycos7560.com/cpp/%eb%b0%b1%ec%a4%80-1162%eb%b2%88-%eb%8f%84%eb%a1%9c%ed%8f%ac%ec%9e%a5-c-dijkstra-%ec%b6%94%ea%b0%80-%eb%b0%98%eb%a1%80-baekjoon/5955/">백준 1162번 (도로포장, C++, Dijkstra) / 추가 반례 [BAEKJOON]</a></blockquote><iframe loading="lazy" class="wp-embedded-content" sandbox="allow-scripts" security="restricted"  title="&#8220;백준 1162번 (도로포장, C++, Dijkstra) / 추가 반례 [BAEKJOON]&#8221; &#8212; 어제와 내일의 나 그 사이의 이야기" src="https://lycos7560.com/cpp/%eb%b0%b1%ec%a4%80-1162%eb%b2%88-%eb%8f%84%eb%a1%9c%ed%8f%ac%ec%9e%a5-c-dijkstra-%ec%b6%94%ea%b0%80-%eb%b0%98%eb%a1%80-baekjoon/5955/embed/#?secret=O9IYrbEt7s#?secret=XUKUyEZE81" data-secret="XUKUyEZE81" width="600" height="338" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe>
</div></figure>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<p class="has-medium-font-size">1162번 도로포장 문제와 유사한 방법으로 해결하였다.</p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<figure class="wp-block-image size-full"><img decoding="async" width="1600" height="1606" src="https://lycos7560.com/wp-content/uploads/2023/02/KakaoTalk_20230203_135946996-1.jpg" alt="" class="wp-image-5995" srcset="https://lycos7560.com/wp-content/uploads/2023/02/KakaoTalk_20230203_135946996-1.jpg 1600w, https://lycos7560.com/wp-content/uploads/2023/02/KakaoTalk_20230203_135946996-1-300x300.jpg 300w, https://lycos7560.com/wp-content/uploads/2023/02/KakaoTalk_20230203_135946996-1-150x150.jpg 150w, https://lycos7560.com/wp-content/uploads/2023/02/KakaoTalk_20230203_135946996-1-768x771.jpg 768w, https://lycos7560.com/wp-content/uploads/2023/02/KakaoTalk_20230203_135946996-1-1530x1536.jpg 1530w" sizes="(max-width: 1600px) 100vw, 1600px" /></figure>



<div style="height:60px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading"><s>통과된 코드 </s>(재채점 이후 시간초과)</h2>



<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>



<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">#include &lt;iostream>
#include &lt;vector>
#include &lt;queue>
#include &lt;tuple>

using namespace std;

priority_queue&lt;tuple&lt;long long int, int, int>> myPQ;

long long int disArr[101][10001];

constexpr long long int INF = INT64_MAX;

int T, N, M, K, U, V, C, D, cnt;

long long int Dijstra()
{
    cin >> N >> M >> K;

    // 다익스트라 배열 초기화
    for (int i = 0; i &lt;= N; i++)
        for (int j = 0; j &lt;= M; j++)
            disArr[i][j] = INF;

    vector&lt;tuple&lt;int, int, int>> graph[10000];

    for (int i = 0; i &lt; K; i++) {
        cin >> U >> V >> C >> D;
        graph[U].push_back(make_tuple(V, C, D));
    }

    // 우선순위 큐에 삽입.
    myPQ.push({ 0, 1, 0 }); // &lt;거리 , 노드 인덱스 , 비용>

    disArr[1][0] = 0;

    while (!myPQ.empty()) {
        // -를 붙이는 이유는 우선순위 큐를 이용하여 정렬하기 위함이다.
        // (최소힙으로 구현)
        long long int nCost = -get&lt;0>(myPQ.top()); // 현재 최단 거리

        int now = get&lt;1>(myPQ.top()); // 현재 노드

        int money = get&lt;2>(myPQ.top()); // 비용 

        myPQ.pop();
        // 이미 담겨 있는 것보다 거리가 크면 넘어간다.
        if (disArr[now][money] &lt; nCost) continue;
        // 해당 노드에서 연결된 모든 경로를 확인
        for (int i = 0; i &lt; graph[now].size(); i++) {

            // 비용의 범위가 초과된다면 continue
            if (money + get&lt;1>(graph[now][i]) > M) continue;

            // disSum = 임시 노드 + 현재 노드에서 i로가는 비용
            long long int disSum = nCost + get&lt;2>(graph[now][i]);

            // 비용이 작다면 최단경로 테이블 값을 갱신.
            if (disSum &lt; disArr[get&lt;0>(graph[now][i])][money + get&lt;1>(graph[now][i])]) {
                // 임시 노드 업데이트
                disArr[get&lt;0>(graph[now][i])][money + get&lt;1>(graph[now][i])] = disSum;
                // 우선순위 큐에 (거리, 노드 인덱스, 비용) 푸시
                myPQ.push({ -disSum, get&lt;0>(graph[now][i]), money + get&lt;1>(graph[now][i]) });

            }
        }
    }

    long long int result = INF;

    for (int i = 0; i &lt;= M; i++) {
       result = min(disArr[N][i], result);
    }

    return result;
}


int main()
{
    ios_base::sync_with_stdio(false); // scanf와 동기화를 비활성화
    // cin.tie(null); 코드는 cin과 cout의 묶음을 풀어줍니다.
    cin.tie(NULL);
    std::cout.tie(NULL);

    cin >> T;

    while (T-- > 0) {
        long long int result = Dijstra();
        if (result == INF) cout &lt;&lt; "Poor KCM\n";
        else cout &lt;&lt; result &lt;&lt; "\n";
    }

    return 0;
}

</pre>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<figure class="wp-block-image size-full is-resized"><img decoding="async" width="1024" height="46" src="https://lycos7560.com/wp-content/uploads/2023/02/image-33.png" alt="" class="wp-image-5984" style="width:1134px;height:51px" srcset="https://lycos7560.com/wp-content/uploads/2023/02/image-33.png 1024w, https://lycos7560.com/wp-content/uploads/2023/02/image-33-300x13.png 300w, https://lycos7560.com/wp-content/uploads/2023/02/image-33-768x35.png 768w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">재채점 이후 </h2>



<p>deque을 이용하여 해결</p>



<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">#include &lt;iostream>
#include &lt;vector>
#include &lt;deque>
#include &lt;cstring>
#define INF 987654321

using namespace std;

struct Info {
    int node;  // 도착 공항
    int cost;  // 티켓 비용
    int time;  // 소요 시간
};

int n, m, k;
vector&lt;vector&lt;Info>> edges;
int dp[101][10001];  // dp[i][j]: i번 공항에 j 비용으로 도착했을 때 최소 소요 시간

void solve() {
    // 공항의 연결 정보 및 dp 배열 초기화
    edges.clear();
    edges.resize(n + 1);

    for (int i = 0; i &lt;= n; i++) {
        for (int j = 0; j &lt;= m; j++) {
            dp[i][j] = INF;
        }
    }

    // 티켓 정보 입력
    for (int i = 0; i &lt; k; i++) {
        int u, v, c, d;
        cin >> u >> v >> c >> d;
        edges[u].push_back({ v, c, d });
    }

    // deque를 사용한 BFS 기반 탐색 (비용과 시간을 함께 고려)
    deque&lt;pair&lt;int, pair&lt;int, int>>> dq;  // {소요 시간, {공항, 비용}}
    dq.push_back({ 0, {1, 0} });  // 시작점: 1번 공항, 비용 0, 소요 시간 0
    dp[1][0] = 0;  // 1번 공항, 비용 0에서 시작

    while (!dq.empty()) {
        int time = dq.front().first;
        int here = dq.front().second.first;
        int cost = dq.front().second.second;
        dq.pop_front();

        // 이미 더 빠른 경로가 있는 경우 스킵
        if (dp[here][cost] &lt; time) continue;

        // 현재 공항에서 갈 수 있는 다음 공항 탐색
        for (auto&amp; next_info : edges[here]) {
            int next = next_info.node;
            int next_time = time + next_info.time;
            int next_cost = cost + next_info.cost;

            // 비용이 초과하면 무시
            if (next_cost > m) continue;

            // 다음 공항에 대한 비용-시간 정보가 더 최적이면 갱신
            if (next_time &lt; dp[next][next_cost]) {
                dp[next][next_cost] = next_time;
                // deque에 삽입할 때, 비용이 더 낮은 경로를 우선으로 처리
                if (!dq.empty() &amp;&amp; dq.front().first > next_time) {
                    dq.push_front({ next_time, {next, next_cost} });
                }
                else {
                    dq.push_back({ next_time, {next, next_cost} });
                }
            }
        }
    }

    int answer = INF;

    // 목적지 공항 n에 비용 m 이하로 도착하는 최소 소요 시간 탐색
    for (int i = 0; i &lt;= m; i++) {
        answer = min(answer, dp[n][i]);
    }

    // 결과 출력: 도착할 수 없으면 "Poor KCM", 그렇지 않으면 최소 소요 시간 출력
    if (answer == INF) cout &lt;&lt; "Poor KCM" &lt;&lt; endl;
    else cout &lt;&lt; answer &lt;&lt; endl;
}

int main(void) {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int t;
    cin >> t;  // 테스트 케이스 입력
    while (t--) {
        cin >> n >> m >> k;  // 공항 수, 최대 지원 비용, 티켓 정보 수 입력
        solve();
    }

    return 0;
}
</pre>



<p></p>



<figure class="wp-block-image size-full"><img decoding="async" width="1155" height="762" src="https://lycos7560.com/wp-content/uploads/2023/02/image-90.png" alt="" class="wp-image-38367" srcset="https://lycos7560.com/wp-content/uploads/2023/02/image-90.png 1155w, https://lycos7560.com/wp-content/uploads/2023/02/image-90-300x198.png 300w, https://lycos7560.com/wp-content/uploads/2023/02/image-90-768x507.png 768w" sizes="(max-width: 1155px) 100vw, 1155px" /></figure>



<figure class="wp-block-image size-full"><img decoding="async" width="1157" height="757" src="https://lycos7560.com/wp-content/uploads/2023/02/image-91.png" alt="" class="wp-image-38368" srcset="https://lycos7560.com/wp-content/uploads/2023/02/image-91.png 1157w, https://lycos7560.com/wp-content/uploads/2023/02/image-91-300x196.png 300w, https://lycos7560.com/wp-content/uploads/2023/02/image-91-768x502.png 768w" sizes="(max-width: 1157px) 100vw, 1157px" /></figure>



<figure class="wp-block-image size-full"><img decoding="async" width="1154" height="720" src="https://lycos7560.com/wp-content/uploads/2023/02/image-92.png" alt="" class="wp-image-38369" srcset="https://lycos7560.com/wp-content/uploads/2023/02/image-92.png 1154w, https://lycos7560.com/wp-content/uploads/2023/02/image-92-300x187.png 300w, https://lycos7560.com/wp-content/uploads/2023/02/image-92-768x479.png 768w" sizes="(max-width: 1154px) 100vw, 1154px" /></figure>



<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-5698326622209671" crossorigin="anonymous"></script>
<ins class="adsbygoogle" style="display:block" data-ad-format="autorelaxed" data-ad-client="ca-pub-5698326622209671" data-ad-slot="4245812909"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-10217%eb%b2%88-kcm-travel-c-dijkstra-baekjoon/5982/">백준 10217번 (KCM Travel, C++, Dijkstra) [BAEKJOON]</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-10217%eb%b2%88-kcm-travel-c-dijkstra-baekjoon/5982/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>백준 1162번 (도로포장, C++, Dijkstra) / 추가 반례 [BAEKJOON]</title>
		<link>https://lycos7560.com/cpp/%eb%b0%b1%ec%a4%80-1162%eb%b2%88-%eb%8f%84%eb%a1%9c%ed%8f%ac%ec%9e%a5-c-dijkstra-%ec%b6%94%ea%b0%80-%eb%b0%98%eb%a1%80-baekjoon/5955/</link>
					<comments>https://lycos7560.com/cpp/%eb%b0%b1%ec%a4%80-1162%eb%b2%88-%eb%8f%84%eb%a1%9c%ed%8f%ac%ec%9e%a5-c-dijkstra-%ec%b6%94%ea%b0%80-%eb%b0%98%eb%a1%80-baekjoon/5955/#respond</comments>
		
		<dc:creator><![CDATA[lycos7560]]></dc:creator>
		<pubDate>Thu, 02 Feb 2023 07:21:35 +0000</pubDate>
				<category><![CDATA[BaekjoonOnlineJudge]]></category>
		<category><![CDATA[C++/CPP]]></category>
		<category><![CDATA[1162]]></category>
		<category><![CDATA[1162번]]></category>
		<category><![CDATA[algorithm]]></category>
		<category><![CDATA[Baekjoon]]></category>
		<category><![CDATA[BFS]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[cin.tie(NULL)]]></category>
		<category><![CDATA[cout.tie(NULL)]]></category>
		<category><![CDATA[cpp]]></category>
		<category><![CDATA[Dijkstra]]></category>
		<category><![CDATA[DP]]></category>
		<category><![CDATA[DynamicProgramming]]></category>
		<category><![CDATA[priority_queue]]></category>
		<category><![CDATA[PriorityQueue]]></category>
		<category><![CDATA[Queue]]></category>
		<category><![CDATA[study]]></category>
		<category><![CDATA[공부]]></category>
		<category><![CDATA[그래프 이론]]></category>
		<category><![CDATA[그래프 탐색]]></category>
		<category><![CDATA[기본]]></category>
		<category><![CDATA[기초]]></category>
		<category><![CDATA[길찾기]]></category>
		<category><![CDATA[너비]]></category>
		<category><![CDATA[너비 우선 탐색]]></category>
		<category><![CDATA[다이나믹 프로그래밍]]></category>
		<category><![CDATA[다익스트라]]></category>
		<category><![CDATA[데이크스트라]]></category>
		<category><![CDATA[도로포장]]></category>
		<category><![CDATA[반례]]></category>
		<category><![CDATA[백준]]></category>
		<category><![CDATA[백준 1162]]></category>
		<category><![CDATA[백준 1162번]]></category>
		<category><![CDATA[시간]]></category>
		<category><![CDATA[알고리즘]]></category>
		<category><![CDATA[예제]]></category>
		<category><![CDATA[지름길]]></category>
		<category><![CDATA[추가 반례]]></category>
		<category><![CDATA[추가 예제]]></category>
		<category><![CDATA[추가반례]]></category>
		<category><![CDATA[추가예제]]></category>
		<category><![CDATA[컴파일 에러]]></category>
		<category><![CDATA[코딩테스트]]></category>
		<category><![CDATA[코테]]></category>
		<guid isPermaLink="false">https://lycos7560.com/?p=5955</guid>

					<description><![CDATA[<p>백준 1162번(BAEKJOON) '도로포장' 문제에 대한 글입니다. 'Dijkstra' 알고리즘을 이용하여 해결하였습니다. (This is an article on the issue of 'road pavement' by BAEKJOON. Solved using 'Dijkstra' algorithm.)</p>
<p>The post <a href="https://lycos7560.com/cpp/%eb%b0%b1%ec%a4%80-1162%eb%b2%88-%eb%8f%84%eb%a1%9c%ed%8f%ac%ec%9e%a5-c-dijkstra-%ec%b6%94%ea%b0%80-%eb%b0%98%eb%a1%80-baekjoon/5955/">백준 1162번 (도로포장, C++, Dijkstra) / 추가 반례 [BAEKJOON]</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-5698326622209671" crossorigin="anonymous"></script>
<!-- HorizontalAD -->
<ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-5698326622209671" data-ad-slot="6908948342" data-ad-format="auto" data-full-width-responsive="true"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>



<div style="height:62px" aria-hidden="true" class="wp-block-spacer"></div>


				<div class="wp-block-uagb-table-of-contents uagb-toc__align-left uagb-toc__columns-1  uagb-block-ad25557d      "
					data-scroll= "1"
					data-offset= "30"
					style=""
				>
				<div class="uagb-toc__wrap">
						<div class="uagb-toc__title">
							Contents						</div>
																						<div class="uagb-toc__list-wrap ">
						<ol class="uagb-toc__list"><li class="uagb-toc__list"><a href="#도로포장" class="uagb-toc-link__trigger">도로포장</a><li class="uagb-toc__list"><a href="#추가-예제" class="uagb-toc-link__trigger">추가 예제</a><li class="uagb-toc__list"><a href="#해결방법" class="uagb-toc-link__trigger">해결방법</a><li class="uagb-toc__list"><a href="#통과된-코드" class="uagb-toc-link__trigger">통과된 코드</a></ol>					</div>
									</div>
				</div>
			


<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>



<h1 class="wp-block-heading">도로포장</h1>



<p class="has-medium-font-size"><a href="https://www.acmicpc.net/problem/1162" target="_blank" rel="noreferrer noopener">https://www.acmicpc.net/problem/1162</a></p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<figure id="problem-info" class="wp-block-table"><table class="has-fixed-layout"><thead><tr><th class="has-text-align-left" data-align="left">시간 제한</th><th class="has-text-align-left" data-align="left">메모리 제한</th><th class="has-text-align-left" data-align="left">제출</th><th class="has-text-align-left" data-align="left">정답</th><th class="has-text-align-left" data-align="left">맞힌 사람</th><th class="has-text-align-left" data-align="left">정답 비율</th></tr></thead><tbody><tr><td class="has-text-align-left" data-align="left">2 초</td><td class="has-text-align-left" data-align="left">128 MB</td><td class="has-text-align-left" data-align="left">12239</td><td class="has-text-align-left" data-align="left">2532</td><td class="has-text-align-left" data-align="left">1530</td><td class="has-text-align-left" data-align="left">21.106%</td></tr></tbody></table></figure>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">문제</h2>



<p class="has-medium-font-size">준영이는 매일 서울에서 포천까지 출퇴근을 한다. </p>



<p class="has-medium-font-size">하지만 잠이 많은 준영이는 늦잠을 자 포천에 늦게 도착하기 일쑤다. </p>



<p class="has-medium-font-size">돈이 많은 준영이는 고민 끝에 K개의 도로를 포장하여 서울에서 포천까지 가는 시간을 단축하려 한다.</p>



<p class="has-medium-font-size">문제는 N개의 도시가 주어지고 그 사이 도로와&nbsp;이 도로를 통과할 때 걸리는 시간이 주어졌을 때 </p>



<p class="has-medium-font-size">최소 시간이 걸리도록 하는 K개의 이하의 도로를 포장하는 것이다.&nbsp;</p>



<p class="has-medium-font-size">도로는 이미 있는 도로만 포장할 수 있고,&nbsp;포장하게 되면 도로를 지나는데 걸리는 시간이 0이 된다. </p>



<p class="has-medium-font-size">또한 편의상 서울은 1번 도시, 포천은 N번 도시라 하고 1번에서 N번까지 항상 갈 수 있는 데이터만 주어진다.</p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">입력</h2>



<p class="has-medium-font-size">첫 줄에는 도시의 수 N(1 ≤ N ≤ 10,000)과 도로의 수 M(1 ≤ M ≤ 50,000)과</p>



<p class="has-medium-font-size">포장할 도로의 수 K(1 ≤ K ≤ 20)가 공백으로 구분되어 주어진다. </p>



<p class="has-medium-font-size">M개의 줄에 대해 도로가 연결하는&nbsp;두 도시와 도로를 통과하는데 걸리는 시간이 주어진다. </p>



<p class="has-medium-font-size">도로들은 양방향 도로이며, 걸리는 시간은 1,000,000보다 작거나 같은 자연수이다.</p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">출력</h2>



<p class="has-medium-font-size">첫 줄에 K개 이하의 도로를 포장하여 얻을 수 있는 최소 시간을 출력한다.</p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 입력 1</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">4 4 1
1 2 10
2 4 10
1 3 1
3 4 100</pre>



<div style="height:31px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 출력 1</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">1</pre>



<div style="height:60px" aria-hidden="true" class="wp-block-spacer"></div>



<h1 class="wp-block-heading">추가 예제</h1>



<p><a href="https://codeforces.com/blog/entry/95032" target="_blank" rel="noreferrer noopener">https://codeforces.com/blog/entry/95032</a></p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 입력 A</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">4 6 1
4 3 3 
3 1 3 
2 1 3 
2 4 200 
3 2 300 
1 2 200</pre>



<div style="height:31px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 출력 A</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">3</pre>



<div style="height:60px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 입력 B</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">10 20 3
1 3 50
1 8 3
1 10 15
2 10 37
2 9 42
2 9 14
2 3 44
2 8 26
3 6 80
3 6 60
3 7 12
3 9 66
3 4 81
4 8 6
5 10 45
5 6 73
6 7 83
6 7 83
7 10 14
8 10 59</pre>



<div style="height:31px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 출력 B</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">0</pre>



<div style="height:60px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 입력 C</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">100 500 3
1 9 574
1 11 907
1 18 924
1 14 652
1 21 900
1 14 456
1 12 997
2 10 174
2 15 872
2 22 658
2 13 621
2 10 72
2 13 198
2 15 153
2 21 922
2 7 749
2 21 469
2 18 154
2 12 13
3 12 667
3 9 560
3 4 949
3 22 829
3 14 758
4 23 952
4 21 126
4 18 498
4 22 299
4 20 552
5 17 431
5 14 978
5 24 749
5 24 135
5 8 485
5 13 87
5 17 51
6 13 201
6 23 223
6 12 791
6 9 424
6 26 998
6 24 186
6 14 658
7 8 786
7 18 410
7 16 615
8 9 952
8 21 44
8 17 404
9 12 555
9 10 433
9 17 476
9 13 39
9 28 54
10 18 803
10 25 909
10 29 108
10 28 157
10 25 615
10 29 526
11 22 942
11 25 688
11 16 372
11 21 73
11 18 172
12 14 813
12 18 18
12 28 717
12 14 890
12 31 707
12 29 769
12 24 53
12 24 977
13 24 312
13 29 96
13 23 150
14 31 146
14 17 195
14 28 750
14 28 381
14 16 167
14 33 426
15 22 564
15 32 495
15 27 938
15 20 6
15 29 273
15 18 407
15 28 364
15 24 786
16 17 277
16 28 931
16 35 320
16 17 471
17 20 510
17 18 687
17 35 949
17 34 427
17 27 238
18 20 633
18 25 432
18 31 494
19 29 651
19 21 859
19 20 399
19 39 436
19 34 550
20 33 13
20 24 127
21 31 341
21 22 828
21 23 120
21 34 911
22 23 264
22 33 888
22 35 132
23 38 914
23 42 43
23 33 151
23 36 474
23 24 273
23 29 373
23 34 755
24 32 483
24 30 599
24 34 421
25 35 329
25 28 680
25 37 579
25 42 480
26 43 882
26 45 399
26 27 833
26 45 15
26 38 773
26 38 724
27 43 242
27 33 270
27 37 252
27 29 69
28 33 295
28 36 350
28 44 619
28 42 542
28 34 242
28 33 876
28 31 500
28 46 715
28 43 934
29 41 559
29 32 430
29 34 721
29 39 466
30 37 822
30 39 777
30 36 373
30 47 145
30 33 256
30 36 219
31 50 986
31 50 557
31 38 275
31 40 6
31 42 536
31 49 791
31 36 86
31 43 552
32 49 447
32 34 865
32 39 982
32 37 818
33 47 168
33 48 140
33 45 365
33 51 402
33 45 5
33 49 314
34 35 758
34 44 113
34 42 421
34 35 350
34 47 330
35 54 253
36 47 601
36 37 727
36 40 958
36 51 847
36 44 105
36 38 641
36 46 850
37 42 886
37 38 695
37 41 299
37 55 196
37 51 747
37 49 874
38 52 849
38 57 443
38 50 91
38 55 213
38 44 708
38 49 285
38 55 17
39 46 6
39 50 70
39 58 450
39 53 609
40 41 791
40 47 199
40 57 718
40 42 821
40 59 40
41 56 666
41 53 978
41 60 380
41 56 491
42 51 992
42 62 452
42 44 694
42 60 567
42 60 452
42 43 922
43 53 721
43 51 974
43 45 765
43 62 779
43 54 392
44 49 935
44 63 704
45 53 903
45 55 343
45 50 285
46 56 899
46 60 810
46 65 464
46 61 661
46 66 107
47 50 285
47 59 288
47 59 28
47 66 351
47 55 20
47 67 802
48 52 750
48 62 235
48 53 281
48 55 187
48 62 848
48 52 278
48 68 896
48 52 357
49 67 800
49 66 491
49 51 577
49 65 661
50 55 912
50 63 372
50 54 177
50 57 77
50 57 318
50 66 363
50 56 665
51 56 106
51 66 847
51 69 879
52 53 573
52 61 391
52 67 135
52 58 125
52 69 630
52 53 104
53 60 226
53 70 844
53 58 473
53 57 687
53 72 111
53 57 791
54 72 630
54 70 203
54 63 47
55 60 751
55 75 194
55 56 28
55 70 459
55 69 127
55 61 302
55 60 97
55 66 443
55 61 847
56 61 925
56 62 90
57 77 776
57 65 289
57 74 545
58 64 334
58 59 163
58 77 775
58 66 671
58 66 182
59 70 971
59 64 915
59 73 702
59 60 336
59 68 747
59 66 829
60 75 804
60 64 421
60 75 65
60 67 224
60 75 802
60 62 803
61 78 545
61 66 429
61 70 171
62 79 748
62 78 280
62 67 39
62 68 968
62 79 733
63 82 657
63 70 70
63 78 175
64 79 568
64 66 675
64 77 29
64 74 280
64 81 362
65 73 417
65 74 795
65 71 886
65 75 70
65 66 633
66 77 117
66 77 28
66 71 648
66 72 102
66 84 914
66 84 358
66 70 98
66 67 406
66 86 734
67 70 651
67 82 747
67 79 180
67 69 248
67 81 34
67 71 707
67 83 687
67 79 828
68 69 66
68 77 425
69 72 721
69 83 305
69 71 464
69 71 565
69 72 1
70 75 740
70 87 148
70 82 374
70 85 642
70 89 300
70 86 84
70 75 437
70 83 738
71 84 220
71 80 520
71 82 856
71 90 955
71 82 788
71 88 861
71 83 432
71 80 309
71 75 207
72 75 723
72 92 412
72 87 714
72 77 222
72 92 303
73 76 483
73 93 721
73 74 940
73 90 929
74 86 181
74 78 956
74 91 433
74 88 942
74 87 451
74 75 354
74 85 968
74 79 887
74 88 475
74 83 485
74 89 728
75 80 745
75 78 517
75 95 212
75 76 166
75 78 243
75 79 857
75 84 283
76 95 985
76 81 891
76 83 518
76 79 335
76 90 381
76 89 409
76 88 92
76 87 218
77 90 289
78 81 954
78 93 732
78 94 733
78 94 209
79 90 47
79 92 322
79 90 832
79 94 949
79 98 147
79 92 282
79 99 20
80 96 176
80 99 312
80 84 80
80 94 514
80 93 209
80 89 267
81 94 694
81 93 673
81 97 445
81 85 108
82 83 698
82 100 213
82 84 963
82 91 175
82 94 69
82 89 890
82 86 375
82 92 949
83 100 397
83 100 685
83 99 696
83 96 276
83 97 260
83 99 299
83 85 850
83 96 522
83 96 855
84 85 903
84 85 733
84 87 230
84 92 192
84 97 833
84 95 372
85 100 392
85 91 525
85 89 378
85 86 718
86 94 132
86 88 966
87 91 49
87 90 464
87 88 411
87 92 811
87 91 25
88 94 794
88 97 57
88 89 412
88 95 72
88 95 389
88 89 504
89 100 86
89 94 953
89 91 612
90 92 476
90 93 29
90 99 771
90 95 928
90 93 381
91 99 868
91 98 872
91 95 281
91 96 960
92 98 703
92 98 776
92 95 252
92 95 942
93 95 994
93 99 107
94 97 80
94 96 971
94 99 183
95 97 186
95 97 480
96 97 64
96 98 476
96 100 847
96 100 164
97 100 266
98 100 258
98 100 520</pre>



<div style="height:31px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 출력 C</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">278</pre>



<div style="height:60px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 입력 D</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">500 2000 10
1 2 47274
1 12 37961
1 13 94095
1 5 88061
1 4 72314
1 18 87558
1 20 5639
1 3 30386
2 21 33567
2 15 39436
2 10 44870
2 19 69504
2 4 56250
2 20 22719
2 13 93754
2 8 56582
3 19 13558
3 14 50762
3 6 77110
3 11 21558
3 8 64302
3 15 12473
3 23 18576
3 21 6410
3 15 7202
3 14 18589
3 4 68753
3 8 13354
3 7 72886
4 21 94588
4 18 72136
4 16 17383
4 13 84674
4 20 61758
4 5 83447
4 7 12660
4 14 58771
4 13 68305
4 7 91684
5 16 50471
5 19 24937
5 19 23702
5 7 20186
5 10 28285
5 10 70408
6 13 1201
6 19 91637
6 24 74195
6 11 64295
6 8 68237
7 21 53561
7 26 68637
7 11 13091
7 18 94358
7 21 58748
7 27 34259
8 24 51565
9 17 33661
9 13 38935
9 23 1359
9 11 29224
10 28 52615
10 24 26202
11 27 57648
11 26 21100
11 30 37919
11 25 36533
11 16 64650
12 29 30710
12 26 69743
12 19 24943
12 22 85652
13 26 5189
13 22 76091
14 18 74467
14 29 73065
14 23 40286
14 28 18694
14 17 55006
15 20 56263
15 28 52617
15 35 32795
16 22 26674
16 27 65443
16 19 2850
16 19 76774
17 34 52374
17 29 73811
17 32 9994
17 22 71816
18 29 33171
18 22 37938
18 30 70627
18 19 99734
18 36 58617
18 35 33680
19 27 54151
19 28 19130
19 24 2456
19 39 13648
19 32 82353
19 24 54801
19 39 18375
20 31 31320
20 21 70706
20 27 24235
20 39 2833
21 31 93940
21 28 47822
21 35 25839
21 41 40067
21 41 8659
22 34 29853
22 33 90852
22 37 99279
23 36 49784
23 40 14298
23 43 9145
23 37 16140
24 34 60347
25 37 57063
25 42 44414
25 26 96468
25 31 98613
25 28 92759
26 43 80312
26 35 53781
26 35 56776
26 45 46115
27 38 94465
27 35 29472
27 41 81188
27 43 19846
28 30 68326
28 46 16861
28 36 30947
28 40 53185
28 39 90455
28 33 98485
29 41 27390
29 38 94103
29 40 85194
29 46 96646
29 46 82566
30 40 946
30 48 49673
30 45 92290
30 33 65852
30 33 93468
30 41 93439
31 50 87585
31 36 81065
31 34 95430
31 42 59199
31 32 55754
31 32 28055
32 42 22133
32 48 16798
32 48 89710
32 34 29078
32 46 60128
33 49 62147
33 50 59454
34 43 26888
34 35 40180
34 36 43246
34 36 3360
34 46 62693
34 38 89008
34 43 70686
34 51 13774
35 38 46280
36 50 99369
36 38 17401
36 39 70950
36 39 78320
36 54 97223
36 40 50137
37 55 21307
37 50 59760
37 51 5543
37 40 83545
37 38 60626
37 44 94015
38 57 44135
38 55 84764
38 50 46038
38 53 67321
38 48 7180
39 44 10790
39 43 18037
39 42 99116
39 57 70175
39 48 64017
39 56 90657
40 60 62284
40 53 63963
40 48 68873
40 55 65520
40 53 92799
40 50 32641
41 44 84480
41 52 17436
41 45 57089
41 47 1941
42 60 12251
42 47 42803
43 53 98178
43 54 43631
43 54 62276
43 50 94472
44 53 52931
44 56 52090
44 46 2502
45 64 48336
45 52 79165
45 65 8600
45 64 31620
45 62 27015
46 65 51082
46 66 41644
46 62 13042
46 57 89545
46 58 61801
46 48 69391
47 61 45529
47 51 94953
47 60 39758
48 52 48188
48 62 30904
48 52 76301
48 57 71120
48 64 94931
48 61 55006
49 67 6287
49 50 2682
49 57 99290
49 58 95921
49 64 61182
49 55 96938
49 69 21495
50 57 6970
50 60 40155
51 62 75941
52 58 55528
52 63 35894
52 61 89100
52 66 9025
53 66 85099
53 64 71127
53 59 80925
53 63 84858
53 54 41190
53 55 82385
53 56 85866
53 70 32893
54 72 20064
54 71 45757
54 64 20598
54 69 12544
55 60 66230
55 69 61418
55 61 97815
56 67 93896
56 60 43111
57 77 71815
57 58 89064
57 76 26769
57 70 59736
58 75 25434
58 74 23141
58 59 34090
59 64 85761
59 70 67020
59 73 26499
59 60 85750
59 65 57422
59 73 13994
59 76 85989
59 60 63565
59 63 49055
60 78 51029
60 66 17464
60 64 48857
61 78 32328
61 70 6803
61 81 85016
61 72 90073
62 79 92562
62 76 70157
62 73 59465
62 66 1329
62 72 65081
64 66 43343
64 83 98559
64 81 46520
64 77 796
65 66 28075
65 69 45350
66 82 62216
66 72 10751
66 73 16965
66 86 87658
66 85 89970
66 82 34988
67 71 43304
67 79 86336
68 79 92935
68 69 12958
68 74 20688
68 71 32924
68 77 83600
69 83 52276
69 76 32687
70 82 61640
70 82 45831
70 88 93492
70 84 15129
71 73 81396
71 72 66332
71 91 50870
72 74 63335
72 91 42576
73 91 79345
73 82 78117
73 87 38759
73 91 30644
73 75 27998
74 85 32449
74 91 16106
74 84 23881
74 93 48270
74 86 60656
74 83 74773
75 80 21789
75 78 71296
75 91 74120
76 83 85856
76 90 83346
76 93 81687
76 80 68166
76 90 63482
76 79 90207
76 90 30794
76 94 1883
77 81 1076
77 96 47614
77 88 53221
78 93 98426
78 84 95345
78 83 73021
79 82 2118
79 86 66427
79 92 87981
79 85 6269
80 82 88977
80 96 59562
80 81 70595
81 95 19798
81 91 55420
82 100 81689
82 96 89076
82 91 84241
82 100 35027
83 99 32458
83 97 47799
83 99 64508
83 85 88406
83 90 97254
83 103 85556
84 94 80362
84 87 52582
84 91 41001
85 93 13496
85 96 41760
86 94 93196
86 103 25219
86 91 60324
87 107 3161
87 103 82082
87 100 80024
88 97 69053
88 102 84291
88 104 80423
88 92 70449
88 107 65934
89 107 51568
89 91 61759
89 93 22793
89 109 68161
90 103 65366
90 101 50561
91 98 209
91 101 9306
92 94 95167
92 102 58968
92 93 21709
92 100 30301
93 112 72873
93 99 38019
93 99 39332
93 96 42434
93 101 51063
93 99 2580
94 101 13028
95 101 71000
95 97 65118
95 108 70773
95 103 92172
95 109 90943
95 110 37977
96 113 64318
96 98 336
96 102 61469
96 114 454
96 115 68928
96 109 16072
96 116 22650
96 102 93111
97 116 7309
97 117 57569
98 110 42691
98 112 1378
98 99 73278
98 105 94395
98 117 27195
98 108 86241
98 107 60188
99 106 13113
99 106 41221
99 103 85859
100 110 40471
100 101 26148
100 108 37372
100 114 22745
100 115 92258
100 119 93897
101 116 87116
101 111 6970
101 114 41573
101 120 1782
102 115 48758
102 121 44303
102 107 10985
102 104 22257
103 115 36959
103 121 3143
103 108 33392
104 122 12817
104 110 17201
105 108 26388
105 125 74395
105 123 24498
106 113 57961
107 108 91644
107 115 22577
107 121 31478
108 112 42295
108 110 60666
108 127 50703
108 109 20527
109 110 44818
109 116 93290
109 122 95250
109 118 68182
109 121 63169
109 114 64624
110 115 45807
110 112 7942
110 129 89347
111 129 24850
111 118 40028
112 116 19474
112 120 91512
112 127 49066
112 121 1220
113 121 52299
113 118 37973
113 122 5909
113 122 28727
113 127 96587
114 124 67812
114 129 30502
114 124 28167
114 123 573
114 129 72433
115 123 195
115 119 81084
115 125 9538
117 120 3311
117 118 6327
117 135 95073
117 121 11421
117 129 30033
117 126 8607
117 121 47157
117 126 21224
117 137 7937
117 131 33997
117 119 13803
118 138 74814
118 121 71581
118 119 25448
118 129 76954
118 129 1518
118 122 56101
118 126 90042
119 129 61614
119 122 23358
119 138 38878
119 139 20384
119 138 53461
119 127 18598
119 135 46322
120 124 39245
120 123 14627
120 126 46401
120 136 42653
121 122 3608
121 138 5038
121 140 95807
121 139 39793
122 123 3658
122 135 68083
122 133 24118
123 142 81124
123 133 77169
123 136 98447
123 126 85756
123 132 34477
124 143 70079
124 142 438
124 144 61614
125 140 58554
125 144 26299
125 143 26784
125 135 66108
126 140 17501
126 137 81391
126 133 11303
126 133 87483
127 143 84890
127 133 31816
127 145 22507
127 140 27522
128 144 55419
128 134 51722
128 133 8926
128 135 61742
128 137 29852
128 146 50378
128 148 47064
128 129 83902
129 133 72143
130 139 37777
130 135 24292
130 150 56087
130 150 64266
131 146 89648
131 146 96688
131 143 67569
131 132 33668
132 148 67398
132 141 71825
133 145 25224
133 135 66923
133 152 54091
134 149 97565
134 151 29516
135 145 37073
135 151 20889
136 137 24112
136 151 97569
136 152 7610
136 144 57374
137 146 31624
137 144 47344
137 151 69711
137 142 76188
138 150 14296
138 151 79972
138 140 50575
138 149 85385
138 149 13400
138 144 71054
139 148 59913
139 158 84655
139 141 55716
139 154 75649
139 150 89251
139 156 88636
139 151 8601
140 156 24375
140 150 25542
140 144 2403
140 146 16619
140 145 49978
141 153 91423
141 142 76361
142 158 96497
142 158 97164
142 161 99792
142 158 42697
143 150 39175
143 155 37360
145 153 15020
145 163 22305
145 165 68492
145 163 13967
145 147 1887
145 165 94515
146 150 1426
146 157 65315
146 159 70516
146 148 1589
146 147 79440
146 163 65343
147 156 61805
147 167 1105
147 149 39683
147 163 10277
147 161 29487
147 149 20341
147 153 47288
147 162 27502
148 162 30979
148 152 17924
148 157 30932
148 158 21063
148 158 85942
149 152 17028
149 154 64397
149 151 98094
149 162 82554
149 150 80126
149 156 87550
149 162 75289
150 153 85955
150 154 88241
150 164 28871
150 163 14143
150 153 13786
150 153 94959
150 169 50778
151 164 81148
151 161 1757
151 153 71102
151 170 81424
151 165 26769
151 166 94605
152 167 1401
152 162 58209
152 161 93826
152 162 1999
153 162 77356
153 169 86703
154 170 69127
154 161 80012
154 156 7643
154 168 26189
154 160 74508
155 175 47985
155 173 19164
155 156 2928
155 161 67729
155 169 87567
155 158 69032
156 158 26676
156 162 70634
156 174 1164
156 167 48735
156 170 84906
157 176 68240
157 160 864
159 160 19393
159 172 274
159 170 61694
159 178 14457
159 174 75189
159 173 59206
160 178 53413
160 166 54248
160 163 99589
160 175 8623
161 177 28233
161 170 62962
161 167 352
162 174 89950
162 175 90577
162 172 96287
162 180 18855
163 182 5732
164 176 71035
164 172 1438
164 165 71153
164 171 62906
164 180 2341
164 169 21728
165 174 35496
165 168 82074
165 177 10757
166 170 46712
166 176 22649
166 177 27891
166 185 18547
166 180 14962
166 181 56072
166 183 63146
167 170 5865
167 182 23464
167 179 70425
167 172 91796
167 182 15274
167 177 286
167 180 4408
168 188 67404
168 169 37352
168 176 42168
169 175 74689
169 183 80979
169 172 82332
169 179 7070
169 177 20777
170 187 20987
170 171 83907
170 186 53807
171 178 78836
171 175 71531
171 185 4587
171 172 57243
171 189 18141
171 190 8415
171 189 70473
171 186 90584
172 192 50769
172 174 12741
172 185 45430
172 177 51534
172 184 49636
173 176 80
173 182 75118
174 191 91707
174 187 8074
174 180 54584
174 183 3200
174 192 55888
174 179 48207
174 177 80185
175 187 25889
176 188 90257
176 186 43808
176 193 20128
176 194 47417
176 191 26009
176 189 50704
176 191 38065
177 182 11076
177 178 86212
177 195 40596
178 193 46852
178 189 99042
178 181 71011
178 184 7511
179 190 33987
179 192 29911
179 198 87147
179 195 54726
180 197 95213
180 183 15644
180 196 19654
180 192 82805
181 189 61542
182 194 45596
182 192 71265
182 191 27481
182 188 51045
182 191 21656
182 186 43372
182 201 94188
182 188 80676
182 202 62163
183 195 27022
183 185 70300
183 186 56884
185 203 83543
185 189 20366
185 204 67852
185 199 21159
185 199 21342
185 188 76155
186 202 92273
186 197 63299
186 193 58362
186 195 45237
186 188 70534
187 191 8998
187 190 18350
187 207 51617
187 204 12537
187 203 70221
187 192 25090
188 195 97545
188 202 40219
189 200 92683
189 196 41523
189 198 87834
189 204 87140
189 190 86568
189 201 14256
189 197 83140
190 193 70237
190 195 84511
190 198 39182
191 206 58638
191 195 87605
191 211 91583
191 197 92038
191 198 6559
191 199 6749
191 198 65307
191 195 62658
192 211 47526
192 193 52251
192 199 21690
192 195 18819
192 196 66819
192 194 89450
193 209 96200
193 195 28686
193 205 12827
193 207 78600
194 197 53523
194 207 67753
194 200 24280
194 210 30135
194 199 85039
194 199 36275
194 205 37958
194 205 56397
195 211 34627
195 196 88234
195 204 7512
195 204 54021
195 202 22633
196 204 99731
196 213 62129
196 199 15547
196 202 946
196 216 40614
197 213 11851
197 204 3843
197 211 93007
197 204 88287
197 203 30596
198 212 43603
198 213 36457
198 210 35838
198 204 731
199 200 73956
199 200 50266
199 214 199
199 210 68760
199 209 28165
200 201 15569
200 202 82306
200 220 8081
200 203 47192
200 207 28227
200 216 19612
200 211 40102
200 204 26515
201 215 10613
201 220 94437
201 212 62422
201 210 33425
201 220 40674
202 217 26817
202 213 3671
202 204 43103
202 205 32539
203 213 55063
203 223 27122
203 212 81967
203 204 22066
203 212 48949
203 219 70949
204 223 40248
204 218 38414
204 218 48457
204 212 65568
204 213 1827
204 219 95019
205 217 98553
205 220 63784
205 211 18399
205 217 59351
206 215 59964
206 211 58022
207 219 34111
207 221 91013
207 223 89375
207 225 13577
207 226 54071
208 218 10766
208 226 60098
209 223 44656
209 221 14419
209 213 85115
209 219 95992
210 230 30289
210 214 47327
210 212 45852
211 221 33098
211 218 12405
211 212 52032
211 224 56677
211 212 93444
211 229 74393
211 213 89722
212 217 11995
212 216 22102
212 217 54607
212 224 1687
212 222 22363
213 227 87493
213 217 347
213 218 27319
214 228 69779
214 218 76632
214 224 32326
214 233 68298
214 216 45783
214 218 52555
214 221 94079
214 215 70328
214 232 20478
214 226 23295
215 229 50149
215 224 83817
215 217 46051
215 235 77496
215 224 45390
215 222 47197
216 217 61220
216 235 76046
216 236 21347
217 223 69638
217 230 70182
217 229 64189
217 234 49281
218 224 48713
218 231 85247
218 230 13045
218 227 56441
219 220 79617
219 228 90586
219 234 95450
219 236 60049
220 231 33787
220 233 75383
220 232 21828
220 233 37298
220 240 42475
220 229 91305
220 237 68065
220 235 30920
221 241 34630
221 222 33222
221 235 34030
221 230 65685
221 227 30357
221 234 52730
222 223 59650
222 230 88766
222 227 22537
223 239 52596
224 234 26083
224 236 41212
225 229 75697
225 240 4007
226 227 52751
226 239 33116
226 230 54758
226 237 78138
227 231 69476
227 243 88815
227 230 17008
227 245 14361
228 230 96168
228 243 73643
228 246 61357
228 232 21074
228 241 65922
230 244 11161
230 236 64049
231 246 52729
231 239 4591
232 239 47029
232 233 65473
232 247 71621
232 240 75918
233 235 42736
233 242 29159
233 239 65248
233 242 17157
233 249 73825
233 253 19553
234 239 15851
234 235 67306
235 237 98416
235 252 90274
235 247 1495
236 244 71546
236 247 9786
236 244 74543
236 256 93181
237 246 77669
238 244 63088
238 249 68868
238 248 74743
238 239 15311
239 247 59960
239 250 18892
241 260 92613
241 258 8433
241 251 85754
242 247 48508
242 246 23289
242 262 31047
242 247 77252
243 260 34969
243 258 64683
243 252 656
243 254 42551
243 252 30653
244 255 90086
245 255 36641
246 255 3128
246 252 95303
247 262 2169
248 255 11889
248 262 57456
248 253 89816
248 260 99607
249 250 62142
250 262 6796
250 265 8375
250 252 33111
250 257 3047
250 262 47005
251 254 44893
251 264 63636
251 268 82160
252 253 41448
252 261 94409
252 261 77678
252 259 12498
252 270 23768
252 259 84957
252 260 58136
252 261 11656
252 267 94520
253 261 68847
253 260 25032
253 254 81858
254 264 26601
254 272 47193
254 265 70107
254 268 27270
254 260 14546
255 265 48762
255 272 77422
255 261 51162
255 262 1978
256 274 64387
256 272 36149
257 276 19724
257 269 38032
258 264 74635
258 276 50440
258 277 54650
258 272 74692
258 263 70091
259 266 32097
259 272 72378
260 275 18012
260 269 25074
261 266 92031
261 268 52099
261 276 2576
261 263 77187
261 272 48806
261 276 39794
261 271 910
261 275 9218
261 262 31677
261 263 86758
262 277 48902
262 269 27514
262 265 21791
262 273 85669
262 282 63407
263 279 82522
263 269 22794
264 281 42837
264 281 52017
264 276 67668
265 275 88083
265 281 43091
265 276 8232
265 283 29164
265 283 37904
265 279 55135
266 277 98359
266 271 78623
266 284 52028
266 276 46941
266 284 9331
266 281 38905
267 268 50583
267 275 75910
267 276 59332
267 272 24302
267 284 72148
267 276 52454
267 274 89351
268 272 41810
268 269 81705
268 285 38972
268 277 98159
268 283 31201
269 271 38002
269 283 83772
270 286 67583
270 272 37256
270 286 4158
271 272 2704
271 288 41218
271 284 32489
271 291 95984
271 278 70195
272 291 34051
272 289 13115
272 275 48016
273 293 43541
273 276 54901
273 282 98264
273 277 64487
273 280 97050
274 289 67005
274 282 54766
274 280 81863
274 284 67914
275 283 11411
275 284 61790
275 292 12464
276 290 80242
276 289 69156
276 296 81504
276 290 16892
277 291 10194
277 293 33923
277 286 66988
277 294 72452
277 281 75437
277 294 52403
277 283 25555
278 284 2697
278 297 91245
278 296 19614
278 282 89764
278 291 4045
278 298 64300
278 283 70226
278 296 79559
278 285 50742
279 294 85292
279 293 19295
280 289 45942
280 290 74145
280 289 36447
280 293 51786
281 298 33439
281 285 30781
281 293 11248
281 294 51501
282 288 7060
282 290 26604
282 287 30152
282 296 48788
282 296 74026
284 285 77094
284 286 64042
284 292 76594
284 285 51693
285 305 88006
285 292 95368
285 288 45738
285 292 63627
285 303 94283
286 300 67019
286 306 85318
286 298 42324
286 295 12840
286 288 46127
287 300 17876
287 302 6881
287 304 81364
287 301 2196
287 296 92899
287 301 89434
287 294 62059
288 307 90798
288 304 92507
288 293 46558
289 309 10331
290 295 85430
290 298 4210
290 307 85598
291 305 76104
291 309 88328
291 304 44792
291 309 11490
291 303 81320
292 310 515
292 297 91260
292 308 26819
293 304 33911
293 296 14651
294 299 70148
294 305 34064
294 298 42869
294 303 62202
294 310 58361
294 298 76618
295 303 72099
295 314 1707
295 300 20041
295 315 18151
296 311 59850
296 306 11373
296 309 93459
296 315 92898
296 311 87364
296 316 89383
296 306 77150
296 308 4905
297 314 20960
297 307 10830
297 312 19459
297 303 99278
298 318 86114
299 306 88992
300 315 15630
300 302 99624
300 315 92193
300 306 17693
300 314 58321
301 308 49183
302 320 10264
302 306 30760
303 310 86273
303 310 90306
303 305 61557
303 318 50890
303 320 85160
303 315 19940
304 310 14093
304 312 16224
304 314 75073
304 308 20059
305 315 41478
305 315 27400
306 318 45102
306 311 31344
307 316 41116
307 326 25096
307 322 94549
307 326 80115
307 314 71196
307 323 29735
307 320 31855
307 314 36356
308 316 69868
308 315 35522
308 315 20616
308 312 90538
308 312 85953
308 315 90040
308 327 50098
309 318 97519
309 323 93543
310 318 12014
310 319 39442
310 319 12178
310 330 31533
310 321 28028
311 330 80209
311 316 35972
313 324 81319
313 314 91246
314 328 43244
314 329 67372
314 315 16218
314 332 92932
314 318 42805
314 333 24468
315 321 20755
315 318 14451
315 322 62688
315 332 62969
316 335 37648
316 329 99746
316 329 59622
316 333 78702
317 337 62517
318 324 32141
318 325 42677
318 319 2730
318 322 7993
318 333 66709
319 339 36990
319 324 91413
319 326 23846
319 331 85886
320 323 67526
321 337 78655
321 323 81437
322 342 32175
323 326 12694
323 335 20314
324 330 54351
324 340 61958
324 339 5675
324 343 76779
324 331 49349
325 345 99778
325 332 7926
325 326 47276
325 336 31801
326 333 10343
326 331 53165
326 345 42333
326 335 57147
326 331 23149
326 342 96296
326 344 81806
327 343 52829
327 329 97761
328 345 76662
328 338 8311
328 339 91783
329 335 26627
329 341 24085
329 343 12368
329 342 83188
329 342 80694
330 336 50317
330 345 76793
330 334 89683
330 341 81043
330 338 11574
331 336 13876
331 336 94615
331 347 87159
331 338 99869
332 337 25814
332 346 88071
332 351 55056
332 339 98211
332 346 23930
333 342 91695
333 343 17588
334 344 41198
334 343 18990
334 350 6301
335 344 70314
335 347 25638
335 345 23075
336 347 93189
336 347 11071
337 341 72729
337 342 70241
337 344 19949
337 352 26178
337 339 1784
338 348 4433
338 353 98897
338 354 39448
338 352 93991
339 353 22801
339 344 62145
340 347 83193
340 350 38801
340 347 76724
340 354 52686
340 356 39265
340 342 69714
341 342 33935
342 354 3201
342 343 86112
342 348 77787
342 361 64973
342 348 65956
343 350 30778
343 344 61154
343 357 44572
344 364 29174
344 363 67992
344 358 21183
344 363 77997
344 356 42363
345 363 981
345 357 35728
346 356 26168
346 366 21498
346 359 69826
346 363 82406
347 359 96342
347 357 57454
347 360 84931
347 353 68341
347 366 59270
347 363 42774
348 349 73568
348 354 88872
348 353 45305
348 352 98293
348 363 19844
349 355 52715
349 356 75909
349 365 66402
350 355 90065
350 370 21631
350 369 91797
350 360 62024
352 363 57498
352 367 5481
352 360 77095
352 372 87911
352 353 12087
352 359 16691
353 358 38445
353 368 32737
353 369 54485
354 368 88586
355 361 99053
355 362 49055
355 368 18420
355 364 71887
356 374 83217
356 365 21722
356 366 87383
356 370 44006
356 371 72408
357 374 74674
357 365 43496
357 374 75434
357 364 69683
357 373 69242
357 368 84081
357 374 19990
358 377 15656
358 361 51258
358 367 71491
359 372 31101
359 371 44947
360 377 90955
360 364 82339
361 375 91316
361 368 93728
361 367 71039
362 363 9352
362 380 55699
362 373 12674
362 369 70311
362 371 19423
362 364 70615
362 364 1007
364 379 1626
364 370 11959
364 383 84243
364 367 25489
365 369 40949
365 381 47893
365 381 10778
366 373 9271
366 370 91797
367 383 30689
367 383 88519
367 375 43538
367 370 82495
367 368 3543
368 371 11813
368 379 56249
368 378 39433
368 380 30279
368 375 56711
369 378 75813
369 379 21886
369 389 10117
369 371 8337
369 371 46950
369 379 40702
370 386 50154
370 377 49197
370 378 22556
370 386 30235
370 388 55553
371 380 97512
371 375 50526
371 391 56301
371 377 48523
371 373 78178
371 380 11047
372 379 85231
372 379 61164
372 389 84834
373 386 58836
373 385 70016
373 379 939
373 391 99007
373 378 39703
374 386 5859
374 390 77878
374 377 49303
375 383 51607
375 382 91743
375 377 88236
376 387 37052
376 385 67709
376 387 5872
376 388 13729
376 393 80914
376 380 43929
376 378 45428
377 383 76781
377 384 93491
377 393 16347
379 393 21279
379 386 26253
379 397 38588
379 389 59723
379 395 14855
380 394 55344
380 385 20477
380 399 80321
381 384 58281
381 396 61052
381 398 3196
382 384 30925
382 393 47746
382 397 11959
382 393 46930
382 401 9089
383 400 76295
383 394 41422
383 397 22733
383 395 9266
383 385 32621
384 396 20786
384 388 79869
385 391 42027
385 401 7585
385 403 89685
385 393 9799
386 399 42681
386 406 90130
386 395 79601
386 397 19572
386 391 75920
386 405 55826
386 394 35746
387 394 62307
387 407 11556
387 402 99913
387 398 26039
387 399 19843
388 405 15665
388 396 45730
388 407 9493
389 391 42042
389 400 71748
389 401 43627
389 403 5076
390 399 73169
390 406 94528
391 403 47145
391 403 86592
391 406 20454
391 410 93146
391 398 55650
391 401 61433
392 407 79288
392 406 73773
392 409 98082
392 404 60883
393 406 8286
393 409 84839
393 397 46103
393 402 65367
393 399 3745
394 414 9530
394 400 32644
394 413 10176
394 398 17981
395 402 93675
395 411 94945
395 403 5071
395 413 3060
395 396 5814
395 405 74058
396 411 10344
396 397 35627
396 415 1129
396 407 78844
397 416 75234
397 400 90523
397 412 22468
397 416 9835
397 405 14955
398 410 32738
398 401 73552
399 407 72495
400 411 91137
400 416 287
400 403 12645
400 417 59265
400 402 99370
401 421 69897
401 402 88418
401 410 22723
402 411 59982
402 416 84233
402 412 9049
402 409 61350
403 406 61000
403 410 8539
403 411 11808
403 405 65345
403 423 56056
404 414 73297
404 418 20353
405 420 53361
405 416 95540
406 420 72896
406 415 72506
406 413 16499
406 410 60994
406 416 87105
406 413 7438
407 408 14940
408 422 98927
408 426 20705
408 426 37171
409 411 91202
409 420 3403
409 421 94225
409 416 76418
409 424 59424
409 424 72507
409 429 50695
411 427 37818
411 431 61286
411 412 60198
411 417 40107
411 427 82733
411 422 83478
412 418 97128
412 414 83292
412 422 55297
412 416 70121
413 425 36227
413 419 75640
413 431 58016
414 434 40120
414 422 31705
415 428 40871
415 433 61241
416 428 13314
416 424 50066
416 436 65713
416 427 91421
416 429 32189
416 418 60076
416 423 34627
416 435 98771
417 429 51741
417 429 73654
418 426 21615
418 434 46484
419 436 75999
420 434 45109
420 435 11432
420 424 51055
421 438 3937
421 435 76383
422 433 69594
422 439 22455
422 425 11414
422 435 72786
422 435 93472
422 434 72640
423 438 19127
423 433 53662
423 434 37723
424 436 25621
424 439 19965
424 429 80775
424 431 2962
424 437 80773
425 430 9625
425 435 81012
425 433 37229
425 444 58945
425 441 76620
425 441 73587
425 429 5625
426 445 7808
426 435 22159
426 442 53699
426 430 71047
426 435 3388
427 437 57276
427 440 10072
427 445 73709
427 435 42530
427 444 49203
428 431 90826
428 442 91763
428 441 36469
429 443 11158
429 434 81350
429 445 12855
429 438 43180
429 435 88605
429 433 35595
429 447 84634
429 444 71903
430 442 13842
430 450 46606
430 439 8960
430 445 20926
431 441 59382
431 440 88776
431 448 8916
432 449 99227
432 445 7963
432 438 25491
432 452 81202
432 439 41819
432 447 24120
432 436 56192
432 447 93035
432 436 68520
432 439 57929
432 433 11140
432 441 29973
433 445 54722
433 446 11996
433 449 51126
433 447 34840
433 453 6649
433 447 27860
434 447 77042
434 441 49906
434 444 18181
434 450 83946
435 450 18409
435 449 69539
436 451 11983
436 455 90151
436 452 53903
436 446 82274
436 446 27909
437 451 28459
437 445 37452
438 457 87190
438 454 49561
438 443 44025
438 452 66508
438 451 35246
439 441 61955
439 444 51209
439 458 30175
440 448 26170
440 455 15194
440 457 96245
440 442 3977
441 455 64592
441 448 37567
441 443 70844
441 459 52339
441 443 47896
443 445 21251
443 462 60290
444 461 3240
444 450 63935
445 460 76987
445 461 11390
445 452 61033
445 451 19224
445 463 33523
445 461 19834
446 466 3753
446 450 89926
446 464 81545
446 460 90807
446 449 86450
447 455 39316
447 463 36397
447 463 94535
447 459 79902
447 456 12493
448 452 1288
448 466 85088
448 458 31071
449 452 10964
449 451 2034
449 464 14128
449 465 60632
449 454 12782
450 463 16313
450 451 27892
450 458 89297
450 455 63619
450 456 92145
451 456 58063
452 458 29703
452 459 20640
452 469 8095
452 460 20793
452 458 32617
453 458 62392
453 454 7181
453 465 55757
454 469 40861
454 466 10862
455 456 30810
455 458 23994
455 462 42633
455 468 78941
456 461 67030
456 459 24531
456 459 66394
456 460 42368
456 473 56506
456 469 29344
457 465 22384
457 464 56010
457 476 72350
457 460 25402
457 466 71232
457 475 56143
458 476 95361
458 462 51666
458 461 64289
459 463 39385
459 476 64821
459 468 32715
459 474 77039
459 475 42101
460 463 20848
460 464 43932
461 465 34859
461 480 13059
461 473 30620
461 469 57167
461 465 76313
462 467 49337
462 468 32760
462 479 30277
462 472 35409
462 463 43329
462 479 30677
463 472 43743
463 470 32015
463 472 74070
463 481 32378
463 465 25638
464 480 85075
465 473 99051
465 471 7536
465 477 21972
466 479 12289
466 473 52660
466 486 32726
466 476 89848
467 486 40450
467 486 55115
468 477 86686
468 478 13863
469 483 22241
469 488 48476
470 481 33742
470 475 50662
470 476 31375
470 488 72698
470 478 81729
470 490 14160
470 482 30351
471 482 81528
471 475 26371
471 480 17222
471 488 61643
471 483 86128
471 485 34633
471 479 99978
472 480 41949
472 477 7023
472 473 22822
472 480 17555
474 489 65415
474 491 84215
474 487 25367
474 476 54893
474 489 42235
474 493 61164
474 480 37272
475 493 45430
475 481 14532
475 479 15416
475 489 53228
475 486 13041
475 487 55916
476 495 64615
476 479 68890
476 492 19991
476 495 89395
477 495 28091
477 487 66474
477 489 83217
477 491 48526
478 483 77225
478 496 98700
478 484 31551
478 487 20741
478 497 64603
479 481 95346
479 487 50197
479 497 24996
479 488 93196
479 492 60912
479 495 6760
479 496 8828
480 499 59246
480 483 72771
481 487 56069
481 486 64815
481 491 42407
481 500 62552
482 494 96090
482 489 29671
482 492 35744
482 500 66320
482 489 72635
482 492 77824
483 500 13445
483 493 94786
483 493 1775
483 495 99769
483 486 32408
483 499 41673
483 497 49969
483 485 48310
483 489 49838
484 487 28722
484 485 76367
484 500 14033
484 487 65168
484 486 12880
484 499 54974
485 486 49109
485 489 62012
485 498 10568
486 490 34068
486 489 11586
486 489 2737
487 492 34757
487 488 76556
488 500 8460
488 500 52454
488 498 80196
489 490 20358
489 499 2620
489 499 45171
489 491 84217
489 496 28645
490 493 21341
490 493 57047
490 496 30776
491 492 85023
491 500 6960
492 496 45201
493 500 86061
496 497 15360
496 498 10792
496 498 96390
497 500 63493
498 500 85623</pre>



<div style="height:31px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 출력 D</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">135350</pre>



<div style="height:60px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">출처</h2>



<p><a href="https://www.acmicpc.net/category/2">Olympiad</a>&nbsp;&gt;&nbsp;<a href="https://www.acmicpc.net/category/106">USA Computing Olympiad</a>&nbsp;&gt;&nbsp;<a href="https://www.acmicpc.net/category/137">2008-2009 Season</a>&nbsp;&gt;&nbsp;<a href="https://www.acmicpc.net/category/142">USACO February 2009 Contest</a>&nbsp;&gt;&nbsp;<a href="https://www.acmicpc.net/category/detail/661">Gold</a>&nbsp;3번</p>



<ul class="wp-block-list">
<li>문제를 번역한 사람:&nbsp;<a href="https://www.acmicpc.net/user/author6" target="_blank" rel="noreferrer noopener">author6</a></li>



<li>데이터를 추가한 사람:&nbsp;<a href="https://www.acmicpc.net/user/djm03178" target="_blank" rel="noreferrer noopener">djm03178</a>,&nbsp;<a href="https://www.acmicpc.net/user/sait2000" target="_blank" rel="noreferrer noopener">sait2000</a></li>



<li>빠진 조건을 찾은 사람:&nbsp;<a href="https://www.acmicpc.net/user/jongseo_park" target="_blank" rel="noreferrer noopener">jongseo_park</a>,&nbsp;<a href="https://www.acmicpc.net/user/kdk8361">kd</a><a href="https://www.acmicpc.net/user/kdk8361" target="_blank" rel="noreferrer noopener">k</a><a href="https://www.acmicpc.net/user/kdk8361">8361</a></li>
</ul>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">알고리즘 분류</h2>



<ul class="wp-block-list">
<li><a href="https://www.acmicpc.net/problem/tag/25" target="_blank" rel="noreferrer noopener">다이나믹 프로그래밍</a></li>



<li><a href="https://www.acmicpc.net/problem/tag/7" target="_blank" rel="noreferrer noopener">그래프 이론</a></li>



<li><a href="https://www.acmicpc.net/problem/tag/22" target="_blank" rel="noreferrer noopener">데이크스트라</a></li>
</ul>



<hr class="wp-block-separator has-alpha-channel-opacity is-style-wide" style="margin-top:var(--wp--preset--spacing--80);margin-bottom:var(--wp--preset--spacing--80)"/>



<h1 class="wp-block-heading">해결방법</h1>



<div style="height:35px" aria-hidden="true" class="wp-block-spacer"></div>



<figure class="wp-block-image size-full is-resized"><img decoding="async" src="https://lycos7560.com/wp-content/uploads/2023/02/KakaoTalk_20230202_161434320-1.jpg" alt="" class="wp-image-5965" width="1289" height="2441" srcset="https://lycos7560.com/wp-content/uploads/2023/02/KakaoTalk_20230202_161434320-1.jpg 1014w, https://lycos7560.com/wp-content/uploads/2023/02/KakaoTalk_20230202_161434320-1-158x300.jpg 158w, https://lycos7560.com/wp-content/uploads/2023/02/KakaoTalk_20230202_161434320-1-768x1454.jpg 768w, https://lycos7560.com/wp-content/uploads/2023/02/KakaoTalk_20230202_161434320-1-811x1536.jpg 811w" sizes="(max-width: 1289px) 100vw, 1289px" /></figure>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h1 class="wp-block-heading">통과된 코드</h1>



<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>



<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">#include &lt;iostream>
#include &lt;vector>
#include &lt;queue>
#include &lt;tuple>

using namespace std;

constexpr long long int INF = INT64_MAX;

long long int disArr[10001][21];

priority_queue&lt;tuple&lt;long long int, int, int>> myPQ;

vector&lt;pair&lt;int, int>> graph[50000];

int N, M, K, U, V, dist, cnt;

int main()
{
	ios_base::sync_with_stdio(false); // scanf와 동기화를 비활성화
	// cin.tie(null); 코드는 cin과 cout의 묶음을 풀어줍니다.
	cin.tie(NULL);
	cout.tie(NULL);

	cin >> N >> M >> K;

	for (int i = 0; i &lt; M; i++) {

		cin >> U >> V >> dist;
        // 양방향 도로
		graph[U].push_back(make_pair(V, dist));

        graph[V].push_back(make_pair(U, dist));

	}

    // 다익스트라 배열 초기화
	for (int i = 1; i &lt;= N; i++)
        for (int j = 0; j &lt;= 20; j++)
            disArr[i][j] = INF;


	// 우선순위 큐에 삽입.
	myPQ.push({ 0, 1, 0 }); // &lt; first : 거리 , second : 노드 인덱스 >

	disArr[1][0] = 0;

    while (!myPQ.empty()) {
        // -를 붙이는 이유는 우선순위 큐를 이용하여 정렬하기 위함이다.
        // (최소힙으로 구현)
        long long int nCost = -get&lt;0>(myPQ.top());

        int now = get&lt;1>(myPQ.top());

        int cnt = get&lt;2>(myPQ.top()); // 포장개수

        myPQ.pop();
        // 이미 담겨 있는 것보다 거리가 크면 넘어간다.
        if (disArr[now][cnt] &lt; nCost) continue;
        // 해당 노드에서 연결된 모든 경로를 확인
        for (int i = 0; i &lt; graph[now].size(); i++) {

            // disSum = 임시 노드 + 현재 노드에서 i로가는 비용
            long long int disSum = nCost + graph[now][i].second;

            // 비용이 작다면 최단경로 테이블 값을 갱신.
            // 도로가 포장을 안했을 경우
            if (disSum &lt; disArr[graph[now][i].first][cnt]) {
                // 임시 노드 업데이트
                disArr[graph[now][i].first][cnt] = disSum;
                // 우선순위 큐에 (거리, 노드 인덱스) 푸시
                myPQ.push({ -disSum, graph[now][i].first, cnt});

            }

            // 비용이 작다면 최단경로 테이블 값을 갱신.
            // 도로가 포장을 했을 경우 이동값은 0
            // 포장할 수 있는 도로의 개수를 넘으면 안된다.
            if (nCost &lt; disArr[graph[now][i].first][cnt + 1] &amp;&amp; cnt &lt; K) {
                // 임시 노드 업데이트
                disArr[graph[now][i].first][cnt + 1] = nCost;

                myPQ.push({ -nCost, graph[now][i].first, cnt + 1 });

            }
        }
    }

    long long int result = INF;

    for (int j = 0; j &lt;= K; j++) result = min(disArr[N][j], result);
    
    cout &lt;&lt; result;
  
	return 0;
}

</pre>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<figure class="wp-block-image size-full"><img decoding="async" width="1041" height="237" src="https://lycos7560.com/wp-content/uploads/2023/02/image-32.png" alt="" class="wp-image-5966" srcset="https://lycos7560.com/wp-content/uploads/2023/02/image-32.png 1041w, https://lycos7560.com/wp-content/uploads/2023/02/image-32-300x68.png 300w, https://lycos7560.com/wp-content/uploads/2023/02/image-32-768x175.png 768w" sizes="(max-width: 1041px) 100vw, 1041px" /></figure>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-5698326622209671" crossorigin="anonymous"></script>
<ins class="adsbygoogle" style="display:block" data-ad-format="autorelaxed" data-ad-client="ca-pub-5698326622209671" data-ad-slot="4245812909"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>



<p></p>
<p>The post <a href="https://lycos7560.com/cpp/%eb%b0%b1%ec%a4%80-1162%eb%b2%88-%eb%8f%84%eb%a1%9c%ed%8f%ac%ec%9e%a5-c-dijkstra-%ec%b6%94%ea%b0%80-%eb%b0%98%eb%a1%80-baekjoon/5955/">백준 1162번 (도로포장, C++, Dijkstra) / 추가 반례 [BAEKJOON]</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://lycos7560.com/cpp/%eb%b0%b1%ec%a4%80-1162%eb%b2%88-%eb%8f%84%eb%a1%9c%ed%8f%ac%ec%9e%a5-c-dijkstra-%ec%b6%94%ea%b0%80-%eb%b0%98%eb%a1%80-baekjoon/5955/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>백준 16681번 (등산, C++, Dijkstra) [BAEKJOON]</title>
		<link>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-16681%eb%b2%88-%eb%93%b1%ec%82%b0-c-dijkstra-baekjoon/5559/</link>
					<comments>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-16681%eb%b2%88-%eb%93%b1%ec%82%b0-c-dijkstra-baekjoon/5559/#respond</comments>
		
		<dc:creator><![CDATA[lycos7560]]></dc:creator>
		<pubDate>Wed, 25 Jan 2023 18:14:49 +0000</pubDate>
				<category><![CDATA[BaekjoonOnlineJudge]]></category>
		<category><![CDATA[C++/CPP]]></category>
		<category><![CDATA[16681]]></category>
		<category><![CDATA[16681번]]></category>
		<category><![CDATA[algorithm]]></category>
		<category><![CDATA[Baekjoon]]></category>
		<category><![CDATA[BFS]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[cin]]></category>
		<category><![CDATA[cin.tie(NULL)]]></category>
		<category><![CDATA[cout]]></category>
		<category><![CDATA[cout.tie(NULL)]]></category>
		<category><![CDATA[cpp]]></category>
		<category><![CDATA[Dijkstra]]></category>
		<category><![CDATA[DP]]></category>
		<category><![CDATA[DynamicProgramming]]></category>
		<category><![CDATA[ios_base::sync_with_stdio(false)]]></category>
		<category><![CDATA[ios_base::sync_with_stdio(false);]]></category>
		<category><![CDATA[long long int]]></category>
		<category><![CDATA[map]]></category>
		<category><![CDATA[multimap]]></category>
		<category><![CDATA[mutimap]]></category>
		<category><![CDATA[priority_queue]]></category>
		<category><![CDATA[PriorityQueue]]></category>
		<category><![CDATA[Queue]]></category>
		<category><![CDATA[study]]></category>
		<category><![CDATA[고려대]]></category>
		<category><![CDATA[고려대학교]]></category>
		<category><![CDATA[공부]]></category>
		<category><![CDATA[그래프 이론]]></category>
		<category><![CDATA[그래프 탐색]]></category>
		<category><![CDATA[기본]]></category>
		<category><![CDATA[기초]]></category>
		<category><![CDATA[길찾기]]></category>
		<category><![CDATA[너비]]></category>
		<category><![CDATA[너비 우선 탐색]]></category>
		<category><![CDATA[다이나믹 프로그래밍]]></category>
		<category><![CDATA[다익스트라]]></category>
		<category><![CDATA[데이크스트라]]></category>
		<category><![CDATA[등산]]></category>
		<category><![CDATA[반례]]></category>
		<category><![CDATA[백준]]></category>
		<category><![CDATA[백준 16681]]></category>
		<category><![CDATA[백준 16681번]]></category>
		<category><![CDATA[성취감]]></category>
		<category><![CDATA[시간]]></category>
		<category><![CDATA[알고리즘]]></category>
		<category><![CDATA[예제]]></category>
		<category><![CDATA[지름길]]></category>
		<category><![CDATA[체력]]></category>
		<category><![CDATA[추가 반례]]></category>
		<category><![CDATA[추가 예제]]></category>
		<category><![CDATA[추가반례]]></category>
		<category><![CDATA[추가예제]]></category>
		<category><![CDATA[컴파일 에러]]></category>
		<category><![CDATA[코딩테스트]]></category>
		<category><![CDATA[코테]]></category>
		<category><![CDATA[틀렸습니다]]></category>
		<guid isPermaLink="false">https://lycos7560.com/?p=5559</guid>

					<description><![CDATA[<p>백준(BAEKJOON) 16681번 '등산' 문제에 대한 글입니다. Dijkstra 알고리즘을 이용하여 해결하였습니다. (This is an article about the 'climbing' problem in BAEKJOON No. 16681. It was solved using Dijkstra algorithm.)</p>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-16681%eb%b2%88-%eb%93%b1%ec%82%b0-c-dijkstra-baekjoon/5559/">백준 16681번 (등산, C++, Dijkstra) [BAEKJOON]</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-5698326622209671" crossorigin="anonymous"></script>
<!-- HorizontalAD -->
<ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-5698326622209671" data-ad-slot="6908948342" data-ad-format="auto" data-full-width-responsive="true"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>



<div style="height:62px" aria-hidden="true" class="wp-block-spacer"></div>



<h1 class="wp-block-heading">등산</h1>



<p class="has-medium-font-size"><a href="https://www.acmicpc.net/problem/16681">https://www.acmicpc.n</a><a href="https://www.acmicpc.net/problem/16681" target="_blank" rel="noreferrer noopener">et/problem/16681</a></p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<figure id="problem-info" class="wp-block-table"><table class="has-fixed-layout"><thead><tr><th class="has-text-align-left" data-align="left">시간 제한</th><th class="has-text-align-left" data-align="left">메모리 제한</th><th class="has-text-align-left" data-align="left">제출</th><th class="has-text-align-left" data-align="left">정답</th><th class="has-text-align-left" data-align="left">맞힌 사람</th><th class="has-text-align-left" data-align="left">정답 비율</th></tr></thead><tbody><tr><td class="has-text-align-left" data-align="left">1 초</td><td class="has-text-align-left" data-align="left">256 MB</td><td class="has-text-align-left" data-align="left">3180</td><td class="has-text-align-left" data-align="left">827</td><td class="has-text-align-left" data-align="left">568</td><td class="has-text-align-left" data-align="left">22.576%</td></tr></tbody></table></figure>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">문제</h2>



<p class="has-medium-font-size">주환이는 요즘 등산에 빠졌다. 주환이는 등산을 위해 지도를 가지고 있는데, </p>



<p class="has-medium-font-size">그 지도에는 각 지점의 높이와 갈 수 있는 다른 지점까지의 거리가 표시되어 있다.</p>



<p class="has-medium-font-size">주환이는 아침에 집에서 출발하여 등산을 갔다가, 오후 수업을 듣기 위해 고려대학교로 돌아와야 한다.</p>



<p class="has-medium-font-size">1. 주환이는 지도의 임의의 지점을 골라, 그 지점을 목표로 정한다. 집 또는 고려대학교는 목표로 선택할 수 없다.</p>



<p class="has-medium-font-size">2. 주환이가 집에서 정한 목표에 도달할 때 까지는 항상 높이가 증가하는 방향으로만 이동해야 한다.</p>



<p class="has-medium-font-size">3. 주환이가 정한 목표에 도달한 후, 고려대학교로 갈 때에는 항상 높이가 감소하는 방향으로만 이동해야 한다.</p>



<p class="has-medium-font-size">4. 주환이는 거리 1을 움직일 때 마다&nbsp;<em>D&nbsp;</em>의 체력이 소모된다.</p>



<p class="has-medium-font-size">5. 주환이는 정한 목표에 도달하면 높이 1당&nbsp;<em>E</em>&nbsp;의 성취감을 얻는다. 즉 높이가&nbsp;<em>h</em>인 목표에 도달하면&nbsp;<em>hE</em>의 성취감을 얻는다.</p>



<p class="has-medium-font-size">주환이는 이 등산의 가치를 (얻은 성취감) &#8211; (소모한 체력) 으로 계산하기로 하였다. </p>



<p class="has-medium-font-size">주환이를 위해 가치가 가장 높은 등산 경로를 선택해주자.</p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">입력</h2>



<p class="has-medium-font-size">첫 번째 줄에 지도에 표시된 지점의 개수, 지점을 잇는 경로의 개수, 주환이의 거리 비례 체력 소모량, </p>



<p class="has-medium-font-size">높이 비례 성취감 획득량을 나타내는 정수&nbsp;<em>N</em>,&nbsp;<em>M</em>,&nbsp;<em>D</em>,&nbsp;<em>E</em>가 공백을 사이에 두고 주어진다. </p>



<p class="has-medium-font-size">(2 ≤&nbsp;<em>N</em>&nbsp;≤ 100,000, 1 ≤&nbsp;<em>M</em>&nbsp;≤ 200,000, 1&nbsp;≤&nbsp;<em>D</em>&nbsp;≤ 100, 1 ≤&nbsp;&nbsp;<em>E&nbsp;</em>≤ 100)</p>



<p class="has-medium-font-size">두 번째 줄에&nbsp;<em>N</em>개의 정수&nbsp;<em>h<sub>1</sub>, &#8230;&nbsp; ,h<sub>N</sub></em>이 공백으로 구분되어 주어진다.&nbsp;<em>h<sub>i</sub></em>는&nbsp;<em>i&nbsp;</em>번째 지점의 높이를 의미한다. </p>



<p class="has-medium-font-size">(1 ≤&nbsp;<em>h<sub>i</sub></em>&nbsp;≤ 1,000,000, 1 ≤&nbsp;<em>i&nbsp;</em>≤&nbsp;<em>N</em>)</p>



<p class="has-medium-font-size">세 번째 줄부터&nbsp;<em>M</em>개의 줄에 걸쳐 세 정수&nbsp;<em>a, b, n</em>이 공백으로 구분되어 주어진다. </p>



<p class="has-medium-font-size">이는&nbsp;<em>a</em>번 지점과&nbsp;<em>b</em>번 지점을 잇는 거리&nbsp;<em>n</em>의 양방향 경로가 있음을 의미한다. (1 ≤&nbsp;<em>a, b</em>&nbsp;≤&nbsp;<em>N</em>, 1 ≤&nbsp;<em>n</em>&nbsp;≤ 100,000)</p>



<p class="has-medium-font-size">어떤 지점에서 다른 지점으로 가는 경로가 여러 개 있을 수도 있으며 (등산로는 여러 개가 있을 수 있다), </p>



<p class="has-medium-font-size">한 지점에서 출발해 그 지점으로 돌아가는 경로가 있을 수도 있다 (쉼터에서 몇 바퀴 돌며 쉴 수도 있다).</p>



<p class="has-medium-font-size">주환이의 집은 1번 지점에 위치하고, 고려대학교는&nbsp;<em>N</em>번 지점에 위치하며 주환이의 집과 고려대학교의 높이는 1임이 보장된다.</p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">출력</h2>



<p class="has-medium-font-size">첫 번째 줄에 주환이가 얻을 수 있는 가치의 최댓값을 출력한다. </p>



<p class="has-medium-font-size">만약 조건을 만족하는 등산 경로를 선택할 수 없다면, &#8220;<code>Impossible</code>&#8220;을 쌍따옴표를 제외하고 출력한다. </p>



<p class="has-medium-font-size">답이 음수일 수 있음에 유의하여라.</p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 입력 1</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">8 13 4 9
1 4 7 3 10 2 15 1
1 2 3
3 4 2
5 6 6
7 8 2
2 3 4
6 7 2
3 6 1
4 8 3
5 1 6
8 3 5
2 5 4
4 6 3
5 3 8</pre>



<div style="height:31px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 출력 1</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">15</pre>



<div style="height:60px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 입력 2</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">3 2 1 1
1 1 1
1 2 5
2 3 5</pre>



<div style="height:31px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 출력 2</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">Impossible</pre>



<div style="height:60px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">출처</h2>



<p><a href="https://www.acmicpc.net/category/5" target="_blank" rel="noreferrer noopener">University</a>&nbsp;&gt;&nbsp;<a href="https://www.acmicpc.net/category/341" target="_blank" rel="noreferrer noopener">고려대학교</a>&nbsp;&gt;&nbsp;<a href="https://www.acmicpc.net/category/698" target="_blank" rel="noreferrer noopener">2018 고려대학교 프로그래밍 경시대회 (KCPC)</a>&nbsp;&gt;&nbsp;<a href="https://www.acmicpc.net/category/detail/1974" target="_blank" rel="noreferrer noopener">Advanced Div.</a>&nbsp;C번</p>



<p><a href="https://www.acmicpc.net/category/5" target="_blank" rel="noreferrer noopener">University</a>&nbsp;&gt;&nbsp;<a href="https://www.acmicpc.net/category/341" target="_blank" rel="noreferrer noopener">고려대학교</a>&nbsp;&gt;&nbsp;<a href="https://www.acmicpc.net/category/698" target="_blank" rel="noreferrer noopener">2018 고려대학교 프로그래밍 경시대회 (KCPC)</a>&nbsp;&gt;&nbsp;<a href="https://www.acmicpc.net/category/detail/1975" target="_blank" rel="noreferrer noopener">Intermediate Div.</a>&nbsp;E번</p>



<ul class="wp-block-list">
<li>문제를 만든 사람:&nbsp;<a href="https://www.acmicpc.net/user/oree2113">oree2113</a></li>
</ul>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">알고리즘 분류</h2>



<ul class="wp-block-list">
<li><a href="https://www.acmicpc.net/problem/tag/7" target="_blank" rel="noreferrer noopener">그래프 이론</a></li>



<li><a href="https://www.acmicpc.net/problem/tag/22" target="_blank" rel="noreferrer noopener">데이크스트라</a></li>
</ul>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<hr class="wp-block-separator has-alpha-channel-opacity is-style-wide" style="margin-top:var(--wp--preset--spacing--80);margin-bottom:var(--wp--preset--spacing--80)"/>



<figure class="wp-block-image size-large"><img decoding="async" width="1421" height="3000" src="https://lycos7560.com/wp-content/uploads/KakaoTalk_20230126_031127765-1421x3000.jpg" alt="" class="wp-image-5568" srcset="https://lycos7560.com/wp-content/uploads/KakaoTalk_20230126_031127765-1421x3000.jpg 1421w, https://lycos7560.com/wp-content/uploads/KakaoTalk_20230126_031127765-142x300.jpg 142w, https://lycos7560.com/wp-content/uploads/KakaoTalk_20230126_031127765-768x1621.jpg 768w, https://lycos7560.com/wp-content/uploads/KakaoTalk_20230126_031127765-728x1536.jpg 728w, https://lycos7560.com/wp-content/uploads/KakaoTalk_20230126_031127765-970x2048.jpg 970w, https://lycos7560.com/wp-content/uploads/KakaoTalk_20230126_031127765-scaled.jpg 910w" sizes="(max-width: 1421px) 100vw, 1421px" /></figure>



<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">통과된 코드</h2>



<p class="has-medium-font-size"><strong>자료형 long long 필수!</strong></p>



<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>



<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">#include &lt;iostream>
#include &lt;queue>
#include &lt;vector>

using namespace std;

constexpr int MAXN = 100001;
constexpr long long int INF = INT64_MAX;
constexpr long long int uINF = INT64_MIN;

///*
//각 노드에 연결되어 있는 노드에 대한 정보를 담는 벡터
//a번 노드에서 b번 노드로 가는 비용이 c라는 의미
//graph[a].push_back((make_pair(B, C));
//*/
vector&lt;pair&lt;int, int>> upGraph[MAXN];
vector&lt;pair&lt;int, int>> downGraph[MAXN];

// Dijkstra 알고리즘에 사용할 우선순위 큐
priority_queue&lt;pair&lt;long long int, int>> myPQ;

// N 지점의 개수, M 지점을 잇는 경로의 개수
// D 주환이의 거리 비례 체력 소모량, E 높이 비례 성취감 획득량
int N, M, D, E;

// 각 지점의 높이를 저장하는 배열
int hArr[MAXN];

// 등산을 하는데 필요한 배열 (집 -> 산)
long long int upArr[MAXN];

long long int temp = 0;
// 결과를 저장
long long int result = uINF;

// 하산을 하는데 필요한 배열 (산 -> 고려대)
long long int downArr[MAXN];

// V1,2 지점, d : 지점 사이의 거리
int V1, V2, d;

void Dijkstra(int start, long long int arr[], vector&lt;pair&lt;int, int>> graph[])
{
    // 임시배열 초기화
    for (int i = 1; i &lt;= N; i++) arr[i] = INF;
    // 우선순위 큐에 삽입.
    myPQ.push({ 0, start }); // &lt; first : 거리 , second : 노드 인덱스 >
    arr[start] = 0;
    while (!myPQ.empty()) {
        // -를 붙이는 이유는 우선순위 큐를 이용하여 정렬하기 위함이다.
        // (최소힙으로 구현)
        long long int nCost = -myPQ.top().first;
        int now = myPQ.top().second;
        myPQ.pop();
        // 이미 담겨 있는 것보다 거리가 크면 넘어간다.
        if (arr[now] &lt; nCost) continue;
        // 해당 노드에서 연결된 모든 경로를 확인
        for (int i = 0; i &lt; graph[now].size(); i++) {
            // disSum = 임시 노드 + 현재 노드에서 i로가는 비용
            long long int disSum = nCost + graph[now][i].second;
            // 비용이 더 작다면 최단경로 테이블 값을 갱신.
            if (disSum &lt; arr[graph[now][i].first]) {
                // 임시 노드 업데이트
                arr[graph[now][i].first] = disSum;
                // 우선순위 큐에 (거리, 노드 인덱스) 푸시
                myPQ.push(make_pair(-disSum, graph[now][i].first));
            }
        }
    }
}


int main()
{
    ios_base::sync_with_stdio(false); // scanf와 동기화를 비활성화
    // cin.tie(null); 코드는 cin과 cout의 묶음을 풀어줍니다.
    cin.tie(NULL);
    cout.tie(NULL);

    // 문제의 조건들을 입력 받는다.
    cin >> N >> M >> D >> E;

    // 각 지점의 높이를 입력 받는다.
    for (int i = 1; i &lt;= N; i++) cin >> hArr[i];

    // 경로를 입력 받는다.
    for (int i = 0; i &lt; M; i++) {

        cin >> V1 >> V2 >> d;

        // 높이가 같을 경우에는 생각하지 않는다.
        if (hArr[V1] > hArr[V2]) { // V1의 높이가 더 높을 경우
            upGraph[V2].push_back(make_pair(V1, d));
            downGraph[V2].push_back(make_pair(V1, d));
        }
        else if (hArr[V1] &lt; hArr[V2]) { // V1의 높이가 더 낮은 경우
            upGraph[V1].push_back(make_pair(V2, d));
            downGraph[V1].push_back(make_pair(V2, d));
        }

    }

    Dijkstra(1, upArr, upGraph);
    Dijkstra(N, downArr, downGraph);

    for (int i = 1; i &lt;= N; i++) {
        if (upArr[i] == INF || downArr[i] == INF) continue;
        temp = upArr[i] + downArr[i];
        result = max(result, hArr[i] * E - temp * D);
    }

    if (result == uINF) cout &lt;&lt; "Impossible";
    else cout &lt;&lt; result;
    
    return 0;
}</pre>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<p class="has-medium-font-size">최악의 경우 예를 들어 N = 100,000 / M = 200,000  / <em>h<sub>i</sub></em> = i / 모든 경로 거리 1 / D = 1 / E = 100 이런 식이면</p>



<p class="has-medium-font-size">int 를 사용하면 터진다. ㅠㅠ</p>



<p class="has-medium-font-size">long long int 로 바꾸어주자</p>



<p class="has-medium-font-size">로직 문제로 오인해서 많이 틀렸다.</p>



<div style="height:41px" aria-hidden="true" class="wp-block-spacer"></div>



<figure class="wp-block-image size-full"><img decoding="async" width="1038" height="321" src="https://lycos7560.com/wp-content/uploads/image-1060.png" alt="" class="wp-image-5560" srcset="https://lycos7560.com/wp-content/uploads/image-1060.png 1038w, https://lycos7560.com/wp-content/uploads/image-1060-300x93.png 300w, https://lycos7560.com/wp-content/uploads/image-1060-768x238.png 768w" sizes="(max-width: 1038px) 100vw, 1038px" /></figure>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-5698326622209671" crossorigin="anonymous"></script>
<ins class="adsbygoogle" style="display:block" data-ad-format="autorelaxed" data-ad-client="ca-pub-5698326622209671" data-ad-slot="4245812909"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-16681%eb%b2%88-%eb%93%b1%ec%82%b0-c-dijkstra-baekjoon/5559/">백준 16681번 (등산, C++, Dijkstra) [BAEKJOON]</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-16681%eb%b2%88-%eb%93%b1%ec%82%b0-c-dijkstra-baekjoon/5559/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>백준 2211번 (네트워크 복구, C++, Dijkstra) / 추가 반례 [BAEKJOON]</title>
		<link>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-2211%eb%b2%88-%eb%84%a4%ed%8a%b8%ec%9b%8c%ed%81%ac-%eb%b3%b5%ea%b5%ac-c-dijkstra-%ec%b6%94%ea%b0%80-%eb%b0%98%eb%a1%80-baekjoon/5453/</link>
					<comments>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-2211%eb%b2%88-%eb%84%a4%ed%8a%b8%ec%9b%8c%ed%81%ac-%eb%b3%b5%ea%b5%ac-c-dijkstra-%ec%b6%94%ea%b0%80-%eb%b0%98%eb%a1%80-baekjoon/5453/#respond</comments>
		
		<dc:creator><![CDATA[lycos7560]]></dc:creator>
		<pubDate>Tue, 24 Jan 2023 14:13:16 +0000</pubDate>
				<category><![CDATA[BaekjoonOnlineJudge]]></category>
		<category><![CDATA[C++/CPP]]></category>
		<category><![CDATA[2211]]></category>
		<category><![CDATA[2211번]]></category>
		<category><![CDATA[algorithm]]></category>
		<category><![CDATA[Baekjoon]]></category>
		<category><![CDATA[BFS]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[cin.tie(NULL)]]></category>
		<category><![CDATA[cout.tie(NULL)]]></category>
		<category><![CDATA[cpp]]></category>
		<category><![CDATA[Dijkstra]]></category>
		<category><![CDATA[DP]]></category>
		<category><![CDATA[DynamicProgramming]]></category>
		<category><![CDATA[map]]></category>
		<category><![CDATA[multimap]]></category>
		<category><![CDATA[mutimap]]></category>
		<category><![CDATA[priority_queue]]></category>
		<category><![CDATA[PriorityQueue]]></category>
		<category><![CDATA[Queue]]></category>
		<category><![CDATA[study]]></category>
		<category><![CDATA[공부]]></category>
		<category><![CDATA[그래프 이론]]></category>
		<category><![CDATA[그래프 탐색]]></category>
		<category><![CDATA[기본]]></category>
		<category><![CDATA[기초]]></category>
		<category><![CDATA[길찾기]]></category>
		<category><![CDATA[너비]]></category>
		<category><![CDATA[너비 우선 탐색]]></category>
		<category><![CDATA[네트워크 복구]]></category>
		<category><![CDATA[다이나믹 프로그래밍]]></category>
		<category><![CDATA[다익스트라]]></category>
		<category><![CDATA[데이크스트라]]></category>
		<category><![CDATA[반례]]></category>
		<category><![CDATA[백준]]></category>
		<category><![CDATA[백준 2211]]></category>
		<category><![CDATA[백준 2211번]]></category>
		<category><![CDATA[시간]]></category>
		<category><![CDATA[알고리즘]]></category>
		<category><![CDATA[예제]]></category>
		<category><![CDATA[지름길]]></category>
		<category><![CDATA[추가 반례]]></category>
		<category><![CDATA[추가 예제]]></category>
		<category><![CDATA[추가반례]]></category>
		<category><![CDATA[추가예제]]></category>
		<category><![CDATA[컴파일 에러]]></category>
		<category><![CDATA[코딩테스트]]></category>
		<category><![CDATA[코테]]></category>
		<guid isPermaLink="false">https://lycos7560.com/?p=5453</guid>

					<description><![CDATA[<p>백준(BAEKJOON) 2211번 '네트워크 복구' 문제에 대한 글입니다. Dijkstra 알고리즘과 자료구조 'map' 을 이용하여 해결하였습니다. (This is an article on the 'Network Recovery' problem in BAEKJOON No. 2211. It was solved using Dijkstra algorithm and data structure 'map'.)</p>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-2211%eb%b2%88-%eb%84%a4%ed%8a%b8%ec%9b%8c%ed%81%ac-%eb%b3%b5%ea%b5%ac-c-dijkstra-%ec%b6%94%ea%b0%80-%eb%b0%98%eb%a1%80-baekjoon/5453/">백준 2211번 (네트워크 복구, C++, Dijkstra) / 추가 반례 [BAEKJOON]</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-5698326622209671" crossorigin="anonymous"></script>
<!-- HorizontalAD -->
<ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-5698326622209671" data-ad-slot="6908948342" data-ad-format="auto" data-full-width-responsive="true"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>



<div style="height:62px" aria-hidden="true" class="wp-block-spacer"></div>



<h1 class="wp-block-heading">네트워크 복구</h1>



<p class="has-medium-font-size"><a href="https://www.acmicpc.net/problem/2211" target="_blank" rel="noreferrer noopener">https://www.acmicpc.net/problem/2211</a></p>



<div style="height:30px" aria-hidden="true" class="wp-block-spacer"></div>



<figure id="problem-info" class="wp-block-table"><table class="has-fixed-layout"><thead><tr><th class="has-text-align-left" data-align="left">시간 제한</th><th class="has-text-align-left" data-align="left">메모리 제한</th><th class="has-text-align-left" data-align="left">제출</th><th class="has-text-align-left" data-align="left">정답</th><th class="has-text-align-left" data-align="left">맞힌 사람</th><th class="has-text-align-left" data-align="left">정답 비율</th></tr></thead><tbody><tr><td class="has-text-align-left" data-align="left">2 초</td><td class="has-text-align-left" data-align="left">192 MB</td><td class="has-text-align-left" data-align="left">5644</td><td class="has-text-align-left" data-align="left">2849</td><td class="has-text-align-left" data-align="left">1960</td><td class="has-text-align-left" data-align="left">48.951%</td></tr></tbody></table></figure>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">문제</h2>



<p class="has-medium-font-size">N(1 ≤ N ≤ 1,000)개의 컴퓨터로 구성된 네트워크가 있다. </p>



<p class="has-medium-font-size">이들 중 몇 개의 컴퓨터들은 서로 네트워크 연결이 되어 있어 서로 다른 두 컴퓨터 간 통신이 가능하도록 되어 있다. </p>



<p class="has-medium-font-size">통신을 할 때에는 서로 직접 연결되어 있는 회선을 이용할 수도 있으며, 회선과 다른 컴퓨터를 거쳐서 통신을 할 수도 있다.</p>



<p class="has-medium-font-size">각 컴퓨터들과 회선은 그 성능이 차이가 날 수 있다. </p>



<p class="has-medium-font-size">따라서 각각의 직접 연결되어 있는 회선을 이용해서 통신을 하는데 걸리는 시간이 서로 다를 수 있다. </p>



<p class="has-medium-font-size">심지어는 직접 연결되어 있는 회선이 오히려 더 느려서, 다른 컴퓨터를 통해서 통신을 하는 것이 더 유리할 수도 있다. </p>



<p class="has-medium-font-size">직접 연결되어 있는 회선을 사용할 경우에는 그 회선을 이용해서 통신을 하는 데 드는 시간만큼이 들게 된다. </p>



<p class="has-medium-font-size">여러 개의 회선을 거치는 경우에는 각 회선을 이용해서 통신을 하는 데 드는 시간의 합만큼의 시간이 걸리게 된다.</p>



<p class="has-medium-font-size">어느 날, 해커가 네트워크에 침입하였다. </p>



<p class="has-medium-font-size">네트워크의 관리자는 우선 모든 회선과 컴퓨터를 차단한 후, 해커의 공격을 막을 수 있었다.</p>



<p class="has-medium-font-size"> 관리자는 컴퓨터에 보안 시스템을 설치하려 하였는데, 버전 문제로 보안 시스템을 한 대의 슈퍼컴퓨터에만 설치할 수 있었다. </p>



<p class="has-medium-font-size">한 컴퓨터가 공격을 받게 되면, 네트워크를 통해 슈퍼컴퓨터에 이 사실이 전달이 되고, </p>



<p class="has-medium-font-size">그러면 슈퍼컴퓨터에서는 네트워크를 이용해서 보안 패킷을 전송하는 방식을 사용하기로 하였다. </p>



<p class="has-medium-font-size">준비를 마친 뒤, 관리자는 다시 네트워크를 복구하기로 하였다. 이때, 다음의 조건들이 만족되어야 한다.</p>



<p class="has-medium-font-size">1. 해커가 다시 공격을 할 우려가 있기 때문에, 최소 개수의 회선만을 복구해야 한다. </p>



<p class="has-medium-font-size">물론, 그렇다면 아무 회선도 복구하지 않으면 되겠지만, 이럴 경우 네트워크의 사용에 지장이 생기게 된다. </p>



<p class="has-medium-font-size">따라서 네트워크를 복구한 후에 서로 다른 두 컴퓨터 간에 통신이 가능하도록 복구해야 한다.</p>



<p class="has-medium-font-size">2. 네트워크를 복구해서 통신이 가능하도록 만드는 것도 중요하지만, </p>



<p class="has-medium-font-size">해커에게 공격을 받았을 때 보안 패킷을 전송하는 데 걸리는 시간도 중요한 문제가 된다. </p>



<p class="has-medium-font-size">따라서 슈퍼컴퓨터가 다른 컴퓨터들과 통신하는데 걸리는 최소 시간이, </p>



<p class="has-medium-font-size">원래의 네트워크에서 통신하는데 걸리는 최소 시간보다 커져서는 안 된다.</p>



<p class="has-medium-font-size">원래의 네트워크에 대한 정보가 주어졌을 때, </p>



<p class="has-medium-font-size">위의 조건을 만족하면서 네트워크를 복구하는 방법을 알아내는 프로그램을 작성하시오.</p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">입력</h2>



<p class="has-medium-font-size">첫째 줄에 두 정수 N, M이 주어진다. </p>



<p class="has-medium-font-size">다음 M개의 줄에는 회선의 정보를 나타내는 세 정수 A, B, C가 주어진다. </p>



<p class="has-medium-font-size">이는 A번 컴퓨터와 B번 컴퓨터가 통신 시간이 C (1 ≤ C ≤ 10)인 회선으로 연결되어 있다는 의미이다. </p>



<p class="has-medium-font-size">컴퓨터들의 번호는 1부터 N까지의 정수이며, 1번 컴퓨터는 보안 시스템을 설치할 슈퍼컴퓨터이다. </p>



<p class="has-medium-font-size">모든 통신은 완전쌍방향 방식으로 이루어지기 때문에, </p>



<p class="has-medium-font-size">한 회선으로 연결된 두 컴퓨터는 어느 방향으로도 통신할 수 있다.</p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">출력</h2>



<p class="has-medium-font-size">첫째 줄에 복구할 회선의 개수 K를 출력한다. </p>



<p class="has-medium-font-size">다음 K개의 줄에는 복구한 회선을 나타내는 두 정수 A, B를 출력한다. </p>



<p class="has-medium-font-size">이는 A번 컴퓨터와 B번 컴퓨터를 연결하던 회선을 복구한다는 의미이다. </p>



<p class="has-medium-font-size">출력은 임의의 순서대로 하며, 답이 여러 개 존재하는 경우에는 아무 것이나 하나만 출력하면 된다.</p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 입력 1</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">4 5
1 2 1
1 4 4
1 3 2
4 2 2
4 3 3</pre>



<div style="height:31px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 출력 1</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">3
1 2
3 1
4 2</pre>



<div style="height:60px" aria-hidden="true" class="wp-block-spacer"></div>



<p><a href="https://www.acmicpc.net/board/view/70830" target="_blank" rel="noreferrer noopener">https://www.acmicpc.net/board/view/70830</a> &lt;- 참조</p>



<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 입력 A</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">6 12
5 6 4 
4 3 3 
6 5 8 
5 1 4 
1 6 3 
6 2 6 
1 2 4 
3 2 8 
4 2 5 
5 3 5 
1 4 2 
2 4 8</pre>



<div style="height:31px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 출력 A</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">5
2 1
3 4
4 1
5 1
6 1</pre>



<div style="height:60px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 입력 B</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">8 14
2 4 1 
7 6 7 
1 5 7 
1 4 6 
5 3 9 
4 1 5 
7 8 3 
6 1 4 
4 2 3 
5 6 3 
7 5 9 
7 1 9 
2 1 1 
7 4 6</pre>



<div style="height:31px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 출력 B</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">7
2 1
3 5
4 2
5 1
6 1
7 4
8 7</pre>



<div style="height:60px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 입력 C</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">24 45
20 16 5 
24 7 9 
24 18 6 
4 2 7 
23 5 7 
8 19 5 
1 6 3 
9 8 7 
2 3 5 
11 12 4 
4 20 1 
2 16 5 
22 11 1 
17 10 4 
3 7 4 
18 10 9 
21 2 5 
20 18 3 
5 10 5 
15 2 4 
14 10 7 
4 15 9 
20 10 8 
14 12 8 
12 8 1 
23 14 8 
15 24 9 
12 5 1 
15 8 3 
21 15 7 
12 7 6 
18 12 5 
13 7 2 
7 10 3 
24 5 1 
15 10 7 
3 2 2 
22 5 6 
3 16 4 
1 12 1 
9 17 3 
17 12 7 
8 12 8 
11 15 7 
4 9 5</pre>



<div style="height:31px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 출력 C</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">23
2 15
3 7
4 20
5 12
6 1
7 12
8 12
9 8
10 5
11 12
12 1
13 7
14 12
15 8
16 20
17 12
18 12
19 8
20 18
21 15
22 11
23 5
24 5</pre>



<div style="height:60px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">출처</h2>



<ul class="wp-block-list">
<li>빠진 조건을 찾은 사람: <a href="https://www.acmicpc.net/user/his130" target="_blank" rel="noreferrer noopener">his130</a></li>



<li>어색한 표현을 찾은 사람: <a href="https://www.acmicpc.net/user/mwy3055" target="_blank" rel="noreferrer noopener">mwy3055</a></li>
</ul>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">알고리즘 분류</h2>



<ul class="wp-block-list">
<li><a href="https://www.acmicpc.net/problem/tag/7" target="_blank" rel="noreferrer noopener">그래프 이론</a></li>



<li><a href="https://www.acmicpc.net/problem/tag/22" target="_blank" rel="noreferrer noopener">데이크스트라</a></li>
</ul>



<hr class="wp-block-separator has-alpha-channel-opacity is-style-wide" style="margin-top:var(--wp--preset--spacing--80);margin-bottom:var(--wp--preset--spacing--80)"/>



<h2 class="wp-block-heading">풀이 과정</h2>



<figure class="wp-block-image size-full"><img decoding="async" width="1091" height="1920" src="https://lycos7560.com/wp-content/uploads/KakaoTalk_20230124_230823201-scaled.jpg" alt="" class="wp-image-5464" srcset="https://lycos7560.com/wp-content/uploads/KakaoTalk_20230124_230823201-scaled.jpg 1091w, https://lycos7560.com/wp-content/uploads/KakaoTalk_20230124_230823201-170x300.jpg 170w, https://lycos7560.com/wp-content/uploads/KakaoTalk_20230124_230823201-768x1352.jpg 768w, https://lycos7560.com/wp-content/uploads/KakaoTalk_20230124_230823201-873x1536.jpg 873w, https://lycos7560.com/wp-content/uploads/KakaoTalk_20230124_230823201-1164x2048.jpg 1164w" sizes="(max-width: 1091px) 100vw, 1091px" /></figure>



<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">통과된 코드</h2>



<p class="has-medium-font-size">map을 사용하는 생각이 바로 떠올라서 빠르게 해결</p>



<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>



<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">#include &lt;iostream>
#include &lt;vector>
#include &lt;queue>
#include &lt;map>

using namespace std;

// N(1 ≤ N ≤ 1,000)개의 컴퓨터
constexpr int MAXN = 1001;
constexpr int INF = INT32_MAX;

///*
//각 노드에 연결되어 있는 노드에 대한 정보를 담는 벡터
//a번 노드에서 b번 노드로 가는 비용이 c라는 의미
//graph[a].push_back((make_pair(B, C));
//*/
vector&lt;pair&lt;int, int>> graph[MAXN];

// Dijkstra 알고리즘에 사용할 우선순위 큐
priority_queue&lt;pair&lt;int, int>> myPQ;

// 문제의 결과를 출력하기 위한 map 
map&lt;int, int> myMap;

// dist[i][j]  
// => i 에서 j 까지의 최단거리 (임시 노드)
int timeArr[MAXN];

// dist[i][j]  
// => i 에서 j 까지의 최단거리 (임시 노드)
int cntArr[MAXN];

// N : 정점의 개수, M : 간선의 개수
// U: 현재 노드, V : 이웃 노드, t : 통신 시간
int N, M, U, V, t;


void Dijkstra(int start)
{
    // 임시배열 초기화
    for (int i = 1; i &lt;= N; i++) timeArr[i] = INF;
    // 우선순위 큐에 삽입.
    myPQ.push({ 0, start }); // &lt; first : 거리 , second : 노드 인덱스 >
    timeArr[start] = 0;

    while (!myPQ.empty()) {
        // -를 붙이는 이유는 우선순위 큐를 이용하여 정렬하기 위함이다.
        // (최소힙으로 구현)
        int nCost = -myPQ.top().first;
        int now = myPQ.top().second;
        myPQ.pop();

        // 이미 담겨 있는 것보다 거리가 크면 넘어간다.
        if (timeArr[now] &lt; nCost) continue;

        // 해당 노드에서 연결된 모든 경로를 확인
        for (int i = 0; i &lt; graph[now].size(); i++) {
            // 0이라면 길이 없다는 의미 continue
            if (graph[now][i].second == 0) continue;
            // disSum = 임시 노드 + 현재 노드에서 i로가는 비용
            int disSum = nCost + graph[now][i].second;
            // 비용이 더 작다면 최단경로 테이블 값을 갱신.
            if (disSum &lt; timeArr[graph[now][i].first]) {
                // 임시 노드 업데이트
                timeArr[graph[now][i].first] = disSum;
                // 우선순위 큐에 (거리, 노드 인덱스) 푸시
                myPQ.push(make_pair(-disSum, graph[now][i].first));

                // 만약 map의 key 값이 겹친다면 해당 값을 지운다.
                if (myMap.count(graph[now][i].first)) myMap.erase(graph[now][i].first);
                // 도착지를 Key 값으로 map에 저장
                myMap.insert(make_pair(graph[now][i].first, now));
            }
        }
    }
}

int main()
{
    ios_base::sync_with_stdio(false); // scanf와 동기화를 비활성화
    // cin.tie(null); 코드는 cin과 cout의 묶음을 풀어줍니다.
    cin.tie(NULL);
    cout.tie(NULL);

    cin >> N >> M;

    for (int i = 0; i &lt; M; i++) {
        cin >> U >> V >> t;
        // 양방향 통신 가능
        graph[U].push_back(make_pair(V, t));
        graph[V].push_back(make_pair(U, t)); 
    }

    Dijkstra(1);

    // map의 사이즈를 출력합니다.
    cout &lt;&lt; myMap.size() &lt;&lt; "\n";

    // 복구한 회선을 출력
    for (auto it = myMap.begin(); it != myMap.end(); it++) 
        cout &lt;&lt; it->first &lt;&lt; " " &lt;&lt; it->second &lt;&lt; "\n";

	return 0;
}
</pre>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<figure class="wp-block-image size-full"><img decoding="async" width="1045" height="105" src="https://lycos7560.com/wp-content/uploads/image-1050.png" alt="" class="wp-image-5459" srcset="https://lycos7560.com/wp-content/uploads/image-1050.png 1045w, https://lycos7560.com/wp-content/uploads/image-1050-300x30.png 300w, https://lycos7560.com/wp-content/uploads/image-1050-768x77.png 768w" sizes="(max-width: 1045px) 100vw, 1045px" /></figure>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-5698326622209671" crossorigin="anonymous"></script>
<ins class="adsbygoogle" style="display:block" data-ad-format="autorelaxed" data-ad-client="ca-pub-5698326622209671" data-ad-slot="4245812909"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-2211%eb%b2%88-%eb%84%a4%ed%8a%b8%ec%9b%8c%ed%81%ac-%eb%b3%b5%ea%b5%ac-c-dijkstra-%ec%b6%94%ea%b0%80-%eb%b0%98%eb%a1%80-baekjoon/5453/">백준 2211번 (네트워크 복구, C++, Dijkstra) / 추가 반례 [BAEKJOON]</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-2211%eb%b2%88-%eb%84%a4%ed%8a%b8%ec%9b%8c%ed%81%ac-%eb%b3%b5%ea%b5%ac-c-dijkstra-%ec%b6%94%ea%b0%80-%eb%b0%98%eb%a1%80-baekjoon/5453/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>백준 1916번 (최소비용 구하기, C++, Dijkstra) / 추가 반례 [BAEKJOON]</title>
		<link>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-1916%eb%b2%88-%ec%b5%9c%ec%86%8c%eb%b9%84%ec%9a%a9-%ea%b5%ac%ed%95%98%ea%b8%b0-c-dijkstra-%ec%b6%94%ea%b0%80-%eb%b0%98%eb%a1%80-baekjoon/5438/</link>
					<comments>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-1916%eb%b2%88-%ec%b5%9c%ec%86%8c%eb%b9%84%ec%9a%a9-%ea%b5%ac%ed%95%98%ea%b8%b0-c-dijkstra-%ec%b6%94%ea%b0%80-%eb%b0%98%eb%a1%80-baekjoon/5438/#respond</comments>
		
		<dc:creator><![CDATA[lycos7560]]></dc:creator>
		<pubDate>Tue, 24 Jan 2023 10:53:05 +0000</pubDate>
				<category><![CDATA[BaekjoonOnlineJudge]]></category>
		<category><![CDATA[C++/CPP]]></category>
		<category><![CDATA[1916]]></category>
		<category><![CDATA[1916번]]></category>
		<category><![CDATA[algorithm]]></category>
		<category><![CDATA[Baekjoon]]></category>
		<category><![CDATA[BFS]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[cin.tie(NULL)]]></category>
		<category><![CDATA[cout.tie(NULL)]]></category>
		<category><![CDATA[cpp]]></category>
		<category><![CDATA[Dijkstra]]></category>
		<category><![CDATA[DP]]></category>
		<category><![CDATA[DynamicProgramming]]></category>
		<category><![CDATA[priority_queue]]></category>
		<category><![CDATA[PriorityQueue]]></category>
		<category><![CDATA[Queue]]></category>
		<category><![CDATA[study]]></category>
		<category><![CDATA[공부]]></category>
		<category><![CDATA[그래프 이론]]></category>
		<category><![CDATA[그래프 탐색]]></category>
		<category><![CDATA[기본]]></category>
		<category><![CDATA[기초]]></category>
		<category><![CDATA[길찾기]]></category>
		<category><![CDATA[너비]]></category>
		<category><![CDATA[너비 우선 탐색]]></category>
		<category><![CDATA[다이나믹 프로그래밍]]></category>
		<category><![CDATA[다익스트라]]></category>
		<category><![CDATA[데이크스트라]]></category>
		<category><![CDATA[반례]]></category>
		<category><![CDATA[백준]]></category>
		<category><![CDATA[백준 1916]]></category>
		<category><![CDATA[백준 1916번]]></category>
		<category><![CDATA[시간]]></category>
		<category><![CDATA[시간 초과]]></category>
		<category><![CDATA[시간초과]]></category>
		<category><![CDATA[알고리즘]]></category>
		<category><![CDATA[예제]]></category>
		<category><![CDATA[최소비용 구하기]]></category>
		<category><![CDATA[추가 반례]]></category>
		<category><![CDATA[추가 예제]]></category>
		<category><![CDATA[추가반례]]></category>
		<category><![CDATA[추가예제]]></category>
		<category><![CDATA[컴파일 에러]]></category>
		<category><![CDATA[코딩테스트]]></category>
		<category><![CDATA[코테]]></category>
		<guid isPermaLink="false">https://lycos7560.com/?p=5438</guid>

					<description><![CDATA[<p>백준(BAEKJOON) 1916번 '최소비용 구하기'에대한 글입니다. Dijkstra 알고리즘을 이용하여 해결하였습니다.  (This is BAEKJOON's 1916 article about "Getting the Minimum Cost". It was solved using Dijkstra algorithm.)</p>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-1916%eb%b2%88-%ec%b5%9c%ec%86%8c%eb%b9%84%ec%9a%a9-%ea%b5%ac%ed%95%98%ea%b8%b0-c-dijkstra-%ec%b6%94%ea%b0%80-%eb%b0%98%eb%a1%80-baekjoon/5438/">백준 1916번 (최소비용 구하기, C++, Dijkstra) / 추가 반례 [BAEKJOON]</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-5698326622209671" crossorigin="anonymous"></script>
<!-- HorizontalAD -->
<ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-5698326622209671" data-ad-slot="6908948342" data-ad-format="auto" data-full-width-responsive="true"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>



<div style="height:62px" aria-hidden="true" class="wp-block-spacer"></div>



<h1 class="wp-block-heading">최소비용 구하기</h1>



<p class="has-medium-font-size"><a href="https://www.acmicpc.net/problem/1916" target="_blank" rel="noreferrer noopener">https://www.acmicpc.net/problem/1916</a></p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<figure id="problem-info" class="wp-block-table"><table class="has-fixed-layout"><thead><tr><th class="has-text-align-left" data-align="left">시간 제한</th><th class="has-text-align-left" data-align="left">메모리 제한</th><th class="has-text-align-left" data-align="left">제출</th><th class="has-text-align-left" data-align="left">정답</th><th class="has-text-align-left" data-align="left">맞힌 사람</th><th class="has-text-align-left" data-align="left">정답 비율</th></tr></thead><tbody><tr><td class="has-text-align-left" data-align="left">0.5 초</td><td class="has-text-align-left" data-align="left">128 MB</td><td class="has-text-align-left" data-align="left">64786</td><td class="has-text-align-left" data-align="left">20209</td><td class="has-text-align-left" data-align="left">13257</td><td class="has-text-align-left" data-align="left">32.113%</td></tr></tbody></table></figure>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">문제</h2>



<p class="has-medium-font-size">N개의&nbsp;도시가 있다. 그리고 한 도시에서 출발하여 다른 도시에 도착하는 M개의 버스가 있다. </p>



<p class="has-medium-font-size">우리는 A번째 도시에서 B번째 도시까지 가는데 드는 버스 비용을 최소화 시키려고 한다.&nbsp;</p>



<p class="has-medium-font-size">A번째 도시에서 B번째 도시까지 가는데 드는 최소비용을 출력하여라. </p>



<p class="has-medium-font-size">도시의 번호는 1부터 N까지이다.</p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">입력</h2>



<p class="has-medium-font-size">첫째 줄에 도시의 개수 N(1 ≤ N ≤ 1,000)이 주어지고 둘째 줄에는 버스의 개수 M(1 ≤ M ≤ 100,000)이 주어진다. </p>



<p class="has-medium-font-size">그리고 셋째 줄부터 M+2줄까지 다음과 같은 버스의 정보가 주어진다. </p>



<p class="has-medium-font-size">먼저 처음에는 그 버스의 출발 도시의 번호가 주어진다. </p>



<p class="has-medium-font-size">그리고 그 다음에는 도착지의 도시 번호가 주어지고 또 그 버스 비용이 주어진다. </p>



<p class="has-medium-font-size">버스 비용은 0보다 크거나 같고, 100,000보다 작은 정수이다.</p>



<p class="has-medium-font-size">그리고 M+3째 줄에는 우리가 구하고자 하는 구간 출발점의 도시번호와 도착점의 도시번호가 주어진다. </p>



<p class="has-medium-font-size">출발점에서 도착점을 갈 수 있는 경우만 입력으로 주어진다.</p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">출력</h2>



<p class="has-medium-font-size">첫째 줄에 출발 도시에서 도착 도시까지 가는데 드는 최소 비용을 출력한다.</p>



<div style="height:37px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 입력 1</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">5
8
1 2 2
1 3 3
1 4 1
1 5 10
2 4 2
3 4 1
3 5 1
4 5 3
1 5</pre>



<div style="height:31px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 출력 1</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">4</pre>



<div style="height:60px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 입력 A</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">2
1
1 2 0
1 2</pre>



<div style="height:31px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 출력 A</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">0</pre>



<div style="height:60px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 입력 B</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">4
3
2 1 1
1 3 1
3 4 1
2 4</pre>



<div style="height:31px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 출력 B</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">3</pre>



<div style="height:60px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 입력 C</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">1000
999
1 2 99999
2 3 99999
3 4 99999
4 5 99999
5 6 99999
6 7 99999
7 8 99999
8 9 99999
9 10 99999
10 11 99999
11 12 99999
12 13 99999
13 14 99999
14 15 99999
15 16 99999
16 17 99999
17 18 99999
18 19 99999
19 20 99999
20 21 99999
21 22 99999
22 23 99999
23 24 99999
24 25 99999
25 26 99999
26 27 99999
27 28 99999
28 29 99999
29 30 99999
30 31 99999
31 32 99999
32 33 99999
33 34 99999
34 35 99999
35 36 99999
36 37 99999
37 38 99999
38 39 99999
39 40 99999
40 41 99999
41 42 99999
42 43 99999
43 44 99999
44 45 99999
45 46 99999
46 47 99999
47 48 99999
48 49 99999
49 50 99999
50 51 99999
51 52 99999
52 53 99999
53 54 99999
54 55 99999
55 56 99999
56 57 99999
57 58 99999
58 59 99999
59 60 99999
60 61 99999
61 62 99999
62 63 99999
63 64 99999
64 65 99999
65 66 99999
66 67 99999
67 68 99999
68 69 99999
69 70 99999
70 71 99999
71 72 99999
72 73 99999
73 74 99999
74 75 99999
75 76 99999
76 77 99999
77 78 99999
78 79 99999
79 80 99999
80 81 99999
81 82 99999
82 83 99999
83 84 99999
84 85 99999
85 86 99999
86 87 99999
87 88 99999
88 89 99999
89 90 99999
90 91 99999
91 92 99999
92 93 99999
93 94 99999
94 95 99999
95 96 99999
96 97 99999
97 98 99999
98 99 99999
99 100 99999
100 101 99999
101 102 99999
102 103 99999
103 104 99999
104 105 99999
105 106 99999
106 107 99999
107 108 99999
108 109 99999
109 110 99999
110 111 99999
111 112 99999
112 113 99999
113 114 99999
114 115 99999
115 116 99999
116 117 99999
117 118 99999
118 119 99999
119 120 99999
120 121 99999
121 122 99999
122 123 99999
123 124 99999
124 125 99999
125 126 99999
126 127 99999
127 128 99999
128 129 99999
129 130 99999
130 131 99999
131 132 99999
132 133 99999
133 134 99999
134 135 99999
135 136 99999
136 137 99999
137 138 99999
138 139 99999
139 140 99999
140 141 99999
141 142 99999
142 143 99999
143 144 99999
144 145 99999
145 146 99999
146 147 99999
147 148 99999
148 149 99999
149 150 99999
150 151 99999
151 152 99999
152 153 99999
153 154 99999
154 155 99999
155 156 99999
156 157 99999
157 158 99999
158 159 99999
159 160 99999
160 161 99999
161 162 99999
162 163 99999
163 164 99999
164 165 99999
165 166 99999
166 167 99999
167 168 99999
168 169 99999
169 170 99999
170 171 99999
171 172 99999
172 173 99999
173 174 99999
174 175 99999
175 176 99999
176 177 99999
177 178 99999
178 179 99999
179 180 99999
180 181 99999
181 182 99999
182 183 99999
183 184 99999
184 185 99999
185 186 99999
186 187 99999
187 188 99999
188 189 99999
189 190 99999
190 191 99999
191 192 99999
192 193 99999
193 194 99999
194 195 99999
195 196 99999
196 197 99999
197 198 99999
198 199 99999
199 200 99999
200 201 99999
201 202 99999
202 203 99999
203 204 99999
204 205 99999
205 206 99999
206 207 99999
207 208 99999
208 209 99999
209 210 99999
210 211 99999
211 212 99999
212 213 99999
213 214 99999
214 215 99999
215 216 99999
216 217 99999
217 218 99999
218 219 99999
219 220 99999
220 221 99999
221 222 99999
222 223 99999
223 224 99999
224 225 99999
225 226 99999
226 227 99999
227 228 99999
228 229 99999
229 230 99999
230 231 99999
231 232 99999
232 233 99999
233 234 99999
234 235 99999
235 236 99999
236 237 99999
237 238 99999
238 239 99999
239 240 99999
240 241 99999
241 242 99999
242 243 99999
243 244 99999
244 245 99999
245 246 99999
246 247 99999
247 248 99999
248 249 99999
249 250 99999
250 251 99999
251 252 99999
252 253 99999
253 254 99999
254 255 99999
255 256 99999
256 257 99999
257 258 99999
258 259 99999
259 260 99999
260 261 99999
261 262 99999
262 263 99999
263 264 99999
264 265 99999
265 266 99999
266 267 99999
267 268 99999
268 269 99999
269 270 99999
270 271 99999
271 272 99999
272 273 99999
273 274 99999
274 275 99999
275 276 99999
276 277 99999
277 278 99999
278 279 99999
279 280 99999
280 281 99999
281 282 99999
282 283 99999
283 284 99999
284 285 99999
285 286 99999
286 287 99999
287 288 99999
288 289 99999
289 290 99999
290 291 99999
291 292 99999
292 293 99999
293 294 99999
294 295 99999
295 296 99999
296 297 99999
297 298 99999
298 299 99999
299 300 99999
300 301 99999
301 302 99999
302 303 99999
303 304 99999
304 305 99999
305 306 99999
306 307 99999
307 308 99999
308 309 99999
309 310 99999
310 311 99999
311 312 99999
312 313 99999
313 314 99999
314 315 99999
315 316 99999
316 317 99999
317 318 99999
318 319 99999
319 320 99999
320 321 99999
321 322 99999
322 323 99999
323 324 99999
324 325 99999
325 326 99999
326 327 99999
327 328 99999
328 329 99999
329 330 99999
330 331 99999
331 332 99999
332 333 99999
333 334 99999
334 335 99999
335 336 99999
336 337 99999
337 338 99999
338 339 99999
339 340 99999
340 341 99999
341 342 99999
342 343 99999
343 344 99999
344 345 99999
345 346 99999
346 347 99999
347 348 99999
348 349 99999
349 350 99999
350 351 99999
351 352 99999
352 353 99999
353 354 99999
354 355 99999
355 356 99999
356 357 99999
357 358 99999
358 359 99999
359 360 99999
360 361 99999
361 362 99999
362 363 99999
363 364 99999
364 365 99999
365 366 99999
366 367 99999
367 368 99999
368 369 99999
369 370 99999
370 371 99999
371 372 99999
372 373 99999
373 374 99999
374 375 99999
375 376 99999
376 377 99999
377 378 99999
378 379 99999
379 380 99999
380 381 99999
381 382 99999
382 383 99999
383 384 99999
384 385 99999
385 386 99999
386 387 99999
387 388 99999
388 389 99999
389 390 99999
390 391 99999
391 392 99999
392 393 99999
393 394 99999
394 395 99999
395 396 99999
396 397 99999
397 398 99999
398 399 99999
399 400 99999
400 401 99999
401 402 99999
402 403 99999
403 404 99999
404 405 99999
405 406 99999
406 407 99999
407 408 99999
408 409 99999
409 410 99999
410 411 99999
411 412 99999
412 413 99999
413 414 99999
414 415 99999
415 416 99999
416 417 99999
417 418 99999
418 419 99999
419 420 99999
420 421 99999
421 422 99999
422 423 99999
423 424 99999
424 425 99999
425 426 99999
426 427 99999
427 428 99999
428 429 99999
429 430 99999
430 431 99999
431 432 99999
432 433 99999
433 434 99999
434 435 99999
435 436 99999
436 437 99999
437 438 99999
438 439 99999
439 440 99999
440 441 99999
441 442 99999
442 443 99999
443 444 99999
444 445 99999
445 446 99999
446 447 99999
447 448 99999
448 449 99999
449 450 99999
450 451 99999
451 452 99999
452 453 99999
453 454 99999
454 455 99999
455 456 99999
456 457 99999
457 458 99999
458 459 99999
459 460 99999
460 461 99999
461 462 99999
462 463 99999
463 464 99999
464 465 99999
465 466 99999
466 467 99999
467 468 99999
468 469 99999
469 470 99999
470 471 99999
471 472 99999
472 473 99999
473 474 99999
474 475 99999
475 476 99999
476 477 99999
477 478 99999
478 479 99999
479 480 99999
480 481 99999
481 482 99999
482 483 99999
483 484 99999
484 485 99999
485 486 99999
486 487 99999
487 488 99999
488 489 99999
489 490 99999
490 491 99999
491 492 99999
492 493 99999
493 494 99999
494 495 99999
495 496 99999
496 497 99999
497 498 99999
498 499 99999
499 500 99999
500 501 99999
501 502 99999
502 503 99999
503 504 99999
504 505 99999
505 506 99999
506 507 99999
507 508 99999
508 509 99999
509 510 99999
510 511 99999
511 512 99999
512 513 99999
513 514 99999
514 515 99999
515 516 99999
516 517 99999
517 518 99999
518 519 99999
519 520 99999
520 521 99999
521 522 99999
522 523 99999
523 524 99999
524 525 99999
525 526 99999
526 527 99999
527 528 99999
528 529 99999
529 530 99999
530 531 99999
531 532 99999
532 533 99999
533 534 99999
534 535 99999
535 536 99999
536 537 99999
537 538 99999
538 539 99999
539 540 99999
540 541 99999
541 542 99999
542 543 99999
543 544 99999
544 545 99999
545 546 99999
546 547 99999
547 548 99999
548 549 99999
549 550 99999
550 551 99999
551 552 99999
552 553 99999
553 554 99999
554 555 99999
555 556 99999
556 557 99999
557 558 99999
558 559 99999
559 560 99999
560 561 99999
561 562 99999
562 563 99999
563 564 99999
564 565 99999
565 566 99999
566 567 99999
567 568 99999
568 569 99999
569 570 99999
570 571 99999
571 572 99999
572 573 99999
573 574 99999
574 575 99999
575 576 99999
576 577 99999
577 578 99999
578 579 99999
579 580 99999
580 581 99999
581 582 99999
582 583 99999
583 584 99999
584 585 99999
585 586 99999
586 587 99999
587 588 99999
588 589 99999
589 590 99999
590 591 99999
591 592 99999
592 593 99999
593 594 99999
594 595 99999
595 596 99999
596 597 99999
597 598 99999
598 599 99999
599 600 99999
600 601 99999
601 602 99999
602 603 99999
603 604 99999
604 605 99999
605 606 99999
606 607 99999
607 608 99999
608 609 99999
609 610 99999
610 611 99999
611 612 99999
612 613 99999
613 614 99999
614 615 99999
615 616 99999
616 617 99999
617 618 99999
618 619 99999
619 620 99999
620 621 99999
621 622 99999
622 623 99999
623 624 99999
624 625 99999
625 626 99999
626 627 99999
627 628 99999
628 629 99999
629 630 99999
630 631 99999
631 632 99999
632 633 99999
633 634 99999
634 635 99999
635 636 99999
636 637 99999
637 638 99999
638 639 99999
639 640 99999
640 641 99999
641 642 99999
642 643 99999
643 644 99999
644 645 99999
645 646 99999
646 647 99999
647 648 99999
648 649 99999
649 650 99999
650 651 99999
651 652 99999
652 653 99999
653 654 99999
654 655 99999
655 656 99999
656 657 99999
657 658 99999
658 659 99999
659 660 99999
660 661 99999
661 662 99999
662 663 99999
663 664 99999
664 665 99999
665 666 99999
666 667 99999
667 668 99999
668 669 99999
669 670 99999
670 671 99999
671 672 99999
672 673 99999
673 674 99999
674 675 99999
675 676 99999
676 677 99999
677 678 99999
678 679 99999
679 680 99999
680 681 99999
681 682 99999
682 683 99999
683 684 99999
684 685 99999
685 686 99999
686 687 99999
687 688 99999
688 689 99999
689 690 99999
690 691 99999
691 692 99999
692 693 99999
693 694 99999
694 695 99999
695 696 99999
696 697 99999
697 698 99999
698 699 99999
699 700 99999
700 701 99999
701 702 99999
702 703 99999
703 704 99999
704 705 99999
705 706 99999
706 707 99999
707 708 99999
708 709 99999
709 710 99999
710 711 99999
711 712 99999
712 713 99999
713 714 99999
714 715 99999
715 716 99999
716 717 99999
717 718 99999
718 719 99999
719 720 99999
720 721 99999
721 722 99999
722 723 99999
723 724 99999
724 725 99999
725 726 99999
726 727 99999
727 728 99999
728 729 99999
729 730 99999
730 731 99999
731 732 99999
732 733 99999
733 734 99999
734 735 99999
735 736 99999
736 737 99999
737 738 99999
738 739 99999
739 740 99999
740 741 99999
741 742 99999
742 743 99999
743 744 99999
744 745 99999
745 746 99999
746 747 99999
747 748 99999
748 749 99999
749 750 99999
750 751 99999
751 752 99999
752 753 99999
753 754 99999
754 755 99999
755 756 99999
756 757 99999
757 758 99999
758 759 99999
759 760 99999
760 761 99999
761 762 99999
762 763 99999
763 764 99999
764 765 99999
765 766 99999
766 767 99999
767 768 99999
768 769 99999
769 770 99999
770 771 99999
771 772 99999
772 773 99999
773 774 99999
774 775 99999
775 776 99999
776 777 99999
777 778 99999
778 779 99999
779 780 99999
780 781 99999
781 782 99999
782 783 99999
783 784 99999
784 785 99999
785 786 99999
786 787 99999
787 788 99999
788 789 99999
789 790 99999
790 791 99999
791 792 99999
792 793 99999
793 794 99999
794 795 99999
795 796 99999
796 797 99999
797 798 99999
798 799 99999
799 800 99999
800 801 99999
801 802 99999
802 803 99999
803 804 99999
804 805 99999
805 806 99999
806 807 99999
807 808 99999
808 809 99999
809 810 99999
810 811 99999
811 812 99999
812 813 99999
813 814 99999
814 815 99999
815 816 99999
816 817 99999
817 818 99999
818 819 99999
819 820 99999
820 821 99999
821 822 99999
822 823 99999
823 824 99999
824 825 99999
825 826 99999
826 827 99999
827 828 99999
828 829 99999
829 830 99999
830 831 99999
831 832 99999
832 833 99999
833 834 99999
834 835 99999
835 836 99999
836 837 99999
837 838 99999
838 839 99999
839 840 99999
840 841 99999
841 842 99999
842 843 99999
843 844 99999
844 845 99999
845 846 99999
846 847 99999
847 848 99999
848 849 99999
849 850 99999
850 851 99999
851 852 99999
852 853 99999
853 854 99999
854 855 99999
855 856 99999
856 857 99999
857 858 99999
858 859 99999
859 860 99999
860 861 99999
861 862 99999
862 863 99999
863 864 99999
864 865 99999
865 866 99999
866 867 99999
867 868 99999
868 869 99999
869 870 99999
870 871 99999
871 872 99999
872 873 99999
873 874 99999
874 875 99999
875 876 99999
876 877 99999
877 878 99999
878 879 99999
879 880 99999
880 881 99999
881 882 99999
882 883 99999
883 884 99999
884 885 99999
885 886 99999
886 887 99999
887 888 99999
888 889 99999
889 890 99999
890 891 99999
891 892 99999
892 893 99999
893 894 99999
894 895 99999
895 896 99999
896 897 99999
897 898 99999
898 899 99999
899 900 99999
900 901 99999
901 902 99999
902 903 99999
903 904 99999
904 905 99999
905 906 99999
906 907 99999
907 908 99999
908 909 99999
909 910 99999
910 911 99999
911 912 99999
912 913 99999
913 914 99999
914 915 99999
915 916 99999
916 917 99999
917 918 99999
918 919 99999
919 920 99999
920 921 99999
921 922 99999
922 923 99999
923 924 99999
924 925 99999
925 926 99999
926 927 99999
927 928 99999
928 929 99999
929 930 99999
930 931 99999
931 932 99999
932 933 99999
933 934 99999
934 935 99999
935 936 99999
936 937 99999
937 938 99999
938 939 99999
939 940 99999
940 941 99999
941 942 99999
942 943 99999
943 944 99999
944 945 99999
945 946 99999
946 947 99999
947 948 99999
948 949 99999
949 950 99999
950 951 99999
951 952 99999
952 953 99999
953 954 99999
954 955 99999
955 956 99999
956 957 99999
957 958 99999
958 959 99999
959 960 99999
960 961 99999
961 962 99999
962 963 99999
963 964 99999
964 965 99999
965 966 99999
966 967 99999
967 968 99999
968 969 99999
969 970 99999
970 971 99999
971 972 99999
972 973 99999
973 974 99999
974 975 99999
975 976 99999
976 977 99999
977 978 99999
978 979 99999
979 980 99999
980 981 99999
981 982 99999
982 983 99999
983 984 99999
984 985 99999
985 986 99999
986 987 99999
987 988 99999
988 989 99999
989 990 99999
990 991 99999
991 992 99999
992 993 99999
993 994 99999
994 995 99999
995 996 99999
996 997 99999
997 998 99999
998 999 99999
999 1000 99999
1 1000</pre>



<div style="height:31px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 출력 C</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">99899001</pre>



<div style="height:60px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">출처</h2>



<ul class="wp-block-list">
<li>데이터를 추가한 사람:&nbsp;<a href="https://www.acmicpc.net/user/djm03178" target="_blank" rel="noreferrer noopener">djm03178</a>,&nbsp;<a href="https://www.acmicpc.net/user/qf9ar8nv" target="_blank" rel="noreferrer noopener">qf9ar8nv</a>,&nbsp;<a href="https://www.acmicpc.net/user/sait2000" target="_blank" rel="noreferrer noopener">sait2000</a></li>



<li>시간 제한을 수정한 사람:&nbsp;<a href="https://www.acmicpc.net/user/djm03178" target="_blank" rel="noreferrer noopener">djm03178</a></li>



<li>문제의 오타를 찾은 사람:&nbsp;<a href="https://www.acmicpc.net/user/HowlingOfSouL" target="_blank" rel="noreferrer noopener">HowlingOfSouL</a>,&nbsp;<a href="https://www.acmicpc.net/user/ibjsw" target="_blank" rel="noreferrer noopener">ibjsw</a></li>



<li>잘못된 데이터를 찾은 사람:&nbsp;<a href="https://www.acmicpc.net/user/hsnks100" target="_blank" rel="noreferrer noopener">hsnks100</a></li>



<li>빠진 조건을 찾은 사람:&nbsp;<a href="https://www.acmicpc.net/user/jh05013" target="_blank" rel="noreferrer noopener">jh05013</a>,&nbsp;<a href="https://www.acmicpc.net/user/luke0201" target="_blank" rel="noreferrer noopener">luke0201</a>,&nbsp;<a href="https://www.acmicpc.net/user/toysmars" target="_blank" rel="noreferrer noopener">toysmars</a></li>
</ul>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">알고리즘 분류</h2>



<ul class="wp-block-list">
<li><a href="https://www.acmicpc.net/problem/tag/7" target="_blank" rel="noreferrer noopener">그래프 이론</a></li>



<li><a href="https://www.acmicpc.net/problem/tag/22" target="_blank" rel="noreferrer noopener">데이크스트라</a></li>
</ul>



<hr class="wp-block-separator has-alpha-channel-opacity is-style-wide" style="margin-top:var(--wp--preset--spacing--80);margin-bottom:var(--wp--preset--spacing--80)"/>



<p class="has-medium-font-size">버스 비용은 0보다 크거나 같고, 100,000보다 작은 정수 </p>



<p class="has-medium-font-size">=&gt; <strong>버스의 비용이 0 인 경우가 존재</strong>한다는 것에 주의</p>



<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>



<p class="has-medium-font-size">추가적으로 현재 임시노드보다 거리가 크다면 넘어간다 (없으면 시간초과 발생)</p>



<figure class="wp-block-image size-full"><img decoding="async" width="842" height="132" src="https://lycos7560.com/wp-content/uploads/image-1049.png" alt="" class="wp-image-5448" srcset="https://lycos7560.com/wp-content/uploads/image-1049.png 842w, https://lycos7560.com/wp-content/uploads/image-1049-300x47.png 300w, https://lycos7560.com/wp-content/uploads/image-1049-768x120.png 768w" sizes="(max-width: 842px) 100vw, 842px" /></figure>



<div style="height:39px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">통과된 코드</h2>



<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>



<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">#include &lt;iostream>
#include &lt;vector>
#include &lt;queue>

using namespace std;

constexpr int MAXN = 10001;
constexpr int INF = INT32_MAX;

// Dijkstra 알고리즘에 사용할 우선순위 큐
priority_queue&lt;pair&lt;int, int>> myPQ;

///*
//각 노드에 연결되어 있는 노드에 대한 정보를 담는 벡터
//a번 노드에서 b번 노드로 가는 비용이 c라는 의미
//graph[a].push_back((make_pair(B, C));
//*/
vector&lt;pair&lt;int, int>> graph[MAXN];

// 임시노드
int disArr[MAXN];

// N : 도시의 개수, M : 간선의 개수, K : 시작 노드, E : 도착 노드
// u : 현재 노드, v : 이웃 노드, dist : 거리
int N, M, K, E, u, v, dist;

void Dijkstra(int start)
{
    // 임시배열 초기화
    for (int i = 1; i &lt;= N; i++) disArr[i] = INF;
    // 우선순위 큐에 삽입.
    myPQ.push({ 0, start }); // &lt; first : 거리 , second : 노드 인덱스 >
    disArr[start] = 0;

    while (!myPQ.empty()) {
        // -를 붙이는 이유는 우선순위 큐를 이용하여 정렬하기 위함이다.
        // (최소힙으로 구현)
        int nCost = -myPQ.top().first;
        int now = myPQ.top().second;
        myPQ.pop();

        // 이미 담겨 있는 것보다 거리가 크면 넘어간다.
        if (disArr[now] &lt; nCost) continue;

        // 해당 노드에서 연결된 모든 경로를 확인
        for (int i = 0; i &lt; graph[now].size(); i++) {

            // disSum = 임시 노드 + 현재 노드에서 i로가는 비용
            int disSum = nCost + graph[now][i].second;
            // 비용이 작다면 최단경로 테이블 값을 갱신.
            if (disSum &lt; disArr[graph[now][i].first]) {
                // 임시 노드 업데이트
                disArr[graph[now][i].first] = disSum;
                // 우선순위 큐에 (거리, 노드 인덱스) 푸시
                myPQ.push(make_pair(-disSum, graph[now][i].first));
            }
        }
    }
}

int main()
{
    ios_base::sync_with_stdio(false); // scanf와 동기화를 비활성화
    // cin.tie(null); 코드는 cin과 cout의 묶음을 풀어줍니다.
    cin.tie(NULL);
    cout.tie(NULL);

    // N 정점의 개수, E 간선의 개수를 입력받는다.
    cin >> N >> M;

    for (int i = 0; i &lt; M; i++) {
        cin >> u >> v >> dist;
        graph[u].push_back(make_pair(v, dist));
    }

    cin >> K >> E;

    Dijkstra(K);

    cout &lt;&lt; disArr[E];

    return 0;
}</pre>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<figure class="wp-block-image size-full"><img decoding="async" width="1034" height="225" src="https://lycos7560.com/wp-content/uploads/image-1048.png" alt="" class="wp-image-5441" srcset="https://lycos7560.com/wp-content/uploads/image-1048.png 1034w, https://lycos7560.com/wp-content/uploads/image-1048-300x65.png 300w, https://lycos7560.com/wp-content/uploads/image-1048-768x167.png 768w" sizes="(max-width: 1034px) 100vw, 1034px" /></figure>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-5698326622209671" crossorigin="anonymous"></script>
<ins class="adsbygoogle" style="display:block" data-ad-format="autorelaxed" data-ad-client="ca-pub-5698326622209671" data-ad-slot="4245812909"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-1916%eb%b2%88-%ec%b5%9c%ec%86%8c%eb%b9%84%ec%9a%a9-%ea%b5%ac%ed%95%98%ea%b8%b0-c-dijkstra-%ec%b6%94%ea%b0%80-%eb%b0%98%eb%a1%80-baekjoon/5438/">백준 1916번 (최소비용 구하기, C++, Dijkstra) / 추가 반례 [BAEKJOON]</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-1916%eb%b2%88-%ec%b5%9c%ec%86%8c%eb%b9%84%ec%9a%a9-%ea%b5%ac%ed%95%98%ea%b8%b0-c-dijkstra-%ec%b6%94%ea%b0%80-%eb%b0%98%eb%a1%80-baekjoon/5438/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>백준 1753번 (최단경로, C++, Dijkstra) [BAEKJOON]</title>
		<link>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-1753%eb%b2%88-%ec%b5%9c%eb%8b%a8%ea%b2%bd%eb%a1%9c-c-dijkstra-baekjoon/5398/</link>
					<comments>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-1753%eb%b2%88-%ec%b5%9c%eb%8b%a8%ea%b2%bd%eb%a1%9c-c-dijkstra-baekjoon/5398/#respond</comments>
		
		<dc:creator><![CDATA[lycos7560]]></dc:creator>
		<pubDate>Tue, 24 Jan 2023 08:50:09 +0000</pubDate>
				<category><![CDATA[BaekjoonOnlineJudge]]></category>
		<category><![CDATA[C++/CPP]]></category>
		<category><![CDATA[1753]]></category>
		<category><![CDATA[1753번]]></category>
		<category><![CDATA[algorithm]]></category>
		<category><![CDATA[Baekjoon]]></category>
		<category><![CDATA[BFS]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[cin.tie(NULL)]]></category>
		<category><![CDATA[cout.tie(NULL)]]></category>
		<category><![CDATA[cpp]]></category>
		<category><![CDATA[Dijkstra]]></category>
		<category><![CDATA[DP]]></category>
		<category><![CDATA[DynamicProgramming]]></category>
		<category><![CDATA[priority_queue]]></category>
		<category><![CDATA[PriorityQueue]]></category>
		<category><![CDATA[Queue]]></category>
		<category><![CDATA[study]]></category>
		<category><![CDATA[공부]]></category>
		<category><![CDATA[그래프 이론]]></category>
		<category><![CDATA[그래프 탐색]]></category>
		<category><![CDATA[기본]]></category>
		<category><![CDATA[기초]]></category>
		<category><![CDATA[길찾기]]></category>
		<category><![CDATA[너비]]></category>
		<category><![CDATA[너비 우선 탐색]]></category>
		<category><![CDATA[다이나믹 프로그래밍]]></category>
		<category><![CDATA[다익스트라]]></category>
		<category><![CDATA[데이크스트라]]></category>
		<category><![CDATA[백준]]></category>
		<category><![CDATA[백준 1753]]></category>
		<category><![CDATA[백준 1753번]]></category>
		<category><![CDATA[시간]]></category>
		<category><![CDATA[알고리즘]]></category>
		<category><![CDATA[지름길]]></category>
		<category><![CDATA[최단경로]]></category>
		<category><![CDATA[컴파일 에러]]></category>
		<category><![CDATA[코딩테스트]]></category>
		<category><![CDATA[코테]]></category>
		<guid isPermaLink="false">https://lycos7560.com/?p=5398</guid>

					<description><![CDATA[<p>백준(BAEKJOON) 1753번 '최단경로' 문제에 대한 글입니다. Dijkstra 알고리즘을 이용하여 해결하였습니다. (This is an article about the 'shortest route' problem in BAEKJOON No. 1753. It was solved using Dijkstra algorithm.)</p>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-1753%eb%b2%88-%ec%b5%9c%eb%8b%a8%ea%b2%bd%eb%a1%9c-c-dijkstra-baekjoon/5398/">백준 1753번 (최단경로, C++, Dijkstra) [BAEKJOON]</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-5698326622209671" crossorigin="anonymous"></script>
<!-- HorizontalAD -->
<ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-5698326622209671" data-ad-slot="6908948342" data-ad-format="auto" data-full-width-responsive="true"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>



<div style="height:62px" aria-hidden="true" class="wp-block-spacer"></div>


				<div class="wp-block-uagb-table-of-contents uagb-toc__align-left uagb-toc__columns-1  uagb-block-d79a1d63      "
					data-scroll= "1"
					data-offset= "30"
					style=""
				>
				<div class="uagb-toc__wrap">
						<div class="uagb-toc__title">
							목차 테이블						</div>
																						<div class="uagb-toc__list-wrap ">
						<ol class="uagb-toc__list"><li class="uagb-toc__list"><a href="#최단경로" class="uagb-toc-link__trigger">최단경로</a><ul class="uagb-toc__list"><li class="uagb-toc__list"><a href="#통과된-코드" class="uagb-toc-link__trigger">통과된 코드</a></ul></ol>					</div>
									</div>
				</div>
			


<div style="height:62px" aria-hidden="true" class="wp-block-spacer"></div>



<h1 class="wp-block-heading">최단경로</h1>



<p class="has-medium-font-size"><a href="https://www.acmicpc.net/problem/1753" target="_blank" rel="noreferrer noopener">https://www.acmicpc.net/problem/1753</a></p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<figure id="problem-info" class="wp-block-table"><table class="has-fixed-layout"><thead><tr><th class="has-text-align-left" data-align="left">시간 제한</th><th class="has-text-align-left" data-align="left">메모리 제한</th><th class="has-text-align-left" data-align="left">제출</th><th class="has-text-align-left" data-align="left">정답</th><th class="has-text-align-left" data-align="left">맞힌 사람</th><th class="has-text-align-left" data-align="left">정답 비율</th></tr></thead><tbody><tr><td class="has-text-align-left" data-align="left">1 초</td><td class="has-text-align-left" data-align="left">256 MB</td><td class="has-text-align-left" data-align="left">152962</td><td class="has-text-align-left" data-align="left">44492</td><td class="has-text-align-left" data-align="left">22229</td><td class="has-text-align-left" data-align="left">24.702%</td></tr></tbody></table></figure>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading">문제</h3>



<p class="has-medium-font-size">방향그래프가 주어지면 주어진 시작점에서 다른 모든 정점으로의 최단 경로를 구하는 프로그램을 작성하시오. </p>



<p class="has-medium-font-size">단, 모든 간선의 가중치는 10 이하의 자연수이다.</p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading">입력</h3>



<p class="has-medium-font-size">첫째 줄에 정점의 개수 V와 간선의 개수 E가 주어진다. (1 ≤ V ≤ 20,000, 1 ≤ E ≤ 300,000) </p>



<p class="has-medium-font-size">모든 정점에는 1부터 V까지 번호가 매겨져 있다고 가정한다. </p>



<p class="has-medium-font-size">둘째 줄에는 시작 정점의 번호 K(1 ≤ K ≤ V)가 주어진다. </p>



<p class="has-medium-font-size">셋째 줄부터 E개의 줄에 걸쳐 각 간선을 나타내는 세 개의 정수 (u, v, w)가 순서대로 주어진다. </p>



<p class="has-medium-font-size">이는 u에서 v로 가는 가중치 w인 간선이 존재한다는 뜻이다. </p>



<p class="has-medium-font-size">u와 v는 서로 다르며 w는 10 이하의 자연수이다. </p>



<p class="has-medium-font-size">서로 다른 두 정점 사이에 여러 개의 간선이 존재할 수도 있음에 유의한다.</p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading">출력</h3>



<p class="has-medium-font-size">첫째 줄부터 V개의 줄에 걸쳐, i번째 줄에 i번&nbsp;정점으로의 최단 경로의 경로값을 출력한다. </p>



<p class="has-medium-font-size">시작점 자신은 0으로 출력하고, 경로가 존재하지 않는 경우에는 INF를 출력하면 된다.</p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading">예제 입력 1</h3>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">5 6
1
5 1 1
1 2 2
1 3 3
2 3 4
2 4 5
3 4 6</pre>



<div style="height:31px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading">예제 출력 1</h3>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">0
2
3
7
INF</pre>



<div style="height:60px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading">출처</h3>



<ul class="wp-block-list">
<li>빠진 조건을 찾은 사람:&nbsp;<a href="https://www.acmicpc.net/user/algoshipda" target="_blank" rel="noreferrer noopener">algoshipda</a></li>



<li>데이터를 추가한 사람:&nbsp;<a href="https://www.acmicpc.net/user/august14" target="_blank" rel="noreferrer noopener">august14</a>,&nbsp;<a href="https://www.acmicpc.net/user/djm03178" target="_blank" rel="noreferrer noopener">djm03178</a>,&nbsp;<a href="https://www.acmicpc.net/user/sait2000" target="_blank" rel="noreferrer noopener">sait2000</a>,&nbsp;<a href="https://www.acmicpc.net/user/tolelom" target="_blank" rel="noreferrer noopener">tolelom</a></li>



<li>문제를 만든 사람:&nbsp;<a href="https://www.acmicpc.net/user/author5" target="_blank" rel="noreferrer noopener">author5</a></li>
</ul>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading">알고리즘 분류</h3>



<ul class="wp-block-list">
<li><a href="https://www.acmicpc.net/problem/tag/7" target="_blank" rel="noreferrer noopener">그래프 이론</a></li>



<li><a href="https://www.acmicpc.net/problem/tag/22" target="_blank" rel="noreferrer noopener">데이크스트라</a></li>
</ul>



<hr class="wp-block-separator has-alpha-channel-opacity is-style-wide" style="margin-top:var(--wp--preset--spacing--80);margin-bottom:var(--wp--preset--spacing--80)"/>



<p class="has-medium-font-size">1504번을 해결하고 접한 문제라서 쉽게 풀었다.</p>



<p class="has-medium-font-size">Dijkstra 알고리즘을 구현하는 기본적인 문제</p>



<div style="height:37px" aria-hidden="true" class="wp-block-spacer"></div>



<figure class="wp-block-embed is-type-wp-embed is-provider-어제와-내일의-나-그-사이의-이야기 wp-block-embed-어제와-내일의-나-그-사이의-이야기"><div class="wp-block-embed__wrapper">
<blockquote class="wp-embedded-content" data-secret="PIUlObEGXd"><a href="https://lycos7560.com/?p=5366">백준 1504번 (특정한 최단 경로, C++, Dijkstra) / 추가 반례 [BAEKJOON]</a></blockquote><iframe loading="lazy" class="wp-embedded-content" sandbox="allow-scripts" security="restricted"  title="&#8220;백준 1504번 (특정한 최단 경로, C++, Dijkstra) / 추가 반례 [BAEKJOON]&#8221; &#8212; 어제와 내일의 나 그 사이의 이야기" src="https://lycos7560.com/?p=5366&#038;embed=true#?secret=2cwzGKytLg#?secret=PIUlObEGXd" data-secret="PIUlObEGXd" width="600" height="338" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe>
</div></figure>



<figure class="wp-block-embed is-type-wp-embed is-provider-어제와-내일의-나-그-사이의-이야기 wp-block-embed-어제와-내일의-나-그-사이의-이야기"><div class="wp-block-embed__wrapper">
<blockquote class="wp-embedded-content" data-secret="tTtm0Chvk3"><a href="https://lycos7560.com/?p=5223">알고리즘 &#8211; 다익스트라/데이크스트라(Dijkstra) 알고리즘 정리 (길찾기)</a></blockquote><iframe loading="lazy" class="wp-embedded-content" sandbox="allow-scripts" security="restricted"  title="&#8220;알고리즘 &#8211; 다익스트라/데이크스트라(Dijkstra) 알고리즘 정리 (길찾기)&#8221; &#8212; 어제와 내일의 나 그 사이의 이야기" src="https://lycos7560.com/?p=5223&#038;embed=true#?secret=wcdkpVGqc8#?secret=tTtm0Chvk3" data-secret="tTtm0Chvk3" width="600" height="338" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe>
</div></figure>



<div style="height:55px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">통과된 코드</h2>



<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>



<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">#include &lt;iostream>
#include &lt;vector>
#include &lt;queue>

using namespace std;

constexpr int MAXN = 20001;
constexpr int INF = INT32_MAX;

// Dijkstra 알고리즘에 사용할 우선순위 큐
priority_queue&lt;pair&lt;int, int>> myPQ;

///*
//각 노드에 연결되어 있는 노드에 대한 정보를 담는 벡터
//a번 노드에서 b번 노드로 가는 비용이 c라는 의미
//graph[a].push_back((make_pair(B, C));
//*/
vector&lt;pair&lt;int, int>> graph[MAXN];

// 임시노드
int disArr[MAXN];

// v : 정점의 개수, E : 간선의 개수, K : 시작 노드
// u : 현재 노드, v : 이웃 노드, dist : 거리
int V, E, K, u, v, dist;

void Dijkstra(int start)
{
    // 임시배열 초기화
    for (int i = 1; i &lt;= V; i++) disArr[i] = INF;
    // 우선순위 큐에 삽입.
    myPQ.push({ 0, start }); // &lt; first : 거리 , second : 노드 인덱스 >
    disArr[start] = 0;

    while (!myPQ.empty()) {
        // -를 붙이는 이유는 우선순위 큐를 이용하여 정렬하기 위함이다.
        // (최소힙으로 구현)
        int nCost = -myPQ.top().first;
        int now = myPQ.top().second;
        myPQ.pop();
        // 해당 노드에서 연결된 모든 경로를 확인
        for (int i = 0; i &lt; graph[now].size(); i++) {
            // 0이라면 길이 없다는 의미 continue
            if (graph[now][i].second == 0) continue;
            // disSum = 임시 노드 + 현재 노드에서 i로가는 비용
            int disSum = nCost + graph[now][i].second;
            // 비용이 더 작다면 최단경로 테이블 값을 갱신.
            if (disSum &lt; disArr[graph[now][i].first]) {
                // 임시 노드 업데이트
                disArr[graph[now][i].first] = disSum;
                // 우선순위 큐에 (거리, 노드 인덱스) 푸시
                myPQ.push(make_pair(-disSum, graph[now][i].first));
            }
        }
    }
}

int main()
{
    ios_base::sync_with_stdio(false); // scanf와 동기화를 비활성화
    // cin.tie(null); 코드는 cin과 cout의 묶음을 풀어줍니다.
    cin.tie(NULL);
    cout.tie(NULL);

    // N 정점의 개수, E 간선의 개수를 입력받는다.
    cin >> V >> E >> K;

    for (int i = 0; i &lt; E; i++) {
        cin >> u >> v >> dist;
        graph[u].push_back(make_pair(v, dist));
    }

    Dijkstra(K);

    // 출력
    for (int i = 1; i &lt;= V; i++) {
        if (disArr[i] == INF) {
            cout &lt;&lt; "INF" &lt;&lt; "\n";
            continue;
        }

        cout &lt;&lt; disArr[i] &lt;&lt; "\n";
    }

    return 0;
}</pre>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<figure class="wp-block-image size-full"><img decoding="async" width="1038" height="147" src="https://lycos7560.com/wp-content/uploads/image-1045.png" alt="" class="wp-image-5402" srcset="https://lycos7560.com/wp-content/uploads/image-1045.png 1038w, https://lycos7560.com/wp-content/uploads/image-1045-300x42.png 300w, https://lycos7560.com/wp-content/uploads/image-1045-768x109.png 768w" sizes="(max-width: 1038px) 100vw, 1038px" /></figure>



<div style="height:35px" aria-hidden="true" class="wp-block-spacer"></div>



<p class="has-medium-font-size">주석을 제거하고 제출하니 시간이 약간 줄었다!! (큰 의미는 없지만)</p>



<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>



<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-5698326622209671" crossorigin="anonymous"></script>
<ins class="adsbygoogle" style="display:block" data-ad-format="autorelaxed" data-ad-client="ca-pub-5698326622209671" data-ad-slot="4245812909"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-1753%eb%b2%88-%ec%b5%9c%eb%8b%a8%ea%b2%bd%eb%a1%9c-c-dijkstra-baekjoon/5398/">백준 1753번 (최단경로, C++, Dijkstra) [BAEKJOON]</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-1753%eb%b2%88-%ec%b5%9c%eb%8b%a8%ea%b2%bd%eb%a1%9c-c-dijkstra-baekjoon/5398/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>백준 1504번 (특정한 최단 경로, C++, Dijkstra) / 추가 반례 [BAEKJOON]</title>
		<link>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-1504%eb%b2%88-%ed%8a%b9%ec%a0%95%ed%95%9c-%ec%b5%9c%eb%8b%a8-%ea%b2%bd%eb%a1%9c-c-dijkstra-%ec%b6%94%ea%b0%80-%eb%b0%98%eb%a1%80-baekjoon/5366/</link>
					<comments>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-1504%eb%b2%88-%ed%8a%b9%ec%a0%95%ed%95%9c-%ec%b5%9c%eb%8b%a8-%ea%b2%bd%eb%a1%9c-c-dijkstra-%ec%b6%94%ea%b0%80-%eb%b0%98%eb%a1%80-baekjoon/5366/#comments</comments>
		
		<dc:creator><![CDATA[lycos7560]]></dc:creator>
		<pubDate>Tue, 24 Jan 2023 08:18:51 +0000</pubDate>
				<category><![CDATA[BaekjoonOnlineJudge]]></category>
		<category><![CDATA[C++/CPP]]></category>
		<category><![CDATA[1504]]></category>
		<category><![CDATA[1504번]]></category>
		<category><![CDATA[algorithm]]></category>
		<category><![CDATA[Baekjoon]]></category>
		<category><![CDATA[BFS]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[cin.tie(NULL)]]></category>
		<category><![CDATA[cout.tie(NULL)]]></category>
		<category><![CDATA[cpp]]></category>
		<category><![CDATA[Dijkstra]]></category>
		<category><![CDATA[DP]]></category>
		<category><![CDATA[DynamicProgramming]]></category>
		<category><![CDATA[priority_queue]]></category>
		<category><![CDATA[PriorityQueue]]></category>
		<category><![CDATA[Queue]]></category>
		<category><![CDATA[study]]></category>
		<category><![CDATA[공부]]></category>
		<category><![CDATA[그래프 이론]]></category>
		<category><![CDATA[그래프 탐색]]></category>
		<category><![CDATA[기본]]></category>
		<category><![CDATA[기초]]></category>
		<category><![CDATA[길찾기]]></category>
		<category><![CDATA[너비]]></category>
		<category><![CDATA[너비 우선 탐색]]></category>
		<category><![CDATA[다이나믹 프로그래밍]]></category>
		<category><![CDATA[다익스트라]]></category>
		<category><![CDATA[데이크스트라]]></category>
		<category><![CDATA[반례]]></category>
		<category><![CDATA[백준]]></category>
		<category><![CDATA[백준 1504]]></category>
		<category><![CDATA[백준 1504번]]></category>
		<category><![CDATA[시간]]></category>
		<category><![CDATA[알고리즘]]></category>
		<category><![CDATA[예제]]></category>
		<category><![CDATA[지름길]]></category>
		<category><![CDATA[추가 반례]]></category>
		<category><![CDATA[추가 예제]]></category>
		<category><![CDATA[추가반례]]></category>
		<category><![CDATA[추가예제]]></category>
		<category><![CDATA[컴파일 에러]]></category>
		<category><![CDATA[코딩테스트]]></category>
		<category><![CDATA[코테]]></category>
		<category><![CDATA[특정한 최단 경로]]></category>
		<guid isPermaLink="false">https://lycos7560.com/?p=5366</guid>

					<description><![CDATA[<p>백준(BAEKJOON) 1504번 '특정한 최단 경로'에대한 글입니다. Dijkstra 알고리즘을 이용하여 해결하였습니다. (This is an article about BAEKJOON No. 1504 'Specific shortest path'. It was solved using Dijkstra algorithm.)</p>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-1504%eb%b2%88-%ed%8a%b9%ec%a0%95%ed%95%9c-%ec%b5%9c%eb%8b%a8-%ea%b2%bd%eb%a1%9c-c-dijkstra-%ec%b6%94%ea%b0%80-%eb%b0%98%eb%a1%80-baekjoon/5366/">백준 1504번 (특정한 최단 경로, C++, Dijkstra) / 추가 반례 [BAEKJOON]</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-5698326622209671" crossorigin="anonymous"></script>
<!-- HorizontalAD -->
<ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-5698326622209671" data-ad-slot="6908948342" data-ad-format="auto" data-full-width-responsive="true"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>



<div style="height:62px" aria-hidden="true" class="wp-block-spacer"></div>



<h1 class="wp-block-heading">특정한 최단 경로</h1>



<p class="has-medium-font-size"><a href="https://www.acmicpc.net/problem/1504" target="_blank" rel="noreferrer noopener">https://www.acmicpc.net/problem/1504</a></p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<figure id="problem-info" class="wp-block-table"><table class="has-fixed-layout"><thead><tr><th class="has-text-align-left" data-align="left">시간 제한</th><th class="has-text-align-left" data-align="left">메모리 제한</th><th class="has-text-align-left" data-align="left">제출</th><th class="has-text-align-left" data-align="left">정답</th><th class="has-text-align-left" data-align="left">맞힌 사람</th><th class="has-text-align-left" data-align="left">정답 비율</th></tr></thead><tbody><tr><td class="has-text-align-left" data-align="left">1 초</td><td class="has-text-align-left" data-align="left">256 MB</td><td class="has-text-align-left" data-align="left">61021</td><td class="has-text-align-left" data-align="left">15491</td><td class="has-text-align-left" data-align="left">10492</td><td class="has-text-align-left" data-align="left">24.539%</td></tr></tbody></table></figure>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">문제</h2>



<p class="has-medium-font-size">방향성이 없는 그래프가 주어진다. 세준이는 1번 정점에서 N번 정점으로 최단 거리로 이동하려고 한다. </p>



<p class="has-medium-font-size">또한 세준이는 두 가지 조건을 만족하면서 이동하는 특정한 최단 경로를 구하고 싶은데, </p>



<p class="has-medium-font-size">그것은 바로 임의로 주어진 두 정점은 반드시 통과해야 한다는 것이다.</p>



<p class="has-medium-font-size">세준이는 한번 이동했던 정점은 물론, 한번 이동했던 간선도 다시 이동할 수 있다. </p>



<p class="has-medium-font-size">하지만 반드시 최단 경로로 이동해야 한다는 사실에 주의하라. </p>



<p class="has-medium-font-size">1번 정점에서 N번 정점으로 이동할 때, 주어진 두 정점을 반드시 거치면서 최단 경로로 이동하는 프로그램을 작성하시오.</p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">입력</h2>



<p class="has-medium-font-size">첫째 줄에 정점의 개수 N과 간선의 개수 E가 주어진다. (2 ≤ N ≤ 800, 0 ≤ E ≤ 200,000) </p>



<p class="has-medium-font-size">둘째 줄부터 E개의 줄에 걸쳐서 세 개의 정수 a, b, c가 주어지는데, </p>



<p class="has-medium-font-size">a번 정점에서 b번 정점까지 양방향 길이 존재하며, 그 거리가 c라는 뜻이다. (1 ≤ c ≤ 1,000) </p>



<p class="has-medium-font-size">다음 줄에는 반드시 거쳐야 하는 두 개의 서로 다른 정점 번호 v<sub>1</sub>과 v<sub>2</sub>가 주어진다. (v<sub>1</sub>&nbsp;≠ v<sub>2</sub>, v<sub>1</sub>&nbsp;≠ N,&nbsp;v<sub>2</sub>&nbsp;≠ 1) </p>



<p class="has-medium-font-size">임의의 두 정점 u와 v사이에는 간선이 최대 1개 존재한다.</p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">출력</h2>



<p class="has-medium-font-size">첫째 줄에 두 개의 정점을 지나는 최단 경로의 길이를 출력한다. </p>



<p class="has-medium-font-size">그러한 경로가 없을 때에는 -1을 출력한다.</p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 입력 1</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">4 6
1 2 3
2 3 3
3 4 1
1 3 5
2 4 5
1 4 4
2 3</pre>



<div style="height:31px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 출력 1</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">7</pre>



<div style="height:60px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 입력 A</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">2 0
1 2</pre>



<div style="height:31px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 출력 A</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">-1</pre>



<div style="height:60px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 입력 B</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">4 6
1 2 10
2 3 10
4 3 1
4 1 1
1 3 10
4 2 2
2 3</pre>



<div style="height:31px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 출력 B</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">7</pre>



<div style="height:60px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 입력 C</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">800 3
1 800 4
800 2 2
3 800 6
2 3</pre>



<div style="height:31px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 출력 C</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">20</pre>



<div style="height:60px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 입력 D</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">800 3
2 1 2
1 800 3
800 4 1
2 4</pre>



<div style="height:31px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 출력 D</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">9</pre>



<div style="height:60px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 입력 E</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">4 2
1 2 1
2 3 1
2 3</pre>



<div style="height:31px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 출력 E</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">-1</pre>



<div style="height:60px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">출처</h2>



<ul class="wp-block-list">
<li>데이터를 추가한 사람:&nbsp;<a href="https://www.acmicpc.net/user/alex9801" target="_blank" rel="noreferrer noopener">alex9801</a>,&nbsp;<a href="https://www.acmicpc.net/user/paldad" target="_blank" rel="noreferrer noopener">paldad</a>,&nbsp;<a href="https://www.acmicpc.net/user/rhdqor213" target="_blank" rel="noreferrer noopener">rhdqor213</a></li>



<li>문제의 오타를 찾은 사람:&nbsp;<a href="https://www.acmicpc.net/user/ZZangZZang" target="_blank" rel="noreferrer noopener">ZZangZZang</a></li>
</ul>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">알고리즘 분류</h2>



<ul class="wp-block-list">
<li><a href="https://www.acmicpc.net/problem/tag/7" target="_blank" rel="noreferrer noopener">그래프 이론</a></li>



<li><a href="https://www.acmicpc.net/problem/tag/22" target="_blank" rel="noreferrer noopener">데이크스트라</a></li>
</ul>



<hr class="wp-block-separator has-alpha-channel-opacity is-style-wide" style="margin-top:var(--wp--preset--spacing--80);margin-bottom:var(--wp--preset--spacing--80)"/>



<figure class="wp-block-image size-large is-resized"><img decoding="async" src="https://lycos7560.com/wp-content/uploads/KakaoTalk_20230124_165824810-1352x3000.jpg" alt="" class="wp-image-5375" width="1352" height="3000" srcset="https://lycos7560.com/wp-content/uploads/KakaoTalk_20230124_165824810-1352x3000.jpg 1352w, https://lycos7560.com/wp-content/uploads/KakaoTalk_20230124_165824810-135x300.jpg 135w, https://lycos7560.com/wp-content/uploads/KakaoTalk_20230124_165824810-768x1704.jpg 768w, https://lycos7560.com/wp-content/uploads/KakaoTalk_20230124_165824810-692x1536.jpg 692w, https://lycos7560.com/wp-content/uploads/KakaoTalk_20230124_165824810-923x2048.jpg 923w, https://lycos7560.com/wp-content/uploads/KakaoTalk_20230124_165824810-scaled.jpg 865w" sizes="(max-width: 1352px) 100vw, 1352px" /></figure>



<div style="height:54px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">통과된 코드 1 (비효율)</h2>



<p class="has-medium-font-size">모든 배열을 선언해서 메모리 부분에서 꽝</p>



<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>



<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">#include &lt;iostream>
#include &lt;vector>
#include &lt;queue>

using namespace std;

constexpr int MAXN = 8001;
constexpr int INF = INT32_MAX;

// Dijkstra 알고리즘에 사용할 우선순위 큐
priority_queue&lt;pair&lt;int, int>> myPQ;

///*
//각 노드에 연결되어 있는 노드에 대한 정보를 담는 벡터
//a번 노드에서 b번 노드로 가는 비용이 c라는 의미
//graph[a].push_back((make_pair(B, C));
//*/
vector&lt;pair&lt;int, int>> graph[MAXN];

// dist[i][j]  
// => i 에서 j 까지의 최단거리 (임시 노드)
int disArr[MAXN][MAXN];

// N : 정점의 개수, E : 간선의 개수
// U: 현재 노드, V : 이웃 노드, dist : 거리
// V1, V2 필수 정점
int N, E, U, V, dist, V1, V2;

int result = 0;

void Dijkstra(int start)
{
    // 임시배열 초기화
    for (int i = 1; i &lt;= N; i++) disArr[start][i] = INF;
    // 우선순위 큐에 삽입.
    myPQ.push({ 0, start }); // &lt; first : 거리 , second : 노드 인덱스 >

    disArr[start][start] = 0;

    while (!myPQ.empty()) {
        // -를 붙이는 이유는 우선순위 큐를 이용하여 정렬하기 위함이다.
        // (최소힙으로 구현)
        int nCost = -myPQ.top().first;
        int now = myPQ.top().second;
        myPQ.pop();
        // 해당 노드에서 연결된 모든 경로를 확인
        for (int i = 0; i &lt; graph[now].size(); i++) {
            // 0이라면 길이 없다는 의미 continue
            if (graph[now][i].second == 0) continue;
            // disSum = 임시 노드 + 현재 노드에서 i로가는 비용
            int disSum = nCost + graph[now][i].second;
            // 비용이 더 작다면 최단경로 테이블 값을 갱신.
            if (disSum &lt; disArr[start][graph[now][i].first]) {
                // 임시 노드 업데이트
                disArr[start][graph[now][i].first] = disSum;
                // 우선순위 큐에 (거리, 노드 인덱스) 푸시
                myPQ.push(make_pair(-disSum, graph[now][i].first));
            }
        }
    }
}

int main()
{
    ios_base::sync_with_stdio(false); // scanf와 동기화를 비활성화
    // cin.tie(null); 코드는 cin과 cout의 묶음을 풀어줍니다.
    cin.tie(NULL);
    cout.tie(NULL);

    // N 정점의 개수, E 간선의 개수를 입력받는다.
    cin >> N >> E;

    for (int i = 0; i &lt; E; i++) {
        cin >> U >> V >> dist;
        graph[U].push_back(make_pair(V, dist));
        graph[V].push_back(make_pair(U, dist)); // 역방향
    }

    // V1, V2 필수 정점을 입력받는다.
    cin >> V1 >> V2;

    Dijkstra(1);
    Dijkstra(V1);
    Dijkstra(V2);
    Dijkstra(N);

    // 1 -> V1 -> V2 -> N 과 1 -> V2 -> V1 -> N 을 비교
    result = min(disArr[1][V2] + disArr[V2][V1] + disArr[V1][N], disArr[1][V1] + disArr[V1][V2] + disArr[V2][N]);

    // 1 -> N 으로 가는 경우 또는 
    if (disArr[1][N] == INF || disArr[V1][V2] == INF) cout &lt;&lt; -1;
    else cout &lt;&lt; result;




    return 0;
}</pre>



<div style="height:37px" aria-hidden="true" class="wp-block-spacer"></div>



<figure class="wp-block-image size-full"><img decoding="async" width="1032" height="182" src="https://lycos7560.com/wp-content/uploads/image-1039.png" alt="" class="wp-image-5369" srcset="https://lycos7560.com/wp-content/uploads/image-1039.png 1032w, https://lycos7560.com/wp-content/uploads/image-1039-300x53.png 300w, https://lycos7560.com/wp-content/uploads/image-1039-768x135.png 768w" sizes="(max-width: 1032px) 100vw, 1032px" /></figure>



<div style="height:39px" aria-hidden="true" class="wp-block-spacer"></div>



<p class="has-medium-font-size">메모리 제한이 256MB인데 255.968MB&#8230;</p>



<p class="has-medium-font-size">동적으로 만들고 싶지 않아서 나올 수 있는 경우의 배열을 전부 선언해서 그런 것 같다.</p>



<p class="has-medium-font-size">1 -&gt; V1 -&gt; V2 -&gt; N 과 1 -&gt; V2 -&gt; V1 -&gt; N 을 비교를 생각 못해서 계속 틀렸다.</p>



<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">통과된 코드 2</h2>



<p class="has-medium-font-size">생각해보니 N에서는 Dijkstra 알고리즘을 사용할 필요가 없다. =&gt; 제거 (약간의 속도 상승)</p>



<figure class="wp-block-image size-full"><img decoding="async" width="352" height="113" src="https://lycos7560.com/wp-content/uploads/image-1042.png" alt="" class="wp-image-5383" srcset="https://lycos7560.com/wp-content/uploads/image-1042.png 352w, https://lycos7560.com/wp-content/uploads/image-1042-300x96.png 300w" sizes="(max-width: 352px) 100vw, 352px" /></figure>



<p class="has-medium-font-size">배열로 모든 간선을 미리 선언해두면 낭비 =&gt; 불필요한 배열 제거 (이곳에서 큰 차이가 난다.)</p>



<figure class="wp-block-image size-full"><img decoding="async" width="545" height="113" src="https://lycos7560.com/wp-content/uploads/image-1041.png" alt="" class="wp-image-5382" srcset="https://lycos7560.com/wp-content/uploads/image-1041.png 545w, https://lycos7560.com/wp-content/uploads/image-1041-300x62.png 300w" sizes="(max-width: 545px) 100vw, 545px" /></figure>



<p class="has-medium-font-size">위의 변경사항으로 함수도 수정</p>



<figure class="wp-block-image size-full"><img decoding="async" width="706" height="587" src="https://lycos7560.com/wp-content/uploads/image-1043.png" alt="" class="wp-image-5384" srcset="https://lycos7560.com/wp-content/uploads/image-1043.png 706w, https://lycos7560.com/wp-content/uploads/image-1043-300x249.png 300w" sizes="(max-width: 706px) 100vw, 706px" /></figure>



<div style="height:37px" aria-hidden="true" class="wp-block-spacer"></div>



<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">#include &lt;iostream>
#include &lt;vector>
#include &lt;queue>

using namespace std;

constexpr int MAXN = 8001;
constexpr int INF = INT32_MAX;

// Dijkstra 알고리즘에 사용할 우선순위 큐
priority_queue&lt;pair&lt;int, int>> myPQ;

///*
//각 노드에 연결되어 있는 노드에 대한 정보를 담는 벡터
//a번 노드에서 b번 노드로 가는 비용이 c라는 의미
//graph[a].push_back((make_pair(B, C));
//*/
vector&lt;pair&lt;int, int>> graph[MAXN];

// 임시노드 dist[i][j] 
// i = 0  시작
// 1, 2 경유
int disArr[3][MAXN];

// N : 정점의 개수, E : 간선의 개수
// U: 현재 노드, V : 이웃 노드, dist : 거리
// V1, V2 필수 정점
int N, E, U, V, dist, V1, V2;

int result = 0;

void Dijkstra(int start, int number)
{
    // 임시배열 초기화
    for (int i = 1; i &lt;= N; i++) disArr[number][i] = INF;
    // 우선순위 큐에 삽입.
    myPQ.push({ 0, start }); // &lt; first : 거리 , second : 노드 인덱스 >

    disArr[number][start] = 0;

    while (!myPQ.empty()) {
        // -를 붙이는 이유는 우선순위 큐를 이용하여 정렬하기 위함이다.
        // (최소힙으로 구현)
        int nCost = -myPQ.top().first;
        int now = myPQ.top().second;
        myPQ.pop();
        // 해당 노드에서 연결된 모든 경로를 확인
        for (int i = 0; i &lt; graph[now].size(); i++) {
            // 0이라면 길이 없다는 의미 continue
            if (graph[now][i].second == 0) continue;
            // disSum = 임시 노드 + 현재 노드에서 i로가는 비용
            int disSum = nCost + graph[now][i].second;
            // 비용이 더 작다면 최단경로 테이블 값을 갱신.
            if (disSum &lt; disArr[number][graph[now][i].first]) {
                // 임시 노드 업데이트
                disArr[number][graph[now][i].first] = disSum;
                // 우선순위 큐에 (거리, 노드 인덱스) 푸시
                myPQ.push(make_pair(-disSum, graph[now][i].first));
            }
        }
    }
}

int main()
{
    ios_base::sync_with_stdio(false); // scanf와 동기화를 비활성화
    // cin.tie(null); 코드는 cin과 cout의 묶음을 풀어줍니다.
    cin.tie(NULL);
    cout.tie(NULL);

    // N 정점의 개수, E 간선의 개수를 입력받는다.
    cin >> N >> E;

    for (int i = 0; i &lt; E; i++) {
        cin >> U >> V >> dist;
        graph[U].push_back(make_pair(V, dist));
        graph[V].push_back(make_pair(U, dist)); // 역방향
    }

    // V1, V2 필수 정점을 입력받는다.
    cin >> V1 >> V2;

    Dijkstra(1, 0);
    Dijkstra(V1, 1);
    Dijkstra(V2, 2);

    // 1 -> V1 -> V2 -> N 과 1 -> V2 -> V1 -> N 을 비교
    result = min(disArr[0][V2] + disArr[2][V1] + disArr[1][N], disArr[0][V1] + disArr[1][V2] + disArr[2][N]);

    // 1 -> N 으로 가는 경우 또는 
    if (disArr[0][N] == INF || disArr[1][V2] == INF) cout &lt;&lt; -1;
    else cout &lt;&lt; result;

    return 0;
}</pre>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<figure class="wp-block-image size-full"><img decoding="async" width="1037" height="142" src="https://lycos7560.com/wp-content/uploads/image-1044.png" alt="" class="wp-image-5385" srcset="https://lycos7560.com/wp-content/uploads/image-1044.png 1037w, https://lycos7560.com/wp-content/uploads/image-1044-300x41.png 300w, https://lycos7560.com/wp-content/uploads/image-1044-768x105.png 768w" sizes="(max-width: 1037px) 100vw, 1037px" /></figure>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<p class="has-medium-font-size">아주 성공적이다 . </p>



<p class="has-medium-font-size">엄청난 메모리 차이를 보여준다.</p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-5698326622209671" crossorigin="anonymous"></script>
<ins class="adsbygoogle" style="display:block" data-ad-format="autorelaxed" data-ad-client="ca-pub-5698326622209671" data-ad-slot="4245812909"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-1504%eb%b2%88-%ed%8a%b9%ec%a0%95%ed%95%9c-%ec%b5%9c%eb%8b%a8-%ea%b2%bd%eb%a1%9c-c-dijkstra-%ec%b6%94%ea%b0%80-%eb%b0%98%eb%a1%80-baekjoon/5366/">백준 1504번 (특정한 최단 경로, C++, Dijkstra) / 추가 반례 [BAEKJOON]</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-1504%eb%b2%88-%ed%8a%b9%ec%a0%95%ed%95%9c-%ec%b5%9c%eb%8b%a8-%ea%b2%bd%eb%a1%9c-c-dijkstra-%ec%b6%94%ea%b0%80-%eb%b0%98%eb%a1%80-baekjoon/5366/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
		<item>
		<title>백준 1238번 (파티, C++, Dijkstra) [BAEKJOON]</title>
		<link>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-1238%eb%b2%88-%ed%8c%8c%ed%8b%b0-c-dijkstra-baekjoon/5357/</link>
					<comments>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-1238%eb%b2%88-%ed%8c%8c%ed%8b%b0-c-dijkstra-baekjoon/5357/#respond</comments>
		
		<dc:creator><![CDATA[lycos7560]]></dc:creator>
		<pubDate>Tue, 24 Jan 2023 04:53:36 +0000</pubDate>
				<category><![CDATA[BaekjoonOnlineJudge]]></category>
		<category><![CDATA[C++/CPP]]></category>
		<category><![CDATA[1238]]></category>
		<category><![CDATA[1238번]]></category>
		<category><![CDATA[algorithm]]></category>
		<category><![CDATA[Baekjoon]]></category>
		<category><![CDATA[BFS]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[cin.tie(NULL)]]></category>
		<category><![CDATA[cout.tie(NULL)]]></category>
		<category><![CDATA[cpp]]></category>
		<category><![CDATA[Dijkstra]]></category>
		<category><![CDATA[DP]]></category>
		<category><![CDATA[DynamicProgramming]]></category>
		<category><![CDATA[priority_queue]]></category>
		<category><![CDATA[PriorityQueue]]></category>
		<category><![CDATA[Queue]]></category>
		<category><![CDATA[study]]></category>
		<category><![CDATA[공부]]></category>
		<category><![CDATA[그래프 이론]]></category>
		<category><![CDATA[그래프 탐새ㅔㄱ]]></category>
		<category><![CDATA[그래프 탐색]]></category>
		<category><![CDATA[기본]]></category>
		<category><![CDATA[기초]]></category>
		<category><![CDATA[길찾기]]></category>
		<category><![CDATA[너비]]></category>
		<category><![CDATA[너비 우선 탐색]]></category>
		<category><![CDATA[다이나믹 프로그래밍]]></category>
		<category><![CDATA[다익스트라]]></category>
		<category><![CDATA[데이크스트라]]></category>
		<category><![CDATA[백준]]></category>
		<category><![CDATA[백준 1238]]></category>
		<category><![CDATA[백준 1238번]]></category>
		<category><![CDATA[시간]]></category>
		<category><![CDATA[알고리즘]]></category>
		<category><![CDATA[지름길]]></category>
		<category><![CDATA[컴파일 에러]]></category>
		<category><![CDATA[코딩테스트]]></category>
		<category><![CDATA[코테]]></category>
		<category><![CDATA[파티]]></category>
		<guid isPermaLink="false">https://lycos7560.com/?p=5357</guid>

					<description><![CDATA[<p>백준(BAEKJOON) 1238번 '파티' 문제에 대한 글입니다. Dijkstra 알고리즘을 이용하여 해결하였습니다. (This is an article about the 'party' problem in BAEKJOON 1238. It was solved using Dijkstra algorithm.)</p>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-1238%eb%b2%88-%ed%8c%8c%ed%8b%b0-c-dijkstra-baekjoon/5357/">백준 1238번 (파티, C++, Dijkstra) [BAEKJOON]</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-5698326622209671" crossorigin="anonymous"></script>
<!-- HorizontalAD -->
<ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-5698326622209671" data-ad-slot="6908948342" data-ad-format="auto" data-full-width-responsive="true"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>



<div style="height:62px" aria-hidden="true" class="wp-block-spacer"></div>



<h1 class="wp-block-heading">파티</h1>



<p class="has-medium-font-size"><a href="https://www.acmicpc.net/problem/1238" target="_blank" rel="noreferrer noopener">https://www.acmicpc.net/problem/1238</a></p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<figure id="problem-info" class="wp-block-table"><table class="has-fixed-layout"><thead><tr><th class="has-text-align-left" data-align="left">시간 제한</th><th class="has-text-align-left" data-align="left">메모리 제한</th><th class="has-text-align-left" data-align="left">제출</th><th class="has-text-align-left" data-align="left">정답</th><th class="has-text-align-left" data-align="left">맞힌 사람</th><th class="has-text-align-left" data-align="left">정답 비율</th></tr></thead><tbody><tr><td class="has-text-align-left" data-align="left">1 초</td><td class="has-text-align-left" data-align="left">128 MB</td><td class="has-text-align-left" data-align="left">32979</td><td class="has-text-align-left" data-align="left">16381</td><td class="has-text-align-left" data-align="left">10904</td><td class="has-text-align-left" data-align="left">47.728%</td></tr></tbody></table></figure>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">문제</h2>



<p class="has-medium-font-size">N개의 숫자로 구분된 각각의 마을에 한 명의 학생이 살고 있다.</p>



<p class="has-medium-font-size">어느 날 이 N명의 학생이 X (1 ≤&nbsp;X ≤ N)번 마을에 모여서 파티를 벌이기로 했다. </p>



<p class="has-medium-font-size">이 마을 사이에는 총 M개의 단방향 도로들이 있고 i번째 길을 지나는데 T<sub>i</sub>(1 ≤ T<sub>i</sub>&nbsp;≤ 100)의 시간을 소비한다.</p>



<p class="has-medium-font-size">각각의 학생들은 파티에 참석하기 위해 걸어가서 다시 그들의 마을로 돌아와야 한다. </p>



<p class="has-medium-font-size">하지만 이 학생들은 워낙 게을러서 최단 시간에 오고 가기를 원한다.</p>



<p class="has-medium-font-size">이 도로들은 단방향이기 때문에 아마 그들이 오고 가는 길이 다를지도 모른다. </p>



<p class="has-medium-font-size">N명의 학생들 중 오고 가는데 가장 많은 시간을 소비하는 학생은 누구일지 구하여라.</p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">입력</h2>



<p class="has-medium-font-size">첫째 줄에 N(1 ≤&nbsp;N ≤ 1,000), M(1 ≤ M ≤ 10,000), X가 공백으로 구분되어 입력된다. </p>



<p class="has-medium-font-size">두 번째 줄부터 M+1번째 줄까지 i번째 도로의 시작점, 끝점, 그리고 이 도로를 지나는데 필요한 소요시간 T<sub>i</sub>가 들어온다. </p>



<p class="has-medium-font-size">시작점과 끝점이 같은 도로는 없으며, 시작점과 한 도시 A에서 다른 도시 B로 가는 도로의 개수는 최대 1개이다.</p>



<p class="has-medium-font-size">모든 학생들은 집에서 X에 갈수 있고, X에서 집으로 돌아올 수 있는 데이터만 입력으로 주어진다.</p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">출력</h2>



<p class="has-medium-font-size">첫 번째 줄에 N명의 학생들 중 오고 가는데 가장 오래 걸리는 학생의 소요시간을 출력한다.</p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 입력 1</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">4 8 2
1 2 4
1 3 2
1 4 7
2 1 1
2 3 5
3 1 2
3 4 4
4 2 3</pre>



<div style="height:31px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 출력 1</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">10</pre>



<div style="height:60px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">출처</h2>



<p><a href="https://www.acmicpc.net/category/2" target="_blank" rel="noreferrer noopener">Olympiad</a>&nbsp;&gt;&nbsp;<a href="https://www.acmicpc.net/category/106" target="_blank" rel="noreferrer noopener">USA Computing Olympiad</a>&nbsp;&gt;&nbsp;<a href="https://www.acmicpc.net/category/155" target="_blank" rel="noreferrer noopener">2006-2007 Season</a>&nbsp;&gt;&nbsp;<a href="https://www.acmicpc.net/category/160" target="_blank" rel="noreferrer noopener">USACO February 2007 Contest</a>&nbsp;&gt;&nbsp;<a href="https://www.acmicpc.net/category/detail/704" target="_blank" rel="noreferrer noopener">Silver</a>&nbsp;3번</p>



<ul class="wp-block-list">
<li>문제를 번역한 사람:&nbsp;<a href="https://www.acmicpc.net/user/author6" target="_blank" rel="noreferrer noopener">author6</a></li>



<li>빠진 조건을 찾은 사람:&nbsp;<a href="https://www.acmicpc.net/user/his130" target="_blank" rel="noreferrer noopener">his130</a></li>
</ul>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">알고리즘 분류</h2>



<ul class="wp-block-list">
<li><a href="https://www.acmicpc.net/problem/tag/7" target="_blank" rel="noreferrer noopener">그래프 이론</a></li>



<li><a href="https://www.acmicpc.net/problem/tag/22" target="_blank" rel="noreferrer noopener">데이크스트라</a></li>
</ul>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<hr class="wp-block-separator has-alpha-channel-opacity is-style-wide" style="margin-top:var(--wp--preset--spacing--80);margin-bottom:var(--wp--preset--spacing--80)"/>



<figure class="wp-block-image size-full"><img decoding="async" width="1600" height="1602" src="https://lycos7560.com/wp-content/uploads/KakaoTalk_20230124_125736933.jpg" alt="" class="wp-image-5360" srcset="https://lycos7560.com/wp-content/uploads/KakaoTalk_20230124_125736933.jpg 1600w, https://lycos7560.com/wp-content/uploads/KakaoTalk_20230124_125736933-300x300.jpg 300w, https://lycos7560.com/wp-content/uploads/KakaoTalk_20230124_125736933-150x150.jpg 150w, https://lycos7560.com/wp-content/uploads/KakaoTalk_20230124_125736933-768x769.jpg 768w, https://lycos7560.com/wp-content/uploads/KakaoTalk_20230124_125736933-1534x1536.jpg 1534w" sizes="(max-width: 1600px) 100vw, 1600px" /></figure>



<div style="height:77px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">통과된 코드</h2>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">#include &lt;iostream>
#include &lt;vector>
#include &lt;queue>

using namespace std;

constexpr int MAXN = 1001;
constexpr int INF = INT32_MAX;

// N : 마을의 수, M : 단방향 도로의 개수, X : 파티의 위치
int N, M, X;

// U : 현재 노드, V : 이웃 노드, dist : 거리
int U, V, dist;

int cnt = 1;

bool debug = false;

int result[MAXN];

/*
각 노드에 연결되어 있는 노드에 대한 정보를 담는 벡터
a번 노드에서 b번 노드로 가는 비용이 c라는 의미
graph[a].push_back((make_pair(B, C));
*/
vector&lt;pair&lt;int, int>> graph[MAXN];

// dist[i][j]  => i 에서 j 까지의 최단거리 (임시 노드)
int disArr[MAXN][MAXN];

// 우선 순위 큐는
// 임시 거리를 가진 노드를 효율적으로 선택하는 데 사용
// &lt;거리, 노드 인덱스>
priority_queue&lt;pair&lt;int, int>> myPQ;

int main()
{
    ios_base::sync_with_stdio(false); // scanf와 동기화를 비활성화
    // cin.tie(null); 코드는 cin과 cout의 묶음을 풀어줍니다.
    cin.tie(NULL);
    cout.tie(NULL);

    cin >> N >> M >> X;
    
    // 간선 입력받기
    for (int i = 0; i &lt; M; i++) {
        cin >> U >> V >> dist;
        graph[U].push_back(make_pair(V, dist));
    } 

    while (cnt &lt;= N) {

        // 임시배열 초기화
        for (int i = 1; i &lt;= N; i++)    disArr[cnt][i] = INF;
        
        // 우선순위 큐에 삽입.
        myPQ.push({ 0, cnt }); // &lt; first : 거리 , second : 노드 인덱스 >
        disArr[cnt][cnt] = 0; 

        while (!myPQ.empty()) {
            // -를 붙이는 이유는 우선순위 큐를 이용하여 정렬하기 위함이다.
            // (최소힙으로 구현)
            int nCost = -myPQ.top().first;
            int now = myPQ.top().second;
            myPQ.pop();
            // 해당 노드에서 연결된 모든 경로를 확인
            for (int i = 0; i &lt; graph[now].size(); i++) {
                // 0이라면 길이 없다는 의미 continue
                if (graph[now][i].second == 0) continue;
                // disSum = 임시 노드 + 현재 노드에서 i로가는 비용
                int disSum = nCost + graph[now][i].second;
                // 비용이 더 작다면 최단경로 테이블 값을 갱신.
                if (disSum &lt; disArr[cnt][graph[now][i].first]) {
                    // 임시 노드 업데이트
                    disArr[cnt][graph[now][i].first] = disSum;
                    // 우선순위 큐에 (거리, 노드 인덱스) 푸시
                    myPQ.push(make_pair(-disSum, graph[now][i].first));
                }
            }
        }

        cnt++;
    }

    if (debug) {
        for (int j = 1; j &lt;= N; j++) {
            cout &lt;&lt; "\n";
            for (int i = 1; i &lt;= N; i++) {
                cout &lt;&lt; j &lt;&lt; " -> " &lt;&lt; i &lt;&lt; " 의 최소 거리 " &lt;&lt; disArr[j][i] &lt;&lt; " \n";
            }
            cout &lt;&lt; "\n";
        }
    }


    for (int i = 1; i &lt;= N; i++) {
        result[i] = disArr[X][i] + disArr[i][X];
        result[0] = max(result[i], result[0]);
    }

    cout &lt;&lt; result[0];

	return 0;
}</pre>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<figure class="wp-block-image size-full"><img decoding="async" width="909" height="144" src="https://lycos7560.com/wp-content/uploads/image-1038.png" alt="" class="wp-image-5358" srcset="https://lycos7560.com/wp-content/uploads/image-1038.png 909w, https://lycos7560.com/wp-content/uploads/image-1038-300x48.png 300w, https://lycos7560.com/wp-content/uploads/image-1038-768x122.png 768w" sizes="(max-width: 909px) 100vw, 909px" /></figure>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<p class="has-medium-font-size">배열의 최댓값 MAXN을 101로 두어서 발생한 OutOfBounds 오류</p>



<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>



<p class="has-medium-font-size">추가적인 아이디어로는</p>



<p class="has-medium-font-size">정점들의 정보를 입력 받을 때 <mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-ast-global-color-0-color"><strong>역방향 간선</strong></mark>을 따로 입력을 받으면</p>



<p class="has-medium-font-size">역방향으로 입력을 받으면 각 <mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-ast-global-color-8-color"><strong>정점들(A)에서 X로 가는 최단거리를 X-&gt;A 최단거리</strong></mark>로 바꿀 수 있다.</p>



<p class="has-medium-font-size">다익스트라 한 번이면 각 정점들에서 X로 가는 최단거리배열을 구할 수 있다는 것이다. (시간 차이가 엄청남)</p>



<p><a href="https://hyeo-noo.tistory.com/138" target="_blank" rel="noreferrer noopener">https://hyeo-noo.tistory.com/138</a> &lt;- 참고</p>



<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>



<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-5698326622209671" crossorigin="anonymous"></script>
<ins class="adsbygoogle" style="display:block" data-ad-format="autorelaxed" data-ad-client="ca-pub-5698326622209671" data-ad-slot="4245812909"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-1238%eb%b2%88-%ed%8c%8c%ed%8b%b0-c-dijkstra-baekjoon/5357/">백준 1238번 (파티, C++, Dijkstra) [BAEKJOON]</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-1238%eb%b2%88-%ed%8c%8c%ed%8b%b0-c-dijkstra-baekjoon/5357/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
