<?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>DynamicProgramming Archives - 어제와 내일의 나 그 사이의 이야기</title>
	<atom:link href="https://lycos7560.com/tag/dynamicprogramming/feed/" rel="self" type="application/rss+xml" />
	<link></link>
	<description>생각의 흐름을 타고 다니며 만드는 블로그</description>
	<lastBuildDate>Fri, 15 Mar 2024 15:55:41 +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>DynamicProgramming Archives - 어제와 내일의 나 그 사이의 이야기</title>
	<link></link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>백준 11053번 (가장 긴 증가하는 부분 수열, C++) [BAEKJOON]</title>
		<link>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-11053%eb%b2%88-%ea%b0%80%ec%9e%a5-%ea%b8%b4-%ec%a6%9d%ea%b0%80%ed%95%98%eb%8a%94-%eb%b6%80%eb%b6%84-%ec%88%98%ec%97%b4-c-baekjoon/36512/</link>
					<comments>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-11053%eb%b2%88-%ea%b0%80%ec%9e%a5-%ea%b8%b4-%ec%a6%9d%ea%b0%80%ed%95%98%eb%8a%94-%eb%b6%80%eb%b6%84-%ec%88%98%ec%97%b4-c-baekjoon/36512/#respond</comments>
		
		<dc:creator><![CDATA[lycos7560]]></dc:creator>
		<pubDate>Wed, 16 Aug 2023 02:53:36 +0000</pubDate>
				<category><![CDATA[BaekjoonOnlineJudge]]></category>
		<category><![CDATA[C++/CPP]]></category>
		<category><![CDATA[11053]]></category>
		<category><![CDATA[11053번]]></category>
		<category><![CDATA[Baekjoon]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[cpp]]></category>
		<category><![CDATA[DP]]></category>
		<category><![CDATA[DynamicProgramming]]></category>
		<category><![CDATA[Sliver]]></category>
		<category><![CDATA[Sliver2]]></category>
		<category><![CDATA[study]]></category>
		<category><![CDATA[가장 긴 증가하는 부분 수열]]></category>
		<category><![CDATA[공부]]></category>
		<category><![CDATA[기초]]></category>
		<category><![CDATA[다이나믹 프로그래밍]]></category>
		<category><![CDATA[백준]]></category>
		<category><![CDATA[백준 11053]]></category>
		<category><![CDATA[백준 11053번]]></category>
		<category><![CDATA[실버]]></category>
		<category><![CDATA[실버2]]></category>
		<category><![CDATA[알고리즘]]></category>
		<category><![CDATA[코딩테스트]]></category>
		<category><![CDATA[코테]]></category>
		<guid isPermaLink="false">https://lycos7560.com/?p=36512</guid>

					<description><![CDATA[<p>백준 11053번 '가장 긴 증가하는 부분 수열' 문제에 대한 글입니다. DP 알고리즘을 이용하여 해결하였습니다. (This article is about Baekjoon Number 11053 "Longest Increasing Partial Sequence" problem. It was solved using DP algorithm.)</p>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-11053%eb%b2%88-%ea%b0%80%ec%9e%a5-%ea%b8%b4-%ec%a6%9d%ea%b0%80%ed%95%98%eb%8a%94-%eb%b6%80%eb%b6%84-%ec%88%98%ec%97%b4-c-baekjoon/36512/">백준 11053번 (가장 긴 증가하는 부분 수열, C++) [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>
<ins class="adsbygoogle"
     style="display:block; text-align:center;"
     data-ad-layout="in-article"
     data-ad-format="fluid"
     data-ad-client="ca-pub-5698326622209671"
     data-ad-slot="5078714126"></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-43345b7a      "
					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:100px" aria-hidden="true" class="wp-block-spacer"></div>



<h1 class="wp-block-heading">스티커 <img decoding="async" width="30" height="38" class="wp-image-37910" style="width: 30px;" src="https://lycos7560.com/wp-content/uploads/2024/03/Sliver_2.jpg" alt="" srcset="https://lycos7560.com/wp-content/uploads/2024/03/Sliver_2.jpg 400w, https://lycos7560.com/wp-content/uploads/2024/03/Sliver_2-234x300.jpg 234w" sizes="(max-width: 30px) 100vw, 30px" /></h1>



<p><a href="https://www.acmicpc.net/problem/11053" target="_blank" rel="noreferrer noopener">https://www.acmicpc.net/problem/11053</a></p>



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



<h3 class="wp-block-heading has-large-font-size">문제</h3>



<p>수열 A가 주어졌을 때, 가장 긴 증가하는&nbsp;부분&nbsp;수열을 구하는 프로그램을 작성하시오.</p>



<p>예를 들어, 수열 A = {10, 20, 10, 30, 20, 50} 인 경우에 가장 긴 증가하는 부분&nbsp;수열은&nbsp;A = {<strong>10</strong>,&nbsp;<strong>20</strong>, 10,&nbsp;<strong>30</strong>, 20,&nbsp;<strong>50</strong>} 이고, 길이는 4이다.</p>



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



<h3 class="wp-block-heading has-large-font-size">입력</h3>



<p>첫째 줄에 수열 A의 크기 N (1 ≤ N ≤ 1,000)이 주어진다.</p>



<p>둘째 줄에는 수열 A를 이루고 있는 A<sub>i</sub>가 주어진다. (1 ≤ A<sub>i</sub>&nbsp;≤ 1,000)</p>



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



<h3 class="wp-block-heading has-large-font-size">출력</h3>



<p>첫째 줄에 수열 A의 가장 긴 증가하는 부분 수열의 길이를 출력한다.</p>



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



<h3 class="wp-block-heading has-large-font-size">예제 입력 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="">6
10 20 10 30 20 50</pre>



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



<h3 class="wp-block-heading has-large-font-size">예제 출력 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="">4</pre>



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



<h3 class="wp-block-heading has-large-font-size">출처</h3>



<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/harinboy" target="_blank" rel="noreferrer noopener">harinboy</a></li>
</ul>



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



<h3 class="wp-block-heading has-large-font-size">비슷한 문제</h3>



<ul class="wp-block-list">
<li><a href="https://www.acmicpc.net/problem/11054" target="_blank" rel="noreferrer noopener">11054번. 가장 긴 바이토닉 부분 수열</a></li>



<li><a href="https://www.acmicpc.net/problem/11055" target="_blank" rel="noreferrer noopener">11055번. 가장 큰 증가하는 부분 수열</a></li>



<li><a href="https://www.acmicpc.net/problem/11722" target="_blank" rel="noreferrer noopener">11722번. 가장 긴 감소하는 부분 수열</a></li>



<li><a href="https://www.acmicpc.net/problem/12015" target="_blank" rel="noreferrer noopener">12015번. 가장 긴 증가하는 부분 수열 2</a></li>



<li><a href="https://www.acmicpc.net/problem/12738" target="_blank" rel="noreferrer noopener">12738번. 가장 긴 증가하는 부분 수열 3</a></li>



<li><a href="https://www.acmicpc.net/problem/14002" target="_blank" rel="noreferrer noopener">14002번. 가장 긴 증가하는 부분 수열 4</a></li>



<li><a href="https://www.acmicpc.net/problem/14003" target="_blank" rel="noreferrer noopener">14003번. 가장 긴 증가하는 부분 수열 5</a></li>
</ul>



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



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



<ul class="wp-block-list">
<li><a href="https://www.acmicpc.net/problem/tag/25" 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-full is-resized"><img fetchpriority="high" decoding="async" width="1310" height="1920" src="https://lycos7560.com/wp-content/uploads/2023/08/KakaoTalk_20230816_115045407.jpg" alt="" class="wp-image-36525" style="width:897px;height:1315px" srcset="https://lycos7560.com/wp-content/uploads/2023/08/KakaoTalk_20230816_115045407.jpg 1310w, https://lycos7560.com/wp-content/uploads/2023/08/KakaoTalk_20230816_115045407-205x300.jpg 205w, https://lycos7560.com/wp-content/uploads/2023/08/KakaoTalk_20230816_115045407-768x1126.jpg 768w, https://lycos7560.com/wp-content/uploads/2023/08/KakaoTalk_20230816_115045407-1048x1536.jpg 1048w" sizes="(max-width: 1310px) 100vw, 1310px" /></figure>



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



<h2 class="wp-block-heading has-large-font-size">통과된 코드</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>

using namespace std;

int _N, _Tables[1000], _DP[1000], _Res = 1;

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	cin >> _N;
	for (int i = 0; i &lt; _N; i++)
		cin >> _Tables[i];
	_DP[0] = 1;
	for (int i = 1; i &lt; _N; i++) {
		int _t = 1;
		for (int j = i - 1; j >= 0; j--)
			if (_Tables[i] > _Tables[j])
				_t = max(_t, _DP[j] + 1);
		_DP[i] = _t;
	}

	for (int i = 0; i &lt; _N; i++)
		_Res = max(_Res, _DP[i]);
	
	cout &lt;&lt; _Res;
	return 0;
}</pre>



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



<figure class="wp-block-image size-full"><img decoding="async" width="1061" height="84" src="https://lycos7560.com/wp-content/uploads/2023/08/image-11.png" alt="" class="wp-image-36521" srcset="https://lycos7560.com/wp-content/uploads/2023/08/image-11.png 1061w, https://lycos7560.com/wp-content/uploads/2023/08/image-11-300x24.png 300w, https://lycos7560.com/wp-content/uploads/2023/08/image-11-768x61.png 768w" sizes="(max-width: 1061px) 100vw, 1061px" /></figure>



<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="4386247858"></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-11053%eb%b2%88-%ea%b0%80%ec%9e%a5-%ea%b8%b4-%ec%a6%9d%ea%b0%80%ed%95%98%eb%8a%94-%eb%b6%80%eb%b6%84-%ec%88%98%ec%97%b4-c-baekjoon/36512/">백준 11053번 (가장 긴 증가하는 부분 수열, C++) [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-11053%eb%b2%88-%ea%b0%80%ec%9e%a5-%ea%b8%b4-%ec%a6%9d%ea%b0%80%ed%95%98%eb%8a%94-%eb%b6%80%eb%b6%84-%ec%88%98%ec%97%b4-c-baekjoon/36512/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>백준 4811번 (알약, C++) [BAEKJOON]</title>
		<link>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-4811%eb%b2%88-%ec%95%8c%ec%95%bd-c-baekjoon/36232/</link>
					<comments>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-4811%eb%b2%88-%ec%95%8c%ec%95%bd-c-baekjoon/36232/#respond</comments>
		
		<dc:creator><![CDATA[lycos7560]]></dc:creator>
		<pubDate>Thu, 20 Jul 2023 04:39:11 +0000</pubDate>
				<category><![CDATA[BaekjoonOnlineJudge]]></category>
		<category><![CDATA[C++/CPP]]></category>
		<category><![CDATA[4811]]></category>
		<category><![CDATA[4811번]]></category>
		<category><![CDATA[Baekjoon]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[cpp]]></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[백준 4811]]></category>
		<category><![CDATA[백준 4811번]]></category>
		<category><![CDATA[수학]]></category>
		<category><![CDATA[알고리즘]]></category>
		<category><![CDATA[알약]]></category>
		<category><![CDATA[코딩테스트]]></category>
		<category><![CDATA[코테]]></category>
		<guid isPermaLink="false">https://lycos7560.com/?p=36232</guid>

					<description><![CDATA[<p>백준 4811번 ' 알약 ' 문제에 대한 글입니다. 다이나믹 프로그래밍을 이용하여 해결하였습니다. (This is an article about the 'pill' problem of Baekjoon Number 4811. It was solved using dynamic programming.)</p>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-4811%eb%b2%88-%ec%95%8c%ec%95%bd-c-baekjoon/36232/">백준 4811번 (알약, C++) [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-355fab5d      "
					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><a href="https://www.acmicpc.net/problem/4811" target="_blank" rel="noreferrer noopener">https://www.acmicpc.net/problem/4811</a></p>



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



<figure 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">6484</td><td class="has-text-align-left" data-align="left">4179</td><td class="has-text-align-left" data-align="left">3230</td><td class="has-text-align-left" data-align="left">66.901%</td></tr></tbody></table></figure>



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



<h3 class="wp-block-heading has-large-font-size">문제</h3>



<p>70세 박종수 할아버지는 매일 매일 약 반알을 먹는다. </p>



<p>손녀 선영이는 종수 할아버지에게 약이 N개 담긴 병을 선물로 주었다.</p>



<p>첫째 날에 종수는 병에서 약 하나를 꺼낸다. </p>



<p>그 다음, 그 약을 반으로 쪼개서 한 조각은 먹고, 다른 조각은 다시 병에 넣는다.</p>



<p>다음 날부터 종수는 병에서 약을 하나 꺼낸다. (약은 한 조각 전체 일 수도 있고, 쪼갠 반 조각 일 수도 있다) </p>



<p>반 조각이라면 그 약을 먹고, 아니라면 반을 쪼개서 한 조각을 먹고, 다른 조각은 다시 병에 넣는다.</p>



<p>종수는 손녀에게 한 조각을 꺼낸 날에는 W를, 반 조각을 꺼낸 날에는 H 보낸다. </p>



<p>손녀는 할아버지에게 받은 문자를 종이에 기록해 놓는다. </p>



<p>총 2N일이 지나면 길이가 2N인 문자열이 만들어지게 된다. </p>



<p>이때, 가능한 서로 다른 문자열의 개수는 총 몇 개일까?</p>



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



<h3 class="wp-block-heading has-large-font-size">입력</h3>



<p>입력은 최대 1000개의 테스트 케이스로 이루어져 있다. </p>



<p>각 테스트 케이스는 한 줄이며, 병에 들어있는 약의 개수 N ≤ 30 가 주어진다.</p>



<p>입력의 마지막 줄에는 0이 하나 주어진다.</p>



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



<h3 class="wp-block-heading has-large-font-size">출력</h3>



<p>각 테스트 케이스에 대해서 가능한 문자열의 개수를 출력한다.</p>



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



<h3 class="wp-block-heading has-large-font-size">예제 입력 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="">6
1
4
2
3
30
0</pre>



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



<h3 class="wp-block-heading has-large-font-size">예제 출력 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="">132
1
14
2
5
3814986502092304</pre>



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



<h3 class="wp-block-heading has-large-font-size">출처</h3>



<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/8" target="_blank" rel="noreferrer noopener">North America</a>&nbsp;&gt;&nbsp;<a href="https://www.acmicpc.net/category/41" target="_blank" rel="noreferrer noopener">Rocky Mountain Regional</a>&nbsp;&gt;&nbsp;<a href="https://www.acmicpc.net/category/detail/182" target="_blank" rel="noreferrer noopener">2011 Rocky Mountain Regional Contest</a>&nbsp;E번</p>



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



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



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



<ul class="wp-block-list">
<li><a href="https://www.acmicpc.net/problem/tag/124" target="_blank" rel="noreferrer noopener">수학</a></li>



<li><a href="https://www.acmicpc.net/problem/tag/25" 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-full is-resized"><img decoding="async" src="https://lycos7560.com/wp-content/uploads/2023/07/image-44.png" alt="" class="wp-image-36252" width="734" height="788" srcset="https://lycos7560.com/wp-content/uploads/2023/07/image-44.png 978w, https://lycos7560.com/wp-content/uploads/2023/07/image-44-279x300.png 279w, https://lycos7560.com/wp-content/uploads/2023/07/image-44-768x825.png 768w" sizes="(max-width: 734px) 100vw, 734px" /></figure>



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



<h2 class="wp-block-heading has-large-font-size">통과된 코드</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>
using namespace std;
int _N;
long long int _DP[31][31];
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	for (int i = 0; i &lt;= 30; i++)
		for (int j = 0; j &lt;= 30; j++) {
			// 반쪽이 더 많은 경우는 없다.
			if (i &lt; j) break;
			if (j == 0) _DP[i][j] = 1;
			else _DP[i][j] = _DP[i - 1][j] + _DP[i][j - 1];
		}

	while (true) {
		cin >> _N;
		if (_N == 0) break;
		// 온전한 알약을 N번
		// 반쪽의 알약을 N번 => 다 먹었다.
		cout &lt;&lt; _DP[_N][_N] &lt;&lt; "\n";
	}

	return 0;
}</pre>



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



<figure class="wp-block-image size-full"><img decoding="async" width="1206" height="104" src="https://lycos7560.com/wp-content/uploads/2023/07/image-43.png" alt="" class="wp-image-36241" srcset="https://lycos7560.com/wp-content/uploads/2023/07/image-43.png 1206w, https://lycos7560.com/wp-content/uploads/2023/07/image-43-300x26.png 300w, https://lycos7560.com/wp-content/uploads/2023/07/image-43-768x66.png 768w" sizes="(max-width: 1206px) 100vw, 1206px" /></figure>



<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-4811%eb%b2%88-%ec%95%8c%ec%95%bd-c-baekjoon/36232/">백준 4811번 (알약, C++) [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-4811%eb%b2%88-%ec%95%8c%ec%95%bd-c-baekjoon/36232/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>백준 2293번 (동전 1, C++) [BAEKJOON]</title>
		<link>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-2293%eb%b2%88-%eb%8f%99%ec%a0%84-1-c-baekjoon/36200/</link>
					<comments>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-2293%eb%b2%88-%eb%8f%99%ec%a0%84-1-c-baekjoon/36200/#respond</comments>
		
		<dc:creator><![CDATA[lycos7560]]></dc:creator>
		<pubDate>Tue, 18 Jul 2023 01:32:16 +0000</pubDate>
				<category><![CDATA[BaekjoonOnlineJudge]]></category>
		<category><![CDATA[C++/CPP]]></category>
		<category><![CDATA[2293]]></category>
		<category><![CDATA[2293번]]></category>
		<category><![CDATA[Baekjoon]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[cpp]]></category>
		<category><![CDATA[DP]]></category>
		<category><![CDATA[DynamicProgramming]]></category>
		<category><![CDATA[study]]></category>
		<category><![CDATA[공부]]></category>
		<category><![CDATA[기초]]></category>
		<category><![CDATA[동전 1]]></category>
		<category><![CDATA[백준]]></category>
		<category><![CDATA[백준 2293]]></category>
		<category><![CDATA[백준 2293번]]></category>
		<category><![CDATA[알고리즘]]></category>
		<category><![CDATA[코딩테스트]]></category>
		<category><![CDATA[코테]]></category>
		<guid isPermaLink="false">https://lycos7560.com/?p=36200</guid>

					<description><![CDATA[<p>백준 2293번 '동전 1' 문제에 대한 글입니다.<br />
다이나믹 프로그래밍을 이용하여 해결하였습니다. (<br />
This is a blog post about the problem of 'Coin 1' in Baekjun 2293. I solved the problem using dynamic programming. )</p>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-2293%eb%b2%88-%eb%8f%99%ec%a0%84-1-c-baekjoon/36200/">백준 2293번 (동전 1, C++) [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-1fc1cb68      "
					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="#동전-1" class="uagb-toc-link__trigger">동전 1</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">동전 1</h1>



<p><a href="https://www.acmicpc.net/problem/2293" target="_blank" rel="noreferrer noopener">https://www.acmicpc.net/problem/2293</a></p>



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



<figure 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">4 MB</td><td class="has-text-align-left" data-align="left">55412</td><td class="has-text-align-left" data-align="left">25613</td><td class="has-text-align-left" data-align="left">19398</td><td class="has-text-align-left" data-align="left">46.232%</td></tr></tbody></table></figure>



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



<h3 class="wp-block-heading has-large-font-size">문제</h3>



<p>n가지 종류의 동전이 있다. 각각의 동전이 나타내는 가치는 다르다. </p>



<p>이 동전을 적당히 사용해서, 그 가치의 합이 k원이 되도록 하고 싶다. </p>



<p>그 경우의 수를 구하시오. 각각의 동전은 몇 개라도 사용할 수 있다.</p>



<p>사용한 동전의 구성이 같은데, 순서만 다른 것은 같은 경우이다.</p>



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



<h3 class="wp-block-heading has-large-font-size">입력</h3>



<p>첫째 줄에 n, k가 주어진다. (1 ≤ n ≤ 100, 1 ≤ k ≤ 10,000) </p>



<p>다음 n개의 줄에는 각각의 동전의 가치가 주어진다. </p>



<p>동전의 가치는 100,000보다 작거나 같은 자연수이다.</p>



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



<h3 class="wp-block-heading has-large-font-size">출력</h3>



<p>첫째 줄에 경우의 수를 출력한다. 경우의 수는 2<sup>31</sup>보다 작다.</p>



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



<h3 class="wp-block-heading has-large-font-size">예제 입력 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="">3 10
1
2
5</pre>



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



<h3 class="wp-block-heading has-large-font-size">예제 출력 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="">10</pre>



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



<h3 class="wp-block-heading has-large-font-size">출처</h3>



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



<li>빠진 조건을 찾은 사람: <a href="https://www.acmicpc.net/user/goodcrane3" target="_blank" rel="noreferrer noopener">goodcrane3</a></li>



<li>데이터를 추가한 사람: <a href="https://www.acmicpc.net/user/jh05013" target="_blank" rel="noreferrer noopener">jh05013</a></li>
</ul>



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



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



<ul class="wp-block-list">
<li><a href="https://www.acmicpc.net/problem/tag/25" 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-full is-resized"><img decoding="async" src="https://lycos7560.com/wp-content/uploads/2023/07/KakaoTalk_20230718_102905744.jpg" alt="" class="wp-image-36212" width="1199" height="1440" srcset="https://lycos7560.com/wp-content/uploads/2023/07/KakaoTalk_20230718_102905744.jpg 1598w, https://lycos7560.com/wp-content/uploads/2023/07/KakaoTalk_20230718_102905744-250x300.jpg 250w, https://lycos7560.com/wp-content/uploads/2023/07/KakaoTalk_20230718_102905744-768x923.jpg 768w, https://lycos7560.com/wp-content/uploads/2023/07/KakaoTalk_20230718_102905744-1278x1536.jpg 1278w" sizes="(max-width: 1199px) 100vw, 1199px" /></figure>



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



<h2 class="wp-block-heading has-large-font-size">통과된 코드</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>
using namespace std;
int _N, _K, _Temp, _Coin[100], _DP[10001];
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	cin >> _N >> _K;
	for (int i = 0; i &lt; _N; i++)
		cin >> _Coin[i];
	_DP[0] = 1;
	for (int i = 0; i &lt; _N; i++)
		for (int j = _Coin[i]; j &lt;= _K; j++)
			_DP[j] = _DP[j] + _DP[j - _Coin[i]];
	cout &lt;&lt; _DP[_K];
	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="1201" height="100" src="https://lycos7560.com/wp-content/uploads/2023/07/image-41.png" alt="" class="wp-image-36208" srcset="https://lycos7560.com/wp-content/uploads/2023/07/image-41.png 1201w, https://lycos7560.com/wp-content/uploads/2023/07/image-41-300x25.png 300w, https://lycos7560.com/wp-content/uploads/2023/07/image-41-768x64.png 768w" sizes="(max-width: 1201px) 100vw, 1201px" /></figure>



<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-2293%eb%b2%88-%eb%8f%99%ec%a0%84-1-c-baekjoon/36200/">백준 2293번 (동전 1, C++) [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-2293%eb%b2%88-%eb%8f%99%ec%a0%84-1-c-baekjoon/36200/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>백준 2096번 (내려가기, C++) [BAEKJOON]</title>
		<link>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-2096%eb%b2%88-%eb%82%b4%eb%a0%a4%ea%b0%80%ea%b8%b0-c-baekjoon/34960/</link>
					<comments>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-2096%eb%b2%88-%eb%82%b4%eb%a0%a4%ea%b0%80%ea%b8%b0-c-baekjoon/34960/#respond</comments>
		
		<dc:creator><![CDATA[lycos7560]]></dc:creator>
		<pubDate>Tue, 23 May 2023 03:17:47 +0000</pubDate>
				<category><![CDATA[BaekjoonOnlineJudge]]></category>
		<category><![CDATA[C++/CPP]]></category>
		<category><![CDATA[2096]]></category>
		<category><![CDATA[2096번]]></category>
		<category><![CDATA[Baekjoon]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[cpp]]></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[백준 2096]]></category>
		<category><![CDATA[백준 2096번]]></category>
		<category><![CDATA[슬라이딩 윈도우]]></category>
		<category><![CDATA[알고리즘]]></category>
		<category><![CDATA[코딩테스트]]></category>
		<category><![CDATA[코테]]></category>
		<guid isPermaLink="false">https://lycos7560.com/?p=34960</guid>

					<description><![CDATA[<p>백준 2096번 '내려가기' 문제에 대한 글입니다. (This article is about Baekjoon Number 2096 'Going down'.)</p>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-2096%eb%b2%88-%eb%82%b4%eb%a0%a4%ea%b0%80%ea%b8%b0-c-baekjoon/34960/">백준 2096번 (내려가기, C++) [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-454c1bed      "
					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></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/2096" target="_blank" rel="noreferrer noopener">https://www.acmicpc.net/problem/2096</a></p>



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



<figure 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">4 MB (<a href="https://www.acmicpc.net/problem/2096#">하단 참고</a>)</td><td class="has-text-align-left" data-align="left">33888</td><td class="has-text-align-left" data-align="left">12906</td><td class="has-text-align-left" data-align="left">10056</td><td class="has-text-align-left" data-align="left">36.627%</td></tr></tbody></table></figure>



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



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



<p class="has-medium-font-size">N줄에 0 이상 9 이하의 숫자가 세 개씩 적혀 있다. </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:20px" aria-hidden="true" class="wp-block-spacer"></div>



<figure class="wp-block-image size-full"><img decoding="async" width="1224" height="179" src="https://lycos7560.com/wp-content/uploads/2023/05/image-119.png" alt="" class="wp-image-34961" srcset="https://lycos7560.com/wp-content/uploads/2023/05/image-119.png 1224w, https://lycos7560.com/wp-content/uploads/2023/05/image-119-300x44.png 300w, https://lycos7560.com/wp-content/uploads/2023/05/image-119-768x112.png 768w" sizes="(max-width: 1224px) 100vw, 1224px" /></figure>



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



<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:62px" aria-hidden="true" class="wp-block-spacer"></div>



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



<p class="has-medium-font-size">첫째 줄에 N(1 ≤ N ≤ 100,000)이 주어진다. </p>



<p class="has-medium-font-size">다음 N개의 줄에는 숫자가 세 개씩 주어진다. </p>



<p class="has-medium-font-size">숫자는 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 중의 하나가 된다.</p>



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



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



<p class="has-medium-font-size">첫째 줄에 얻을 수 있는 최대 점수와 최소 점수를 띄어서 출력한다.</p>



<div style="height:62px" 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
4 5 6
4 9 0</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="">18 6</pre>



<div style="height:40px" 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
0 0 0
0 0 0
0 0 0</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="">0 0</pre>



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



<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/user/dreamian" target="_blank" rel="noreferrer noopener">dreamian</a></li>



<li>잘못된 데이터를 찾은 사람: <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/25" target="_blank" rel="noreferrer noopener">다이나믹 프로그래밍</a></li>



<li><a href="https://www.acmicpc.net/problem/tag/68" 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>



<figure class="wp-block-image size-full is-resized"><img decoding="async" src="https://lycos7560.com/wp-content/uploads/2023/05/KakaoTalk_20230523_121808487.jpg" alt="" class="wp-image-34965" width="1200" height="1100" srcset="https://lycos7560.com/wp-content/uploads/2023/05/KakaoTalk_20230523_121808487.jpg 1600w, https://lycos7560.com/wp-content/uploads/2023/05/KakaoTalk_20230523_121808487-300x275.jpg 300w, https://lycos7560.com/wp-content/uploads/2023/05/KakaoTalk_20230523_121808487-768x704.jpg 768w, https://lycos7560.com/wp-content/uploads/2023/05/KakaoTalk_20230523_121808487-1536x1407.jpg 1536w" sizes="(max-width: 1200px) 100vw, 1200px" /></figure>



<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>
using namespace std;
int _N, _DP[9], _Temp[3];
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	cin >> _N >> _DP[0] >> _DP[1] >> _DP[2];
	_DP[3] = _DP[0]; // 최솟값 DP 초기화
	_DP[4] = _DP[1];
	_DP[5] = _DP[2];
	for (int i = 1; i &lt; _N; i++) {
		cin >> _DP[6] >> _DP[7] >> _DP[8];
		_Temp[0] = _DP[0];
		_Temp[1] = _DP[1];
		_Temp[2] = _DP[2];
		_DP[0] = max(_Temp[0], _Temp[1]) + _DP[6];
		_DP[1] = max(_Temp[0], max(_Temp[1], _Temp[2])) + _DP[7];
		_DP[2] = max(_Temp[1], _Temp[2]) + _DP[8];
		_Temp[0] = _DP[3];
		_Temp[1] = _DP[4];
		_Temp[2] = _DP[5];
		_DP[3] = min(_Temp[0], _Temp[1]) + _DP[6];
		_DP[4] = min(_Temp[0], min(_Temp[1], _Temp[2])) + _DP[7];
		_DP[5] = min(_Temp[1], _Temp[2]) + _DP[8];
	}

	cout &lt;&lt; max(_DP[0], max(_DP[1], _DP[2])) &lt;&lt; " " &lt;&lt; min(_DP[3], min(_DP[4], _DP[5]));
	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="1207" height="139" src="https://lycos7560.com/wp-content/uploads/2023/05/image-120.png" alt="" class="wp-image-34962" srcset="https://lycos7560.com/wp-content/uploads/2023/05/image-120.png 1207w, https://lycos7560.com/wp-content/uploads/2023/05/image-120-300x35.png 300w, https://lycos7560.com/wp-content/uploads/2023/05/image-120-768x88.png 768w" sizes="(max-width: 1207px) 100vw, 1207px" /></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-2096%eb%b2%88-%eb%82%b4%eb%a0%a4%ea%b0%80%ea%b8%b0-c-baekjoon/34960/">백준 2096번 (내려가기, C++) [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-2096%eb%b2%88-%eb%82%b4%eb%a0%a4%ea%b0%80%ea%b8%b0-c-baekjoon/34960/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>백준 17070번 (파이프 옮기기 1, C++) [BAEKJOON]</title>
		<link>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-17070%eb%b2%88-%ed%8c%8c%ec%9d%b4%ed%94%84-%ec%98%ae%ea%b8%b0%ea%b8%b0-1-c-baekjoon/34406/</link>
					<comments>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-17070%eb%b2%88-%ed%8c%8c%ec%9d%b4%ed%94%84-%ec%98%ae%ea%b8%b0%ea%b8%b0-1-c-baekjoon/34406/#respond</comments>
		
		<dc:creator><![CDATA[lycos7560]]></dc:creator>
		<pubDate>Wed, 03 May 2023 12:28:58 +0000</pubDate>
				<category><![CDATA[BaekjoonOnlineJudge]]></category>
		<category><![CDATA[C++/CPP]]></category>
		<category><![CDATA[<algorithm>]]></category>
		<category><![CDATA[17070]]></category>
		<category><![CDATA[17070번]]></category>
		<category><![CDATA[algorithm]]></category>
		<category><![CDATA[Baekjoon]]></category>
		<category><![CDATA[BFS]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[cpp]]></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[백준 17070]]></category>
		<category><![CDATA[백준 17070번]]></category>
		<category><![CDATA[알고리즘]]></category>
		<category><![CDATA[코딩테스트]]></category>
		<category><![CDATA[코테]]></category>
		<category><![CDATA[파이프 옮기기 1]]></category>
		<guid isPermaLink="false">https://lycos7560.com/?p=34406</guid>

					<description><![CDATA[<p>백준 17070번 '파이프 옮기기 1' 문제에 대한 글입니다. BFS 탐색 또는 DP 알고리즘을 이용하여 해결이 가능합니다. (This article is about Baekjoon Number 17070 'Pipe Transfer 1' problem. It can be solved using BFS search or DP algorithm.)</p>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-17070%eb%b2%88-%ed%8c%8c%ec%9d%b4%ed%94%84-%ec%98%ae%ea%b8%b0%ea%b8%b0-1-c-baekjoon/34406/">백준 17070번 (파이프 옮기기 1, C++) [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-0725da64      "
					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="#파이프-옮기기-1" class="uagb-toc-link__trigger">파이프 옮기기 1</a><li class="uagb-toc__list"><a href="#통과된-코드-bfs" class="uagb-toc-link__trigger">통과된 코드 (BFS)</a><li class="uagb-toc__list"><a href="#통과된-코드-dp" class="uagb-toc-link__trigger">통과된 코드 (DP)</a></ol>					</div>
									</div>
				</div>
			


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



<h1 class="wp-block-heading">파이프 옮기기 1</h1>



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



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



<figure 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">512 MB</td><td class="has-text-align-left" data-align="left">31068</td><td class="has-text-align-left" data-align="left">14557</td><td class="has-text-align-left" data-align="left">9962</td><td class="has-text-align-left" data-align="left">45.783%</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×N의 격자판으로 나타낼 수 있고, </p>



<p class="has-medium-font-size">1×1크기의 정사각형 칸으로 나누어져 있다. </p>



<p class="has-medium-font-size">각각의 칸은 (r, c)로 나타낼 수 있다. 여기서 r은 행의 번호, c는 열의 번호이고, </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>



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



<figure class="wp-block-image size-full"><img decoding="async" width="292" height="156" src="https://lycos7560.com/wp-content/uploads/2023/05/image.png" alt="" class="wp-image-34408"/></figure>



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



<p class="has-medium-font-size">파이프는 회전시킬 수 있으며, 아래와 같이 3가지 방향이 가능하다.</p>



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



<figure class="wp-block-image size-full"><img decoding="async" width="948" height="294" src="https://lycos7560.com/wp-content/uploads/2023/05/image-1.png" alt="" class="wp-image-34409" srcset="https://lycos7560.com/wp-content/uploads/2023/05/image-1.png 948w, https://lycos7560.com/wp-content/uploads/2023/05/image-1-300x93.png 300w, https://lycos7560.com/wp-content/uploads/2023/05/image-1-768x238.png 768w" sizes="(max-width: 948px) 100vw, 948px" /></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>



<p class="has-medium-font-size">즉, 파이프는 항상 빈 칸만 차지해야 한다.</p>



<p class="has-medium-font-size">파이프를 밀 수 있는 방향은 총 3가지가 있으며,&nbsp;→,&nbsp;<img src="https://s.w.org/images/core/emoji/17.0.2/72x72/2198.png" alt="↘" class="wp-smiley" style="height: 1em; max-height: 1em;" />,&nbsp;↓ 방향이다. </p>



<p class="has-medium-font-size">파이프는 밀면서 회전시킬 수 있다. 회전은 45도만 회전시킬 수 있으며, </p>



<p class="has-medium-font-size">미는 방향은 오른쪽, 아래, 또는 오른쪽 아래 대각선 방향이어야 한다.</p>



<p class="has-medium-font-size">파이프가 가로로 놓여진 경우에&nbsp;가능한 이동 방법은 총 2가지, </p>



<p class="has-medium-font-size">세로로 놓여진 경우에는 2가지, 대각선 방향으로 놓여진 경우에는 3가지가 있다.</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>



<figure class="wp-block-image size-full"><img decoding="async" width="1195" height="422" src="https://lycos7560.com/wp-content/uploads/2023/05/image-2.png" alt="" class="wp-image-34410" srcset="https://lycos7560.com/wp-content/uploads/2023/05/image-2.png 1195w, https://lycos7560.com/wp-content/uploads/2023/05/image-2-300x106.png 300w, https://lycos7560.com/wp-content/uploads/2023/05/image-2-768x271.png 768w" sizes="(max-width: 1195px) 100vw, 1195px" /></figure>



<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="1179" height="416" src="https://lycos7560.com/wp-content/uploads/2023/05/image-3.png" alt="" class="wp-image-34411" srcset="https://lycos7560.com/wp-content/uploads/2023/05/image-3.png 1179w, https://lycos7560.com/wp-content/uploads/2023/05/image-3-300x106.png 300w, https://lycos7560.com/wp-content/uploads/2023/05/image-3-768x271.png 768w" sizes="(max-width: 1179px) 100vw, 1179px" /></figure>



<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="1797" height="421" src="https://lycos7560.com/wp-content/uploads/2023/05/image-4.png" alt="" class="wp-image-34412" srcset="https://lycos7560.com/wp-content/uploads/2023/05/image-4.png 1797w, https://lycos7560.com/wp-content/uploads/2023/05/image-4-300x70.png 300w, https://lycos7560.com/wp-content/uploads/2023/05/image-4-768x180.png 768w, https://lycos7560.com/wp-content/uploads/2023/05/image-4-1536x360.png 1536w" sizes="(max-width: 1797px) 100vw, 1797px" /></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">가장 처음에 파이프는&nbsp;(1, 1)와 (1, 2)를 차지하고 있고, 방향은 가로이다. </p>



<p class="has-medium-font-size">파이프의 한쪽 끝을 (N, 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(3 ≤ N ≤ 16)이 주어진다. </p>



<p class="has-medium-font-size">둘째 줄부터 N개의 줄에는 집의 상태가 주어진다. 빈 칸은 0, 벽은 1로 주어진다. </p>



<p class="has-medium-font-size">(1, 1)과 (1, 2)는 항상 빈 칸이다.</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, N)으로 이동시키는 방법의 수를 출력한다. </p>



<p class="has-medium-font-size">이동시킬 수 없는 경우에는 0을 출력한다. </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">예제 입력 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
0 0 0
0 0 0
0 0 0</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:100px" 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="">4
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0</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="">3</pre>



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



<h2 class="wp-block-heading">예제 입력 3</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
0 0 1 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0</pre>



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



<h2 class="wp-block-heading">예제 출력 3</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:100px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 입력 4</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
0 0 0 0 0 0
0 1 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0</pre>



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



<h2 class="wp-block-heading">예제 출력 4</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="">13</pre>



<div style="height:100px" 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/baekjoon" target="_blank" rel="noreferrer noopener">baekjoon</a></li>



<li>데이터를 추가한 사람:&nbsp;<a href="https://www.acmicpc.net/user/djm03178" target="_blank" rel="noreferrer noopener">djm03178</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/11" 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">통과된 코드 (BFS)</h1>



<div style="height:20px" 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/05/KakaoTalk_20230503_203812154.jpg" alt="" class="wp-image-34417" width="1200" height="473" srcset="https://lycos7560.com/wp-content/uploads/2023/05/KakaoTalk_20230503_203812154.jpg 1600w, https://lycos7560.com/wp-content/uploads/2023/05/KakaoTalk_20230503_203812154-300x118.jpg 300w, https://lycos7560.com/wp-content/uploads/2023/05/KakaoTalk_20230503_203812154-768x302.jpg 768w, https://lycos7560.com/wp-content/uploads/2023/05/KakaoTalk_20230503_203812154-1536x605.jpg 1536w" sizes="(max-width: 1200px) 100vw, 1200px" /></figure>



<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;queue>
#include &lt;tuple>
using namespace std;
// 우측, 하단, 대각선
int _DxDy[3][2] = { { 0, 1},{ 1, 0},{ 1, 1} };
bool _MapArr[17][17];
// x좌표, y좌표, 파이프의 상태
// 0 = 가로 , 1 = 세로 , 2 = 대각선
queue&lt;tuple&lt;int, int, int>> _BFSQ;

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

    int _N, _Res = 0;
	cin >> _N;
	for (int i = 1; i &lt;= _N; i++) {
		for (int j = 1; j &lt;= _N; j++) {
			cin >> _MapArr[i][j];
		}
	}

	tuple&lt;int, int, int> _Temp = make_tuple(1, 2, 0);
	_BFSQ.push(_Temp);
	while (!_BFSQ.empty()) {
		_Temp = _BFSQ.front();
		_BFSQ.pop();
		for (int i = 0; i &lt; 3; i++) {
			if (get&lt;2>(_Temp) == 0 &amp;&amp; i == 1 || get&lt;2>(_Temp) == 1 &amp;&amp; i == 0) continue;
			int _dx = get&lt;0>(_Temp) + _DxDy[i][0];
			int _dy = get&lt;1>(_Temp) + _DxDy[i][1];
			if (_dx > _N || _dy > _N || _dx &lt;= 0 || _dy &lt;= 0 || _MapArr[_dx][_dy]) continue;
			if (i == 2) { // 대각선 방향 우측, 하단부 확인
				if (_MapArr[get&lt;0>(_Temp) + _DxDy[0][0]][get&lt;1>(_Temp) + _DxDy[0][1]] ||
					_MapArr[get&lt;0>(_Temp) + _DxDy[1][0]][get&lt;1>(_Temp) + _DxDy[1][1]]) continue;
			}
			if (_dx == _N &amp;&amp; _dy == _N) {
				_Res++;
				continue;
			}
			tuple&lt;int, int, int> _Temp2 = make_tuple(_dx, _dy, i);
			_BFSQ.push(_Temp2);
		}
	}
	cout &lt;&lt; _Res;

    return 0;
}
</pre>



<div style="height:20px" 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/05/image-5.png" alt="" class="wp-image-34414" width="1160" height="94" srcset="https://lycos7560.com/wp-content/uploads/2023/05/image-5.png 965w, https://lycos7560.com/wp-content/uploads/2023/05/image-5-300x24.png 300w, https://lycos7560.com/wp-content/uploads/2023/05/image-5-768x62.png 768w" sizes="(max-width: 1160px) 100vw, 1160px" /></figure>



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



<p class="has-medium-font-size">BFS 탐색을 이용한 결과 생각보다 시간이 많이 걸리고 메모리도 많이 사용한다.</p>



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



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



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



<p class="has-medium-font-size">DP 알고리즘을 이용한 해결법이 더 효율적</p>



<div style="height:20px" 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/05/KakaoTalk_20230503_212725963.jpg" alt="" class="wp-image-34424" width="1200" height="1410" srcset="https://lycos7560.com/wp-content/uploads/2023/05/KakaoTalk_20230503_212725963.jpg 1600w, https://lycos7560.com/wp-content/uploads/2023/05/KakaoTalk_20230503_212725963-255x300.jpg 255w, https://lycos7560.com/wp-content/uploads/2023/05/KakaoTalk_20230503_212725963-768x902.jpg 768w, https://lycos7560.com/wp-content/uploads/2023/05/KakaoTalk_20230503_212725963-1307x1536.jpg 1307w" sizes="(max-width: 1200px) 100vw, 1200px" /></figure>



<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>
using namespace std;
// 0 가로, 1 세로, 2 대각선
int _DpArr[3][17][17];
bool _MapArr[17][17];
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int _N;
    cin >> _N;  
    for (int i = 1; i &lt;= _N; ++i)
        for (int j = 1; j &lt;= _N; ++j)
            cin >> _MapArr[i][j];
    // 파이프의 상태, (1, 2) 시작
    _DpArr[0][1][2] = 1;
    // Tabulation: Bottom Up(표, 상향식) DP
    for (int i = 1; i &lt;= _N; ++i) {
        for (int j = 1; j &lt;= _N; ++j) {
            if (_MapArr[i][j]) continue; // 움직일 위치가 벽이 라면 넘어간다.
            // 가로 상태로 이동이 가능한 경우 => 전 상태가 우측 or 대각선인 경우
            _DpArr[0][i][j] += _DpArr[0][i][j - 1] + _DpArr[2][i][j - 1];
            // 세로 상태로 이동이 가능한 경우 => 전 상태가 하단 or 대각선인 경우
            _DpArr[1][i][j] += _DpArr[1][i - 1][j] + _DpArr[2][i - 1][j];
            // 대각선 상태로 이동이 가능한 조건 체크 => 우측 + 하단에 벽이 있는지 확인이 필요
            if (_MapArr[i][j - 1] || _MapArr[i - 1][j]) continue;
            // 대각선 상태로 이동이 가능한 경우 => 모든 경우 가능
            _DpArr[2][i][j] += _DpArr[0][i - 1][j - 1] + _DpArr[1][i - 1][j - 1] + _DpArr[2][i - 1][j - 1];
        }
    }
    // DP연산을 마친후에 결과 종합
    // _N,_N 에 도착한 상태가 가로, 세로, 대각선 상태를 전부 더해준다.
    cout &lt;&lt; _DpArr[0][_N][_N] + _DpArr[1][_N][_N] + _DpArr[2][_N][_N];
    return 0;
}</pre>



<div style="height:20px" 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/05/image-6.png" alt="" class="wp-image-34421" width="1165" height="114" srcset="https://lycos7560.com/wp-content/uploads/2023/05/image-6.png 829w, https://lycos7560.com/wp-content/uploads/2023/05/image-6-300x29.png 300w, https://lycos7560.com/wp-content/uploads/2023/05/image-6-768x75.png 768w" sizes="(max-width: 1165px) 100vw, 1165px" /></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-17070%eb%b2%88-%ed%8c%8c%ec%9d%b4%ed%94%84-%ec%98%ae%ea%b8%b0%ea%b8%b0-1-c-baekjoon/34406/">백준 17070번 (파이프 옮기기 1, C++) [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-17070%eb%b2%88-%ed%8c%8c%ec%9d%b4%ed%94%84-%ec%98%ae%ea%b8%b0%ea%b8%b0-1-c-baekjoon/34406/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>백준 9251번 (LCS, C++) [BAEKJOON]</title>
		<link>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-9251%eb%b2%88-lcs-c-baekjoon/34481/</link>
					<comments>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-9251%eb%b2%88-lcs-c-baekjoon/34481/#respond</comments>
		
		<dc:creator><![CDATA[lycos7560]]></dc:creator>
		<pubDate>Tue, 25 Apr 2023 21:56:53 +0000</pubDate>
				<category><![CDATA[BaekjoonOnlineJudge]]></category>
		<category><![CDATA[C++/CPP]]></category>
		<category><![CDATA[9251]]></category>
		<category><![CDATA[9251번]]></category>
		<category><![CDATA[Baekjoon]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[cpp]]></category>
		<category><![CDATA[DP]]></category>
		<category><![CDATA[DynamicProgramming]]></category>
		<category><![CDATA[LCS]]></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[백준 9251]]></category>
		<category><![CDATA[백준 9251번]]></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=34481</guid>

					<description><![CDATA[<p>백준 9251번 'LCS' 문제에 대한 글입니다. (This is an article about the problem of "LCS" in Baekjoon<br />
 Number 9251)</p>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-9251%eb%b2%88-lcs-c-baekjoon/34481/">백준 9251번 (LCS, C++) [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-8a34d67c      "
					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="#lcs" class="uagb-toc-link__trigger">LCS</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:62px" aria-hidden="true" class="wp-block-spacer"></div>



<h1 class="wp-block-heading">LCS</h1>



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



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



<figure 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.1 초 (<a href="https://www.acmicpc.net/problem/9251#" target="_blank" rel="noreferrer noopener">하단 참고</a>)</td><td class="has-text-align-left" data-align="left">256 MB</td><td class="has-text-align-left" data-align="left">69404</td><td class="has-text-align-left" data-align="left">28234</td><td class="has-text-align-left" data-align="left">20700</td><td class="has-text-align-left" data-align="left">40.213%</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">LCS(Longest Common Subsequence, 최장 공통 부분 수열)문제는 두 수열이 주어졌을 때, </p>



<p class="has-medium-font-size">모두의 부분 수열이 되는 수열 중 가장 긴 것을 찾는 문제이다.</p>



<p class="has-medium-font-size">예를 들어, ACAYKP와 CAPCAK의 LCS는 ACAK가 된다.</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">문자열은 알파벳 대문자로만 이루어져 있으며, 최대 1000글자로 이루어져 있다.</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">첫째 줄에 입력으로 주어진 두 문자열의 LCS의 길이를 출력한다.</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="">ACAYKP
CAPCAK</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: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/board/search/all/problem/9251/84485" target="_blank" rel="noreferrer noopener">https://www.acmicpc.net/board/search/all/problem/9251/84485</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="">ACAA
AAACBA</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: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="">ABCDEFG
BCDEFGA</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="">6</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="">AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAXAAAAAAAAAAA</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="">999</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="">A
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA</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="">1</pre>



<div style="height:50px" 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/baekjoon" target="_blank" rel="noreferrer noopener">baekjoon</a></li>



<li>데이터를 추가한 사람: <a href="https://www.acmicpc.net/user/bang627" target="_blank" rel="noreferrer noopener">bang627</a>, <a href="https://www.acmicpc.net/user/eric00513" target="_blank" rel="noreferrer noopener">eric00513</a>, <a href="https://www.acmicpc.net/user/qpwoeiruty" target="_blank" rel="noreferrer noopener">qpwoeiruty</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/158" 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>



<figure class="wp-block-image size-full"><img decoding="async" width="756" height="1920" src="https://lycos7560.com/wp-content/uploads/2023/05/KakaoTalk_20230509_065443079.jpg" alt="" class="wp-image-34490" srcset="https://lycos7560.com/wp-content/uploads/2023/05/KakaoTalk_20230509_065443079.jpg 756w, https://lycos7560.com/wp-content/uploads/2023/05/KakaoTalk_20230509_065443079-118x300.jpg 118w, https://lycos7560.com/wp-content/uploads/2023/05/KakaoTalk_20230509_065443079-605x1536.jpg 605w" sizes="(max-width: 756px) 100vw, 756px" /></figure>



<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>

using namespace std;

string _StrA, _StrB, _TempStr;
int _Res, _DP[1001];

int main()
{
	cin >> _StrA >> _StrB;

	int _Max = max(_StrA.size(), _StrB.size());

	for (int i = 0; i &lt; _StrB.length(); i++) {
		int _Temp = 0;
		for (int j = 0; j &lt; _StrA.length(); j++) {
			if (_Temp &lt; _DP[j]) 
				_Temp = _DP[j];
			else if (_StrB[i] == _StrA[j]) 
				_DP[j] = _Temp + 1;
		}
	}

	for (int i = 0; i &lt; _Max; i++)
		_Res = max(_DP[i], _Res);

	cout &lt;&lt; _Res;

	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" src="https://lycos7560.com/wp-content/uploads/2023/05/image-12.png" alt="" class="wp-image-34482" width="947" height="129" srcset="https://lycos7560.com/wp-content/uploads/2023/05/image-12.png 827w, https://lycos7560.com/wp-content/uploads/2023/05/image-12-300x41.png 300w, https://lycos7560.com/wp-content/uploads/2023/05/image-12-768x105.png 768w" sizes="(max-width: 947px) 100vw, 947px" /></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-9251%eb%b2%88-lcs-c-baekjoon/34481/">백준 9251번 (LCS, C++) [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-9251%eb%b2%88-lcs-c-baekjoon/34481/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>백준 11660번 (구간 합 구하기 5, C++) [BAEKJOON]</title>
		<link>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-11660%eb%b2%88-%ea%b5%ac%ea%b0%84-%ed%95%a9-%ea%b5%ac%ed%95%98%ea%b8%b0-5-c-baekjoon/34291/</link>
					<comments>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-11660%eb%b2%88-%ea%b5%ac%ea%b0%84-%ed%95%a9-%ea%b5%ac%ed%95%98%ea%b8%b0-5-c-baekjoon/34291/#respond</comments>
		
		<dc:creator><![CDATA[lycos7560]]></dc:creator>
		<pubDate>Tue, 04 Apr 2023 10:50:02 +0000</pubDate>
				<category><![CDATA[BaekjoonOnlineJudge]]></category>
		<category><![CDATA[C++/CPP]]></category>
		<category><![CDATA[11660]]></category>
		<category><![CDATA[11660번]]></category>
		<category><![CDATA[Baekjoon]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[cpp]]></category>
		<category><![CDATA[DynamicProgramming]]></category>
		<category><![CDATA[study]]></category>
		<category><![CDATA[공부]]></category>
		<category><![CDATA[구간 합 구하기 5]]></category>
		<category><![CDATA[기본]]></category>
		<category><![CDATA[기초]]></category>
		<category><![CDATA[다이나믹 프로그래밍]]></category>
		<category><![CDATA[백준]]></category>
		<category><![CDATA[백준 11660]]></category>
		<category><![CDATA[백준 11660번]]></category>
		<category><![CDATA[알고리즘]]></category>
		<category><![CDATA[코딩테스트]]></category>
		<category><![CDATA[코테]]></category>
		<guid isPermaLink="false">https://lycos7560.com/?p=34291</guid>

					<description><![CDATA[<p>백준 11660번 '구간 합 구하기 5' 문제에 대한 글입니다. 다이나믹 프로그래밍 알고리즘을 이용하여 해결하였습니다. (This article is about Baekjoon Number 11660 question of 'Total Section 5'. It was solved using a dynamic programming algorithm.)</p>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-11660%eb%b2%88-%ea%b5%ac%ea%b0%84-%ed%95%a9-%ea%b5%ac%ed%95%98%ea%b8%b0-5-c-baekjoon/34291/">백준 11660번 (구간 합 구하기 5, C++) [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-f1d93989      "
					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="#구간-합-구하기-5" class="uagb-toc-link__trigger">구간 합 구하기 5</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:62px" aria-hidden="true" class="wp-block-spacer"></div>



<h1 class="wp-block-heading">구간 합 구하기 5</h1>



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



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



<figure 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">42382</td><td class="has-text-align-left" data-align="left">19664</td><td class="has-text-align-left" data-align="left">15131</td><td class="has-text-align-left" data-align="left">45.408%</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×N개의 수가 N×N 크기의 표에 채워져 있다. </p>



<p class="has-medium-font-size">(x1, y1)부터 (x2, y2)까지 합을 구하는 프로그램을 작성하시오. (x, y)는 x행 y열을 의미한다.</p>



<p class="has-medium-font-size">예를 들어, N = 4이고, 표가 아래와 같이 채워져 있는 경우를 살펴보자.</p>



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



<figure class="wp-block-table"><table class="has-fixed-layout"><tbody><tr><td>1</td><td>2</td><td>3</td><td>4</td></tr><tr><td>2</td><td>3</td><td>4</td><td>5</td></tr><tr><td>3</td><td>4</td><td>5</td><td>6</td></tr><tr><td>4</td><td>5</td><td>6</td><td>7</td></tr></tbody></table></figure>



<p class="has-medium-font-size">여기서 (2, 2)부터 (3, 4)까지 합을 구하면 3+4+5+4+5+6 = 27이고, (4, 4)부터 (4, 4)까지 합을 구하면 7이다.</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">(1 ≤ N ≤ 1024, 1 ≤ M ≤ 100,000) 둘째 줄부터 N개의 줄에는 표에 채워져 있는 수가 1행부터 차례대로 주어진다. </p>



<p class="has-medium-font-size">다음 M개의 줄에는 네 개의 정수 x1, y1, x2, y2 가 주어지며, (x1, y1)부터 (x2, y2)의 합을 구해 출력해야 한다. </p>



<p class="has-medium-font-size">표에 채워져 있는 수는 1,000보다 작거나 같은 자연수이다. (x1 ≤ x2, y1 ≤ y2)</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">총 M줄에 걸쳐 (x1, y1)부터 (x2, y2)까지 합을 구해 출력한다.</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 3
1 2 3 4
2 3 4 5
3 4 5 6
4 5 6 7
2 2 3 4
3 4 3 4
1 1 4 4</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="">27
6
64</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="">2 4
1 2
3 4
1 1 1 1
1 2 1 2
2 1 2 1
2 2 2 2</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="">1
2
3
4</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/baekjoon" target="_blank" rel="noreferrer noopener">baekjoon</a></li>



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



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



<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/problem/tag/25" target="_blank" rel="noreferrer noopener">다이나믹 프로그래밍</a></li>



<li><a href="https://www.acmicpc.net/problem/tag/139" 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>



<figure class="wp-block-image size-full is-resized"><img decoding="async" src="https://lycos7560.com/wp-content/uploads/2023/04/image-6.png" alt="" class="wp-image-34295" width="927" height="524" srcset="https://lycos7560.com/wp-content/uploads/2023/04/image-6.png 888w, https://lycos7560.com/wp-content/uploads/2023/04/image-6-300x170.png 300w, https://lycos7560.com/wp-content/uploads/2023/04/image-6-768x434.png 768w" sizes="(max-width: 927px) 100vw, 927px" /></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">N과 M의 범위를 보고 시간 초과를 받을 것 같다고 예상은 했지만 일단 구현해보았다. </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;tuple>

using namespace std;

int _N, _M, _Res, _Map[1025][1025];

vector&lt;tuple &lt;int, int, int, int>> _QuiryVector;
tuple &lt;int, int, int, int> _Quiry;

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 = 1; i &lt;= _N; i++) {
		for (int j = 1; j &lt;= _N; j++)  
			cin >> _Map[i][j];	
	}

	for (int i = 0; i &lt; _M; i++) {
		int _X1, _Y1, _X2, _Y2;
		cin >> _X1 >> _Y1 >> _X2 >> _Y2;
		_QuiryVector.push_back(make_tuple(_X1, _Y1, _X2, _Y2));
	}

	for (auto&amp; it : _QuiryVector) {
		_Res = 0;
		for (int i = get&lt;0>(it); i &lt;= get&lt;2>(it); i++) {
			for (int j = get&lt;1>(it); j &lt;= get&lt;3>(it); j++) {
				_Res += _Map[i][j];
			}
		}
		cout &lt;&lt; _Res &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="47" src="https://lycos7560.com/wp-content/uploads/2023/04/image-9.png" alt="" class="wp-image-34302" srcset="https://lycos7560.com/wp-content/uploads/2023/04/image-9.png 1038w, https://lycos7560.com/wp-content/uploads/2023/04/image-9-300x14.png 300w, https://lycos7560.com/wp-content/uploads/2023/04/image-9-768x35.png 768w" sizes="(max-width: 1038px) 100vw, 1038px" /></figure>



<div style="height:100px" 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>



<figure class="wp-block-image size-full"><img decoding="async" width="1257" height="532" src="https://lycos7560.com/wp-content/uploads/2023/04/image-8.png" alt="" class="wp-image-34300" srcset="https://lycos7560.com/wp-content/uploads/2023/04/image-8.png 1257w, https://lycos7560.com/wp-content/uploads/2023/04/image-8-300x127.png 300w, https://lycos7560.com/wp-content/uploads/2023/04/image-8-768x325.png 768w" sizes="(max-width: 1257px) 100vw, 1257px" /></figure>



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



<p class="has-medium-font-size">내가 생각한 알고리즘에는 Map을 저장할 필요가 없어서 삭제</p>



<div style="height:50px" 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;tuple>

using namespace std;

int _N, _M, _Res, _RowSum[1025][1025];

int _X1, _Y1, _X2, _Y2;

vector&lt;tuple &lt;int, int, int, int>> _QuiryVector;

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 = 1; i &lt;= _N; i++) {
		int _Temp = 0;
		for (int j = 1; j &lt;= _N; j++) {
			cin >> _Temp;
			_RowSum[i][j] = _RowSum[i][j - 1] + _Temp;
		}
	}

	for (int i = 0; i &lt; _M; i++) {
		cin >> _X1 >> _Y1 >> _X2 >> _Y2;
		_QuiryVector.push_back(make_tuple(_X1, _Y1, _X2, _Y2));
	}

	for (auto&amp; it : _QuiryVector) {
		_Res = 0;
		for (int i = get&lt;0>(it); i &lt;= get&lt;2>(it); i++) {
			_Res += _RowSum[i][get&lt;3>(it)] - _RowSum[i][get&lt;1>(it) - 1];
		}
		cout &lt;&lt; _Res &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="1034" height="96" src="https://lycos7560.com/wp-content/uploads/2023/04/image-7.png" alt="" class="wp-image-34297" srcset="https://lycos7560.com/wp-content/uploads/2023/04/image-7.png 1034w, https://lycos7560.com/wp-content/uploads/2023/04/image-7-300x28.png 300w, https://lycos7560.com/wp-content/uploads/2023/04/image-7-768x71.png 768w" sizes="(max-width: 1034px) 100vw, 1034px" /></figure>



<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-11660%eb%b2%88-%ea%b5%ac%ea%b0%84-%ed%95%a9-%ea%b5%ac%ed%95%98%ea%b8%b0-5-c-baekjoon/34291/">백준 11660번 (구간 합 구하기 5, C++) [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-11660%eb%b2%88-%ea%b5%ac%ea%b0%84-%ed%95%a9-%ea%b5%ac%ed%95%98%ea%b8%b0-5-c-baekjoon/34291/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>백준 1149번 (RGB거리, C++) [BAEKJOON]</title>
		<link>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-1149%eb%b2%88-rgb%ea%b1%b0%eb%a6%ac-c-baekjoon/34286/</link>
					<comments>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-1149%eb%b2%88-rgb%ea%b1%b0%eb%a6%ac-c-baekjoon/34286/#respond</comments>
		
		<dc:creator><![CDATA[lycos7560]]></dc:creator>
		<pubDate>Tue, 04 Apr 2023 06:19:05 +0000</pubDate>
				<category><![CDATA[BaekjoonOnlineJudge]]></category>
		<category><![CDATA[C++/CPP]]></category>
		<category><![CDATA[1149]]></category>
		<category><![CDATA[1149번]]></category>
		<category><![CDATA[algorithm]]></category>
		<category><![CDATA[Baekjoon]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[cpp]]></category>
		<category><![CDATA[DP]]></category>
		<category><![CDATA[DynamicProgramming]]></category>
		<category><![CDATA[RGB거리]]></category>
		<category><![CDATA[study]]></category>
		<category><![CDATA[공부]]></category>
		<category><![CDATA[기본]]></category>
		<category><![CDATA[기초]]></category>
		<category><![CDATA[다이나믹 프로그래밍]]></category>
		<category><![CDATA[백준]]></category>
		<category><![CDATA[백준 1149]]></category>
		<category><![CDATA[백준 1149번]]></category>
		<category><![CDATA[알고리즘]]></category>
		<category><![CDATA[코딩테스트]]></category>
		<category><![CDATA[코테]]></category>
		<guid isPermaLink="false">https://lycos7560.com/?p=34286</guid>

					<description><![CDATA[<p>백준 1149번 'RGB거리' 문제에 대한 글입니다. 다이나믹 프로그래밍 알고리즘을 이용하여 해결하였습니다. (This article is about the issue of "RGB Street" in Baekjun 1149. It was solved using a dynamic programming algorithm.)</p>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-1149%eb%b2%88-rgb%ea%b1%b0%eb%a6%ac-c-baekjoon/34286/">백준 1149번 (RGB거리, C++) [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-5bea3463      "
					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="#rgb거리" class="uagb-toc-link__trigger">RGB거리</a><li class="uagb-toc__list"><a href="#통과된-코드" class="uagb-toc-link__trigger">통과된 코드</a></ol>					</div>
									</div>
				</div>
			


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



<h1 class="wp-block-heading">RGB거리</h1>



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



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



<figure 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">95166</td><td class="has-text-align-left" data-align="left">51392</td><td class="has-text-align-left" data-align="left">38281</td><td class="has-text-align-left" data-align="left">53.315%</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">RGB거리에는 집이 N개 있다. </p>



<p class="has-medium-font-size">거리는 선분으로 나타낼 수 있고, 1번 집부터 N번 집이 순서대로&nbsp;있다.</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:20px" aria-hidden="true" class="wp-block-spacer"></div>



<p>1번 집의 색은 2번 집의 색과 같지 않아야 한다.</p>



<p>N번 집의 색은 N-1번 집의 색과 같지 않아야 한다.</p>



<p>i(2 ≤ i ≤ N-1)번 집의 색은 i-1번, i+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">첫째 줄에 집의 수 N(2 ≤ N ≤ 1,000)이 주어진다. </p>



<p class="has-medium-font-size">둘째 줄부터 N개의 줄에는 각 집을 빨강, 초록, 파랑으로 칠하는 비용이 1번 집부터 한 줄에 하나씩 주어진다. </p>



<p class="has-medium-font-size">집을 칠하는 비용은 1,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">첫째 줄에 모든 집을 칠하는 비용의 최솟값을 출력한다.</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="">3
26 40 83
49 60 57
13 89 99</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="">96</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
1 100 100
100 1 100
100 100 1</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="">3</pre>



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



<h2 class="wp-block-heading">예제 입력 3</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 100 100
100 100 100
1 100 100</pre>



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



<h2 class="wp-block-heading">예제 출력 3</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="">102</pre>



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



<h2 class="wp-block-heading">예제 입력 4</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
30 19 5
64 77 64
15 19 97
4 71 57
90 86 84
93 32 91</pre>



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



<h2 class="wp-block-heading">예제 출력 4</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="">208</pre>



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



<h2 class="wp-block-heading">예제 입력 5</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
71 39 44
32 83 55
51 37 63
89 29 100
83 58 11
65 13 15
47 25 29
60 66 19</pre>



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



<h2 class="wp-block-heading">예제 출력 5</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="">253</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/baekjoon" target="_blank" rel="noreferrer noopener">baekjoon</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/fail456" target="_blank" rel="noreferrer noopener">fail456</a></li>



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



<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/problem/tag/25" 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>



<p> Tuple 자료구조를 사용한 것이 생각보다 비효율적??</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;tuple>

using namespace std;

vector&lt;tuple&lt;int, int, int>> _ColorCost; // 0 - 빨강, 1 - 초록, 2 - 파랑

int N, _Co0, _Co1, _Co2;;

int dp[3][10001];

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

	cin >> N;
	for (int i = 0; i &lt; N; i++) {
		cin >> _Co0 >> _Co1 >> _Co2;
		_ColorCost.push_back(make_tuple(_Co0, _Co1, _Co2));
	}

	// 처음을 어떤 색으로 시작하는 가?
	dp[0][1] = get&lt;0>(_ColorCost[0]);
	dp[1][1] = get&lt;1>(_ColorCost[0]);
	dp[2][1] = get&lt;2>(_ColorCost[0]);
	
	// 시작 값을 최소값으로 정한다고 결과가 최소라는 것은 아니다.
	for (int i = 2; i &lt;= N; i++) { 
		dp[0][i] = min(dp[1][i - 1], dp[2][i - 1]) + get&lt;0>(_ColorCost[i - 1]);
		dp[1][i] = min(dp[0][i - 1], dp[2][i - 1]) + get&lt;1>(_ColorCost[i - 1]);
		dp[2][i] = min(dp[0][i - 1], dp[1][i - 1]) + get&lt;2>(_ColorCost[i - 1]);
	}

	// 시작 색이 어떤 색인지에 따라서 3가지의 결과가 나온다.
	cout &lt;&lt; min(dp[0][N], min(dp[1][N], dp[2][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" src="https://lycos7560.com/wp-content/uploads/2023/04/image-5.png" alt="" class="wp-image-34288" width="935" height="105" srcset="https://lycos7560.com/wp-content/uploads/2023/04/image-5.png 908w, https://lycos7560.com/wp-content/uploads/2023/04/image-5-300x34.png 300w, https://lycos7560.com/wp-content/uploads/2023/04/image-5-768x86.png 768w" sizes="(max-width: 935px) 100vw, 935px" /></figure>



<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-1149%eb%b2%88-rgb%ea%b1%b0%eb%a6%ac-c-baekjoon/34286/">백준 1149번 (RGB거리, C++) [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-1149%eb%b2%88-rgb%ea%b1%b0%eb%a6%ac-c-baekjoon/34286/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<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 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/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>
																<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>
											</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>
	</channel>
</rss>
