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

<channel>
	<title>플로이드 워셜 Archives - 어제와 내일의 나 그 사이의 이야기</title>
	<atom:link href="https://lycos7560.com/tag/%ED%94%8C%EB%A1%9C%EC%9D%B4%EB%93%9C-%EC%9B%8C%EC%85%9C/feed/" rel="self" type="application/rss+xml" />
	<link></link>
	<description>생각의 흐름을 타고 다니며 만드는 블로그</description>
	<lastBuildDate>Tue, 04 Jul 2023 12:17:22 +0000</lastBuildDate>
	<language>ko-KR</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://lycos7560.com/wp-content/uploads/2022/11/cropped-cropped-cropped-log-1-150x150-1-80x80.png</url>
	<title>플로이드 워셜 Archives - 어제와 내일의 나 그 사이의 이야기</title>
	<link></link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>백준 2610번 (회의준비, C++) [BAEKJOON]</title>
		<link>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-2610%eb%b2%88-%ed%9a%8c%ec%9d%98%ec%a4%80%eb%b9%84-c-baekjoon/33706/</link>
					<comments>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-2610%eb%b2%88-%ed%9a%8c%ec%9d%98%ec%a4%80%eb%b9%84-c-baekjoon/33706/#comments</comments>
		
		<dc:creator><![CDATA[lycos7560]]></dc:creator>
		<pubDate>Sat, 04 Mar 2023 16:13:32 +0000</pubDate>
				<category><![CDATA[BaekjoonOnlineJudge]]></category>
		<category><![CDATA[C++/CPP]]></category>
		<category><![CDATA[2610]]></category>
		<category><![CDATA[2610번]]></category>
		<category><![CDATA[Baekjoon]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[cpp]]></category>
		<category><![CDATA[Floyd-Warshall]]></category>
		<category><![CDATA[study]]></category>
		<category><![CDATA[Union-Find]]></category>
		<category><![CDATA[공부]]></category>
		<category><![CDATA[그래프 이론]]></category>
		<category><![CDATA[그래프 탐색]]></category>
		<category><![CDATA[기본]]></category>
		<category><![CDATA[기초]]></category>
		<category><![CDATA[백준]]></category>
		<category><![CDATA[백준 2610]]></category>
		<category><![CDATA[백준 2610번]]></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=33706</guid>

					<description><![CDATA[<p>백준(BAEKJOON) 2610번 '회의준비' 문제에 대한 글입니다. 'Floyd-Warshall' &#038; 'Union Find' 알고리즘을 사용하여 해결하였습니다. (This is an article on the 'meeting preparation' problem in BAEKJOON No. 2610. Solved using 'Floyd-Warshall' &#038; 'UnionFind' algorithms.)</p>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-2610%eb%b2%88-%ed%9a%8c%ec%9d%98%ec%a4%80%eb%b9%84-c-baekjoon/33706/">백준 2610번 (회의준비, 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-46c59bdb      "
					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><li class="uagb-toc__list"><li class="uagb-toc__list"><a href="#통과된-코드" class="uagb-toc-link__trigger">통과된 코드</a></ul></ol>					</div>
									</div>
				</div>
			


<div style="height: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/2610" target="_blank" rel="noreferrer noopener">https://www.acmicpc.net/problem/2610</a></p>



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



<figure id="problem-info" class="wp-block-table"><table class="has-fixed-layout"><thead><tr><th class="has-text-align-left" data-align="left">시간 제한</th><th class="has-text-align-left" data-align="left">메모리 제한</th><th class="has-text-align-left" data-align="left">제출</th><th class="has-text-align-left" data-align="left">정답</th><th class="has-text-align-left" data-align="left">맞힌 사람</th><th class="has-text-align-left" data-align="left">정답 비율</th></tr></thead><tbody><tr><td class="has-text-align-left" data-align="left">1 초</td><td class="has-text-align-left" data-align="left">128 MB</td><td class="has-text-align-left" data-align="left">7575</td><td class="has-text-align-left" data-align="left">2180</td><td class="has-text-align-left" data-align="left">1640</td><td class="has-text-align-left" data-align="left">28.616%</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 class="has-medium-font-size">KOI 준비를 위해 회의를 개최하려 한다. </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 class="has-medium-font-size">1. 서로 알고 있는 사람은 반드시 같은 위원회에 속해야 한다.</p>



<p class="has-medium-font-size">2. 효율적인 회의 진행을 위해 위원회의 수는 최대가 되어야 한다.</p>



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



<p class="has-medium-font-size">대표에게 의견을 전달하는 경로가 여러 개 있을 경우에는 가장 적은 사람을 거치는 경로로 의견을 전달하며 </p>



<p class="has-medium-font-size">이때 거치는 사람의 수를 참석자의 의사전달시간이라고 한다.</p>



<p class="has-medium-font-size">위원회에서 모든 참석자들의 의사전달시간 중 최댓값이 최소가 되도록 대표를 정하는 프로그램을 작성하시오.</p>



<p class="has-medium-font-size">예를 들어 1번, 2번, 3번 세 사람으로 구성되어 있는 위원회에서 1번과 2번, 2번과 3번이 서로 알고 있다고 하자. </p>



<p class="has-medium-font-size">1번이 대표가 되면 3번이 대표인 1번에게 의견을 전달하기 위해서 2번을 거쳐야만 한다. </p>



<p class="has-medium-font-size">반대로 3번이 대표가 되어도 1번이 대표인 3번에게 의견을 전달하려면 2번을 거쳐야만 한다. </p>



<p class="has-medium-font-size">하지만 2번이 대표가 되면 1번과 3번 둘 다 아무도 거치지 않고 대표에게 직접 의견을 전달 할 수 있다. </p>



<p class="has-medium-font-size">따라서 이와 같은 경우 2번이 대표가 되어야 한다.</p>



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



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



<p class="has-medium-font-size">첫째 중에 회의에 참석하는 사람의 수 N이 주어진다. </p>



<p class="has-medium-font-size">참석자들은 1부터 N까지의 자연수로 표현되며 회의에 참석하는 인원은 100 이하이다. </p>



<p class="has-medium-font-size">둘째 줄에는 서로 알고 있는 관계의 수 M이 주어진다. </p>



<p class="has-medium-font-size">이어 M개의 각 줄에는 서로 아는 사이인 참석자를 나타내는 두개의 자연수가 주어진다.</p>



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



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



<p class="has-medium-font-size">첫째 줄에는 구성되는 위원회의 수 K를 출력한다. </p>



<p class="has-medium-font-size">다음 K줄에는 각 위원회의 대표 번호를 작은 수부터 차례로 한 줄에 하나씩 출력한다. </p>



<p class="has-medium-font-size">한 위원회의 대표가 될 수 있는 사람이 둘 이상일 경우 그중 한 명만 출력하면 된다.</p>



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



<h3 class="wp-block-heading 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="">8
7
1 2
2 3
4 5
5 6
4 6
6 7
7 4</pre>



<div style="height:31px" 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
2
4
8</pre>



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



<h2 class="wp-block-heading has-large-font-size">추가 반례</h2>



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



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



<h3 class="wp-block-heading has-large-font-size">예제 출력 A</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="">100
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100</pre>



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



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



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



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



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



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



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



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



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



<div style="height:60px" 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/2" target="_blank" rel="noreferrer noopener">Olympiad</a>&nbsp;&gt;&nbsp;<a href="https://www.acmicpc.net/category/55" target="_blank" rel="noreferrer noopener">한국정보올림피아드</a>&nbsp;&gt;&nbsp;<a href="https://www.acmicpc.net/category/57" target="_blank" rel="noreferrer noopener">한국정보올림피아드시․도지역본선</a>&nbsp;&gt;&nbsp;<a href="https://www.acmicpc.net/category/74">지</a><a href="https://www.acmicpc.net/category/74" target="_blank" rel="noreferrer noopener">역</a><a href="https://www.acmicpc.net/category/74">본선 2004</a>&nbsp;&gt;&nbsp;<a href="https://www.acmicpc.net/category/detail/380" target="_blank" rel="noreferrer noopener">중등부</a>&nbsp;3번</p>



<p><a href="https://www.acmicpc.net/category/2" target="_blank" rel="noreferrer noopener">Olympiad</a>&nbsp;&gt;&nbsp;<a href="https://www.acmicpc.net/category/55" target="_blank" rel="noreferrer noopener">한국정보올림피아드</a>&nbsp;&gt;&nbsp;<a href="https://www.acmicpc.net/category/57" target="_blank" rel="noreferrer noopener">한국정보올림피아드시․도지역본선</a>&nbsp;&gt;&nbsp;<a href="https://www.acmicpc.net/category/74">지역</a><a href="https://www.acmicpc.net/category/74" target="_blank" rel="noreferrer noopener">본</a><a href="https://www.acmicpc.net/category/74">선 2004</a>&nbsp;&gt;&nbsp;<a href="https://www.acmicpc.net/category/detail/381" target="_blank" rel="noreferrer noopener">고등부</a>&nbsp;3번</p>



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



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



<div style="height:60px" 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/175" 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>



<li><a href="https://www.acmicpc.net/problem/tag/81" target="_blank" rel="noreferrer noopener">분리 집합</a></li>



<li><a href="https://www.acmicpc.net/problem/tag/31" target="_blank" rel="noreferrer noopener">플로이드–워셜</a></li>
</ul>



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



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



<p class="has-medium-font-size">Floyd-Warshall 알고리즘을 이용하여  의사전달 시간의 최솟값을 구한다.</p>



<p class="has-medium-font-size">그 후에 Union-Find를 이용하여 위원회의 개수와 대표를 구하면 된다.</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;vector>
#include &lt;set>

using namespace std;

// N : 참석하는 사람
int N, K, f, b;

constexpr int INF = INT32_MAX/2;

constexpr int MAX = 101;

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

int disArr[MAX][MAX];

int sumArr[MAX];

// 유니온 root노드 배열
int uArr[MAX];

set&lt;int> checkSet;

//부모를 찾는 함수
//모든 경로가 부모를 가르키게 함
//상수 시간의 복잡도
int Find(int x)
{
	if (uArr[x] == x) return x;
	else return uArr[x] = Find(uArr[x]);
}

//두 노드를 연결 시키는 것
//기준을 정해서 연결시키는 것이 헷갈리지 않음
//작은쪽이 부모 or 큰쪽이 부모
void Union(int x, int y)
{
	x = Find(x);
	y = Find(y);
	if (x != y) {
		if (sumArr[x] &lt;= sumArr[y]) uArr[y] = x;
		else uArr[x] = y;
	}
}

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

	cin >> N >> K;

	// 유니온 배열 초기화
	for (int i = 1; i &lt;= N; i++) uArr[i] = i;

	// 서로를 알고 있음(양방향)
	for (int i = 0; i &lt; K; i++) {
		cin >> f >> b;
		graph[f].push_back(make_pair(b, 1));
		graph[b].push_back(make_pair(f, 1));
	}

	// 최단 거리 배열 disArr 배열을 INF 초기화
	for (int i = 1; i &lt;= N; i++) {
		for (int j = 1; j &lt;= N; j++) {
			disArr[i][j] = INF;
		}
	}

	for (int i = 1; i &lt;= N; i++) {
		for (int j = 0; j &lt; graph[i].size(); j++) {
			int v = graph[i][j].first;
			int weight = graph[i][j].second;
			disArr[i][v] = weight;
		}
	}

	// Floyd-Warshall 알고리즘
	for (int k = 1; k &lt;= N; k++) {
		for (int i = 1; i &lt;= N; i++) {
			for (int j = 1; j &lt;= N; j++) {
				if (i == j) disArr[i][j] = 0;
				else disArr[i][j] = min(disArr[i][j], disArr[i][k] + disArr[k][j]);
			}
		}
	}

	for (int i = 1; i &lt;= N; i++) {
		for (int j = 1; j &lt;= N; j++) {
			
			if (disArr[i][j] == INF) continue;
			// 의사전달시간 중 최대값이 최소가 되도록 대표를 선출
			sumArr[i] = max(sumArr[i], disArr[i][j]); 
		}
	}

	for (int i = 1; i &lt;= N; i++) {
		for (int j = 0; j &lt; graph[i].size(); j++) {
			// Union Find를 사용하여 위원회를 구성
			Union(i, graph[i][j].first);
		}
	}

	// 결과를 출력
	// Set을 이용하여 위원회의 개수와 대표를 저장
	for (int i = 1; i &lt;= N; i++) checkSet.insert(Find(i));
	
	cout &lt;&lt; checkSet.size() &lt;&lt; "\n";
	for (auto&amp; it : checkSet) cout &lt;&lt; it &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 fetchpriority="high" decoding="async" width="913" height="171" src="https://lycos7560.com/wp-content/uploads/2023/03/image-20.png" alt="" class="wp-image-33710" srcset="https://lycos7560.com/wp-content/uploads/2023/03/image-20.png 913w, https://lycos7560.com/wp-content/uploads/2023/03/image-20-300x56.png 300w, https://lycos7560.com/wp-content/uploads/2023/03/image-20-768x144.png 768w" sizes="(max-width: 913px) 100vw, 913px" /></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-2610%eb%b2%88-%ed%9a%8c%ec%9d%98%ec%a4%80%eb%b9%84-c-baekjoon/33706/">백준 2610번 (회의준비, 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-2610%eb%b2%88-%ed%9a%8c%ec%9d%98%ec%a4%80%eb%b9%84-c-baekjoon/33706/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
		<item>
		<title>백준 1956번 (운동, C++, Floyd-Warshall) / 추가 반례 [BAEKJOON]</title>
		<link>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-1956%eb%b2%88-%ec%9a%b4%eb%8f%99-c-floyd-warshall-baekjoon/33371/</link>
					<comments>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-1956%eb%b2%88-%ec%9a%b4%eb%8f%99-c-floyd-warshall-baekjoon/33371/#respond</comments>
		
		<dc:creator><![CDATA[lycos7560]]></dc:creator>
		<pubDate>Sat, 25 Feb 2023 06:17:49 +0000</pubDate>
				<category><![CDATA[BaekjoonOnlineJudge]]></category>
		<category><![CDATA[C++/CPP]]></category>
		<category><![CDATA[1956]]></category>
		<category><![CDATA[1956번]]></category>
		<category><![CDATA[algorithm]]></category>
		<category><![CDATA[Baekjoon]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[cpp]]></category>
		<category><![CDATA[Floyd-Warshall]]></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[백준 1956]]></category>
		<category><![CDATA[백준 1956번]]></category>
		<category><![CDATA[알고리즘]]></category>
		<category><![CDATA[코딩테스트]]></category>
		<category><![CDATA[코테]]></category>
		<category><![CDATA[플로이드 워셜]]></category>
		<guid isPermaLink="false">https://lycos7560.com/?p=33371</guid>

					<description><![CDATA[<p>백준(BAEKJOON) 1956번 '운동' 문제에 대한 글입니다. 'Floyd-Warshall' 알고리즘을 이용하여 해결하였습니다.(This is BAEKJOON's 1956 'Exercise' issue. Solved using the 'Floyd-Warshall' algorithm.)</p>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-1956%eb%b2%88-%ec%9a%b4%eb%8f%99-c-floyd-warshall-baekjoon/33371/">백준 1956번 (운동, C++, Floyd-Warshall) / 추가 반례 [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-52c92585      "
					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: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/1956" target="_blank" rel="noreferrer noopener">https://www.acmicpc.net/problem/1956</a></p>



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



<figure id="problem-info" class="wp-block-table"><table class="has-fixed-layout"><thead><tr><th class="has-text-align-left" data-align="left">시간 제한</th><th class="has-text-align-left" data-align="left">메모리 제한</th><th class="has-text-align-left" data-align="left">제출</th><th class="has-text-align-left" data-align="left">정답</th><th class="has-text-align-left" data-align="left">맞힌 사람</th><th class="has-text-align-left" data-align="left">정답 비율</th></tr></thead><tbody><tr><td class="has-text-align-left" data-align="left">2 초</td><td class="has-text-align-left" data-align="left">192 MB</td><td class="has-text-align-left" data-align="left">17044</td><td class="has-text-align-left" data-align="left">6747</td><td class="has-text-align-left" data-align="left">4965</td><td class="has-text-align-left" data-align="left">40.310%</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">V개의 마을와 E개의 도로로 구성되어 있는 도시가 있다. </p>



<p class="has-medium-font-size">도로는 마을과 마을 사이에 놓여 있으며, 일방 통행 도로이다. </p>



<p class="has-medium-font-size">마을에는 편의상 1번부터 V번까지 번호가 매겨져 있다고 하자.</p>



<p class="has-medium-font-size">당신은 도로를 따라 운동을 하기 위한 경로를 찾으려고 한다. </p>



<p class="has-medium-font-size">운동을 한 후에는 다시 시작점으로 돌아오는 것이 좋기 때문에, 우리는 사이클을 찾기를 원한다. </p>



<p class="has-medium-font-size">단, 당신은 운동을 매우 귀찮아하므로, 사이클을 이루는 도로의 길이의 합이 최소가 되도록 찾으려고 한다.</p>



<p class="has-medium-font-size">도로의 정보가 주어졌을 때, 도로의 길이의 합이 가장 작은 사이클을 찾는 프로그램을 작성하시오. </p>



<p class="has-medium-font-size">두 마을을 왕복하는 경우도 사이클에 포함됨에 주의한다.</p>



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



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



<p class="has-medium-font-size">첫째 줄에 V와 E가 빈칸을 사이에 두고 주어진다. (2 ≤ V ≤ 400, 0 ≤ E ≤ V(V-1)) </p>



<p class="has-medium-font-size">다음 E개의 줄에는 각각 세 개의 정수 a, b, c가 주어진다. </p>



<p class="has-medium-font-size">a번 마을에서 b번 마을로 가는 거리가 c인 도로가 있다는 의미이다. (a&nbsp;→ b임에 주의) </p>



<p class="has-medium-font-size">거리는 10,000 이하의 자연수이다. (a, b) 쌍이 같은 도로가 여러 번 주어지지 않는다.</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="">3 4
1 2 1
3 2 1
1 3 5
2 3 2</pre>



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



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



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">3</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/joonwu04" target="_blank" rel="noreferrer noopener">joonwu04</a>,&nbsp;<a href="https://www.acmicpc.net/user/rhdqor213" target="_blank" rel="noreferrer noopener">rhdqor213</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/31">플로이드–워셜</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="1600" height="1486" src="https://lycos7560.com/wp-content/uploads/2023/02/KakaoTalk_20230225_150301257.jpg" alt="" class="wp-image-33374" srcset="https://lycos7560.com/wp-content/uploads/2023/02/KakaoTalk_20230225_150301257.jpg 1600w, https://lycos7560.com/wp-content/uploads/2023/02/KakaoTalk_20230225_150301257-300x279.jpg 300w, https://lycos7560.com/wp-content/uploads/2023/02/KakaoTalk_20230225_150301257-768x713.jpg 768w, https://lycos7560.com/wp-content/uploads/2023/02/KakaoTalk_20230225_150301257-1536x1427.jpg 1536w" sizes="(max-width: 1600px) 100vw, 1600px" /></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>



<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 int INF = INT32_MAX / 2; 

constexpr int MAX = 401;

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

int disArr[MAX][MAX];

// V 개의 마을, E 개의 도로
int V, E, a, b, c;

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

    cin >> V >> E;

    // 도로의 정보를 입력
    for (int i = 0; i &lt; E; i++) {

        cin >> a >> b >> c;
        // 일방통행
        graph[a].push_back(make_pair(b, c));

    }

    // 최단 거리 배열 disArr 배열을 INF 초기화
    for (int i = 1; i &lt;= V; i++) {
        for (int j = 1; j &lt;= V; j++) {

            if (i == j) disArr[i][j] = 0;
            else disArr[i][j] = INF;

        }
    }

    // 경로 가중치 입력
    for (int i = 1; i &lt;= V; i++) { // 시작 정점
        for (int j = 0; j &lt; graph[i].size(); j++) {

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

            if (disArr[i][v] > weight) disArr[i][v] = weight;
        }
    }

    // 플로이드 워셜
    for (int k = 1; k &lt;= V; k++) {
        for (int i = 1; i &lt;= V; i++) {
            for (int j = 1; j &lt;= V; j++) {
                if (i == j) continue;
                else disArr[i][j] = min(disArr[i][j], disArr[i][k] + disArr[k][j]);
            }
        }
    }
    
    int result = INF;
    for (int i = 1; i &lt;= V; i++) {
        for (int j = 1; j &lt;= V; j++) {
            // 출발 지점과 도착 지점이 같거나 둘중 하나라도 가중치가 무한대라면 넘어간다. 
            if (i == j || disArr[i][j] == INF || disArr[j][i] == INF) continue;
            // 최소 가중치를 찾는다.
            result = min(result, disArr[i][j] + disArr[j][i]);            
        }
    }

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

    return 0;
}</pre>



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



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



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



<h1 class="wp-block-heading">추가 반례</h1>



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



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



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



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">5 8
2 5 5
1 3 5
1 5 9
2 4 2
4 1 9
3 2 10
3 4 9
4 3 7</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="">16</pre>



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



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



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



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



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



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



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



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



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



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



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



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



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



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



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">400 400
1 2 10000
2 3 10000
3 4 10000
4 5 10000
5 6 10000
6 7 10000
7 8 10000
8 9 10000
9 10 10000
10 11 10000
11 12 10000
12 13 10000
13 14 10000
14 15 10000
15 16 10000
16 17 10000
17 18 10000
18 19 10000
19 20 10000
20 21 10000
21 22 10000
22 23 10000
23 24 10000
24 25 10000
25 26 10000
26 27 10000
27 28 10000
28 29 10000
29 30 10000
30 31 10000
31 32 10000
32 33 10000
33 34 10000
34 35 10000
35 36 10000
36 37 10000
37 38 10000
38 39 10000
39 40 10000
40 41 10000
41 42 10000
42 43 10000
43 44 10000
44 45 10000
45 46 10000
46 47 10000
47 48 10000
48 49 10000
49 50 10000
50 51 10000
51 52 10000
52 53 10000
53 54 10000
54 55 10000
55 56 10000
56 57 10000
57 58 10000
58 59 10000
59 60 10000
60 61 10000
61 62 10000
62 63 10000
63 64 10000
64 65 10000
65 66 10000
66 67 10000
67 68 10000
68 69 10000
69 70 10000
70 71 10000
71 72 10000
72 73 10000
73 74 10000
74 75 10000
75 76 10000
76 77 10000
77 78 10000
78 79 10000
79 80 10000
80 81 10000
81 82 10000
82 83 10000
83 84 10000
84 85 10000
85 86 10000
86 87 10000
87 88 10000
88 89 10000
89 90 10000
90 91 10000
91 92 10000
92 93 10000
93 94 10000
94 95 10000
95 96 10000
96 97 10000
97 98 10000
98 99 10000
99 100 10000
100 101 10000
101 102 10000
102 103 10000
103 104 10000
104 105 10000
105 106 10000
106 107 10000
107 108 10000
108 109 10000
109 110 10000
110 111 10000
111 112 10000
112 113 10000
113 114 10000
114 115 10000
115 116 10000
116 117 10000
117 118 10000
118 119 10000
119 120 10000
120 121 10000
121 122 10000
122 123 10000
123 124 10000
124 125 10000
125 126 10000
126 127 10000
127 128 10000
128 129 10000
129 130 10000
130 131 10000
131 132 10000
132 133 10000
133 134 10000
134 135 10000
135 136 10000
136 137 10000
137 138 10000
138 139 10000
139 140 10000
140 141 10000
141 142 10000
142 143 10000
143 144 10000
144 145 10000
145 146 10000
146 147 10000
147 148 10000
148 149 10000
149 150 10000
150 151 10000
151 152 10000
152 153 10000
153 154 10000
154 155 10000
155 156 10000
156 157 10000
157 158 10000
158 159 10000
159 160 10000
160 161 10000
161 162 10000
162 163 10000
163 164 10000
164 165 10000
165 166 10000
166 167 10000
167 168 10000
168 169 10000
169 170 10000
170 171 10000
171 172 10000
172 173 10000
173 174 10000
174 175 10000
175 176 10000
176 177 10000
177 178 10000
178 179 10000
179 180 10000
180 181 10000
181 182 10000
182 183 10000
183 184 10000
184 185 10000
185 186 10000
186 187 10000
187 188 10000
188 189 10000
189 190 10000
190 191 10000
191 192 10000
192 193 10000
193 194 10000
194 195 10000
195 196 10000
196 197 10000
197 198 10000
198 199 10000
199 200 10000
200 201 10000
201 202 10000
202 203 10000
203 204 10000
204 205 10000
205 206 10000
206 207 10000
207 208 10000
208 209 10000
209 210 10000
210 211 10000
211 212 10000
212 213 10000
213 214 10000
214 215 10000
215 216 10000
216 217 10000
217 218 10000
218 219 10000
219 220 10000
220 221 10000
221 222 10000
222 223 10000
223 224 10000
224 225 10000
225 226 10000
226 227 10000
227 228 10000
228 229 10000
229 230 10000
230 231 10000
231 232 10000
232 233 10000
233 234 10000
234 235 10000
235 236 10000
236 237 10000
237 238 10000
238 239 10000
239 240 10000
240 241 10000
241 242 10000
242 243 10000
243 244 10000
244 245 10000
245 246 10000
246 247 10000
247 248 10000
248 249 10000
249 250 10000
250 251 10000
251 252 10000
252 253 10000
253 254 10000
254 255 10000
255 256 10000
256 257 10000
257 258 10000
258 259 10000
259 260 10000
260 261 10000
261 262 10000
262 263 10000
263 264 10000
264 265 10000
265 266 10000
266 267 10000
267 268 10000
268 269 10000
269 270 10000
270 271 10000
271 272 10000
272 273 10000
273 274 10000
274 275 10000
275 276 10000
276 277 10000
277 278 10000
278 279 10000
279 280 10000
280 281 10000
281 282 10000
282 283 10000
283 284 10000
284 285 10000
285 286 10000
286 287 10000
287 288 10000
288 289 10000
289 290 10000
290 291 10000
291 292 10000
292 293 10000
293 294 10000
294 295 10000
295 296 10000
296 297 10000
297 298 10000
298 299 10000
299 300 10000
300 301 10000
301 302 10000
302 303 10000
303 304 10000
304 305 10000
305 306 10000
306 307 10000
307 308 10000
308 309 10000
309 310 10000
310 311 10000
311 312 10000
312 313 10000
313 314 10000
314 315 10000
315 316 10000
316 317 10000
317 318 10000
318 319 10000
319 320 10000
320 321 10000
321 322 10000
322 323 10000
323 324 10000
324 325 10000
325 326 10000
326 327 10000
327 328 10000
328 329 10000
329 330 10000
330 331 10000
331 332 10000
332 333 10000
333 334 10000
334 335 10000
335 336 10000
336 337 10000
337 338 10000
338 339 10000
339 340 10000
340 341 10000
341 342 10000
342 343 10000
343 344 10000
344 345 10000
345 346 10000
346 347 10000
347 348 10000
348 349 10000
349 350 10000
350 351 10000
351 352 10000
352 353 10000
353 354 10000
354 355 10000
355 356 10000
356 357 10000
357 358 10000
358 359 10000
359 360 10000
360 361 10000
361 362 10000
362 363 10000
363 364 10000
364 365 10000
365 366 10000
366 367 10000
367 368 10000
368 369 10000
369 370 10000
370 371 10000
371 372 10000
372 373 10000
373 374 10000
374 375 10000
375 376 10000
376 377 10000
377 378 10000
378 379 10000
379 380 10000
380 381 10000
381 382 10000
382 383 10000
383 384 10000
384 385 10000
385 386 10000
386 387 10000
387 388 10000
388 389 10000
389 390 10000
390 391 10000
391 392 10000
392 393 10000
393 394 10000
394 395 10000
395 396 10000
396 397 10000
397 398 10000
398 399 10000
399 400 10000
400 1 10000</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="">4000000</pre>



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



<div style="height:20px" 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-1956%eb%b2%88-%ec%9a%b4%eb%8f%99-c-floyd-warshall-baekjoon/33371/">백준 1956번 (운동, C++, Floyd-Warshall) / 추가 반례 [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-1956%eb%b2%88-%ec%9a%b4%eb%8f%99-c-floyd-warshall-baekjoon/33371/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>백준 13168번 (내일로 여행, C++, Floyd-Warshall) [BAEKJOON]</title>
		<link>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-13168%eb%b2%88-%eb%82%b4%ec%9d%bc%eb%a1%9c-%ec%97%ac%ed%96%89-c-floyd-warshall-baekjoon/33286/</link>
					<comments>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-13168%eb%b2%88-%eb%82%b4%ec%9d%bc%eb%a1%9c-%ec%97%ac%ed%96%89-c-floyd-warshall-baekjoon/33286/#respond</comments>
		
		<dc:creator><![CDATA[lycos7560]]></dc:creator>
		<pubDate>Fri, 24 Feb 2023 07:22:09 +0000</pubDate>
				<category><![CDATA[BaekjoonOnlineJudge]]></category>
		<category><![CDATA[C++/CPP]]></category>
		<category><![CDATA[13168]]></category>
		<category><![CDATA[13168번]]></category>
		<category><![CDATA[algorithm]]></category>
		<category><![CDATA[Baekjoon]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[cpp]]></category>
		<category><![CDATA[Floyd-Warshall]]></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[백준 13168]]></category>
		<category><![CDATA[백준 13168번]]></category>
		<category><![CDATA[알고리즘]]></category>
		<category><![CDATA[코딩테스트]]></category>
		<category><![CDATA[코테]]></category>
		<category><![CDATA[플로이드 워셜]]></category>
		<guid isPermaLink="false">https://lycos7560.com/?p=33286</guid>

					<description><![CDATA[<p>백준(BAEKJOON) 13168번 '내일로 여행' 문제에 대한 글입니다. Floyd-Warshall 알고리즘을 이용하여 해결하였습니다. (This is an article on the issue of "Travel to Tomorrow" in BAEKJOON No. 13168. It was solved using the Floyd-Warshall algorithm.)</p>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-13168%eb%b2%88-%eb%82%b4%ec%9d%bc%eb%a1%9c-%ec%97%ac%ed%96%89-c-floyd-warshall-baekjoon/33286/">백준 13168번 (내일로 여행, C++, Floyd-Warshall) [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-9646ba85      "
					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></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/13168" target="_blank" rel="noreferrer noopener">https://www.acmicpc.net/problem/13168</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">512 MB</td><td class="has-text-align-left" data-align="left">1846</td><td class="has-text-align-left" data-align="left">429</td><td class="has-text-align-left" data-align="left">317</td><td class="has-text-align-left" data-align="left">25.647%</td></tr></tbody></table></figure>



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



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



<p class="has-medium-font-size">친한 친구인 승현이와 지학이는 여름방학 때 여행을 가기로 계획했습니다. </p>



<p class="has-medium-font-size">해외여행은 금전적으로 부담이 많기 때문에 둘은 한국의 여러 도시를 여행하기로 했습니다. </p>



<p class="has-medium-font-size">한국에는 N개의 도시가 있으며, 승현이는 이 중 여행할 M개의 도시를 결정했습니다. </p>



<p class="has-medium-font-size">여행 경로에서 같은 도시를 여러 번 방문할 수 있으며, 여행을 시작하는 도시와 끝내는 도시는 같습니다.</p>



<p class="has-medium-font-size">한국에는 두 도시 사이를 오갈 수 있는 K개의 교통수단이 있습니다. </p>



<p class="has-medium-font-size">교통수단의 종류는 기차, 지하철, 버스, 택시, 비행기 등으로 다양합니다. </p>



<p class="has-medium-font-size">특히 기차 코스는 매우 세부적으로 나뉘어 있어서 </p>



<p class="has-medium-font-size">무궁화호(Mugunghwa), ITX-새마을(ITX-Saemaeul), ITX-청춘(ITX-Cheongchun), KTX, S-Train, V-Train 등이 있습니다. </p>



<p class="has-medium-font-size">모든 교통수단은 한 번 이용하는 데 필요한 비용이 정해져 있습니다. </p>



<p class="has-medium-font-size">승현이와 지학이는 계획한 M개의 도시를 최소비용으로 차례대로 움직일 것입니다.</p>



<p class="has-medium-font-size">한편, 코레일에서는 ‘내일로’라는 특별한 기차표를 판매하고 있습니다. </p>



<p class="has-medium-font-size">방학 동안, 젊은 청년들은 R원을 주고 내일로 티켓을 살 수 있습니다. </p>



<p class="has-medium-font-size">한 번 내일로 티켓을 사면, 며칠 동안 무궁화호, ITX-새마을, ITX-청춘은 무료로 이용할 수 있으며, </p>



<p class="has-medium-font-size">S-Train과 V-Train은 50% 할인된 가격으로 이용할 수 있습니다. </p>



<p class="has-medium-font-size">KTX나 지하철, 또는 다른 교통수단에 대해서는 할인이 적용되지 않습니다.</p>



<p class="has-medium-font-size">지학이는 자신이 아무것도 하지 않는 것에 죄책감을 느끼기 때문에, </p>



<p class="has-medium-font-size">자신들의 여행에서 내일로 티켓이 필요한지 생각해보기로 했습니다. </p>



<p class="has-medium-font-size">지학이를 도와 내일로 티켓을 사는 것과 사지 않는 것 중 어떤 선택이 더 좋은 지 구하는 프로그램을 작성하세요.</p>



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



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



<p class="has-medium-font-size">첫 번째 줄에는 한국에 있는 도시의 수 N(1 ≤ N ≤ 100)과 1인당 내일로 티켓의 가격 R(1 ≤ R ≤ 1,000,000)이 주어집니다. </p>



<p class="has-medium-font-size">두 번째 줄에는 N개의 도시의 이름이 주어집니다. </p>



<p class="has-medium-font-size">도시의 이름은 알파벳 대소문자로 구성된 길이 20 이하의 문자열입니다. </p>



<p class="has-medium-font-size">세 번째 줄에는 승현이와 지학이가 여행할 도시의 수 M(1 ≤ M ≤ 200)이 주어집니다. </p>



<p class="has-medium-font-size">네 번째 줄에는 승현이와 지학이가 여행할 M개 도시의 이름이 주어집니다. </p>



<p class="has-medium-font-size">이 도시들은 앞서 언급된 N개의 도시 중 하나입니다. 다섯 번째 줄에는 교통수단의 수 K(1 ≤ K ≤ 10,000)가 주어집니다. </p>



<p class="has-medium-font-size">마지막 K개의 줄에는 교통수단에 대한 정보가 주어집니다. </p>



<p class="has-medium-font-size">줄마다 교통수단의 종류 Type<sub>i</sub>, 양 끝 도시 S<sub>i</sub>, E<sub>i</sub>, 1인당 비용 Cost<sub>i</sub>&nbsp;(1 ≤ Cost<sub>i</sub>&nbsp;≤ 100,000)가 주어집니다. </p>



<p class="has-medium-font-size">Type<sub>i</sub>는 ‘Subway’, ‘Bus’, ‘Taxi’, ‘Airplane’, ‘KTX’, ‘S-Train’, ‘V-Train’, ‘ITX-Saemaeul’, ‘ITX-Cheongchun’, ‘Mugunghwa’ 중 하나이며, </p>



<p class="has-medium-font-size">모든 도시는 주어진 K개의 교통수단을 이용하여 갈 수 있음이 보장되어 있습니다.</p>



<p class="has-medium-font-size">한국에는 이름이 같은 도시가 있을 수 있다. </p>



<p class="has-medium-font-size">따라서, N개의 도시의 이름에는 같은 도시의 이름이 두 번 이상 주어질 수도 있다. </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">한줄에 내일로 티켓을 사는 것이 좋으면 ‘Yes’를 출력하고 그렇지 않으면 ‘No’를 출력합니다. </p>



<p class="has-medium-font-size">내일로 티켓을 사더라도 비용이 정확히 같다면 ‘No’를 출력합니다.</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="">14 40000
Boseong Busan Changwon Cheonan Chuncheon Daegu Daejeon Gwangju Jeonju Jinju Masan Seoul Suncheon Yeosu
9
Seoul Jeonju Suncheon Yeosu Suncheon Boseong Jinju Busan Seoul
23
KTX Seoul Busan 20000
KTX Seoul Gwangju 12000
KTX Gwangju Yeosu 8000
KTX Seoul Jinju 17000
ITX-Saemaeul Seoul Cheonan 7000
ITX-Cheongchun Seoul Chuncheon 3000
V-Train Chuncheon Daegu 10000
Subway Seoul Cheonan 2000
ITX-Saemaeul Cheonan Daejeon 4000
ITX-Saemaeul Daejeon Daegu 10000
ITX-Saemaeul Daegu Busan 7000
Mugunghwa Daejeon Daegu 6000
Mugunghwa Daejeon Gwangju 5000
Mugunghwa Gwangju Jeonju 1500
Mugunghwa Jeonju Suncheon 4000
Mugunghwa Suncheon Yeosu 1500
S-Train Busan Changwon 3000
S-Train Changwon Masan 1000
S-Train Masan Jinju 2000
S-Train Jinju Suncheon 2000
S-Train Suncheon Boseong 1000
Bus Busan Jinju 1500
Bus Suncheon Jinju 4000</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="">Yes</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="">5 50000
Seoul Cheonan Daejeon Daegu Busan
3
Seoul Busan Seoul
5
ITX-Saemaeul Seoul Cheonan 7000
ITX-Saemaeul Cheonan Daejeon 4000
ITX-Saemaeul Daejeon Daegu 10000
ITX-Saemaeul Daegu Busan 7000
KTX Seoul Busan 24999</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="">No</pre>



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



<h2 class="wp-block-heading">힌트</h2>



<p>예제 1에서 내일로 티켓을 구매하는 경우 최소비용은 다음과 같다.</p>



<ul class="wp-block-list">
<li>서울 → 전주 : 0원 (서울 → 천안 → 대전 → 광주 → 전주)</li>



<li>전주 → 순천 : 0원 (전주 → 순천)</li>



<li>순천 → 여수 : 0원 (순천 → 여수)</li>



<li>여수 → 순천 : 0원 (여수 → 순천)</li>



<li>순천 → 보성 : 500원 (순천 → 보성)</li>



<li>보성 → 진주 : 1,500원 (보성 → 순천 → 진주)</li>



<li>진주 → 부산 : 1,000원 (진주 → 순천 → 전주 → 광주 → 대전 → 대구 → 부산)</li>



<li>부산 → 서울 : 0원 (부산 → 대구 → 대전 → 천안 → 서울)</li>



<li>총합 : 3,000원 + 40,000원(내일로 티켓)</li>
</ul>



<p>내일로 티켓을 구매하지 않는 경우 최소비용은 다음과 같다.</p>



<ul class="wp-block-list">
<li>서울 → 전주 : 12,500원 (서울 → 천안(지하철) → 대전 → 광주 → 전주)</li>



<li>전주 → 순천 : 4,000원 (전주 → 순천)</li>



<li>순천 → 여수 : 1,500원 (순천 → 여수)</li>



<li>여수 → 순천 : 1,500원 (여수 → 순천)</li>



<li>순천 → 보성 : 1,000원 (순천 → 보성)</li>



<li>보성 → 진주 : 3,000원 (보성 → 순천 → 진주)</li>



<li>진주 → 부산 : 1,500원 (진주 → 부산)</li>



<li>부산 → 서울 : 18,500원 (부산 → 진주 → 서울)</li>



<li>총합 : 43,500원</li>
</ul>



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



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



<p><a href="https://www.acmicpc.net/category/5" target="_blank" rel="noreferrer noopener">University</a>&nbsp;&gt;&nbsp;<a href="https://www.acmicpc.net/category/318" target="_blank" rel="noreferrer noopener">전국 대학생 프로그래밍 대회 동아리 연합 여름 대회</a>&nbsp;&gt;&nbsp;<a href="https://www.acmicpc.net/category/detail/1510" target="_blank" rel="noreferrer noopener">UCPC 2016</a>&nbsp;J번</p>



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



<li>문제를 만든 사람:&nbsp;<a href="https://www.acmicpc.net/user/functionx" target="_blank" rel="noreferrer noopener">functionx</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/102" target="_blank" rel="noreferrer noopener">구현</a></li>



<li><a href="https://www.acmicpc.net/problem/tag/175" 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/136" target="_blank" rel="noreferrer noopener">해시를 사용한 집합과 맵</a></li>



<li><a href="https://www.acmicpc.net/problem/tag/31" target="_blank" rel="noreferrer noopener">플로이드–워셜</a></li>
</ul>



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



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



<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/02/KakaoTalk_20230224_161420595.jpg" alt="" class="wp-image-33289" width="1450" height="2001" srcset="https://lycos7560.com/wp-content/uploads/2023/02/KakaoTalk_20230224_161420595.jpg 1391w, https://lycos7560.com/wp-content/uploads/2023/02/KakaoTalk_20230224_161420595-217x300.jpg 217w, https://lycos7560.com/wp-content/uploads/2023/02/KakaoTalk_20230224_161420595-768x1060.jpg 768w, https://lycos7560.com/wp-content/uploads/2023/02/KakaoTalk_20230224_161420595-1113x1536.jpg 1113w" sizes="(max-width: 1450px) 100vw, 1450px" /></figure>



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



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



<div style="height:22px" 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;map>
#include &lt;vector>

using namespace std;

constexpr int MAX = 100;
constexpr float INF = 1000000000;

// 티켓이 있는 경우와 없는 경우 2가지 전부 확인
vector&lt;pair&lt;int, float>> graph_No_T[MAX];
vector&lt;pair&lt;int, float>> graph_T[MAX];

// 티켓이 있는 경우와 없는 경우 2가지 전부 확인
float disArr_No_T[MAX][MAX];
float disArr_T[MAX][MAX];

// N : 도시의 개수, R : 내일로 티켓의 가격 
// M : 여행할 도시, K : 교통 수단의 수
int N, R, M, K;

// 도시와 번호 매칭
map&lt;string, int> mapKStr; // Key : 도시 이름, int : 도시의 번호
map&lt;int, string> mapKInt; 

map&lt;string, int> routeKStr = { { "Subway", 0},{ "Bus", 1},{ "Taxi", 2},{ "Airplane", 3}, 
	{ "KTX", 4},{ "S-Train", 5},{ "V-Train", 6},{ "ITX-Saemaeul", 7},{ "ITX-Cheongchun", 8}, { "Mugunghwa", 9} };

// 여행하는 순서를 저장하는 백터
vector&lt;int> travelCity;

pair&lt;int, string> temp;

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

	cin >> N >> R;

	for (int i = 0; i &lt; N; i++) {
		cin >> temp.second;
		temp.first = i;
		mapKStr.insert(make_pair(temp.second, temp.first));
		mapKInt.insert(make_pair(temp.first, temp.second));
	}

	// 최단거리 배열 초기화
	for (int i = 0; i &lt; N; i++) {
		for (int j = 0; j &lt; N; j++) {
			if (i == j) {
				disArr_No_T[i][j] = 0;
				disArr_T[i][j] = 0;
			}
			else {
				disArr_No_T[i][j] = INF;
				disArr_T[i][j] = INF;
			}
		}
	}

	cin >> M;

	for (int i = 0; i &lt; M; i++) {
		cin >> temp.second;
		auto it = mapKStr.find(temp.second);
		travelCity.push_back(it->second);
	}

	cin >> K; // 교통 수단의 수
	string Ru, V, U; // 교통수단, 시작, 도착
	float C; // 비용 (나눈ㅅ)
	
	for (int i = 0; i &lt; K; i++) {
		// 교통 수단은 양방향
		cin >> Ru >> V >> U >> C;

		int ru = routeKStr.find(Ru)->second;
		int v = mapKStr.find(V)->second;
		int u = mapKStr.find(U)->second;
		
		// 티켓이 없을 경우는 그대로 간선 입력
		graph_No_T[v].push_back(make_pair(u, C));
		graph_No_T[u].push_back(make_pair(v, C));

		if (ru == 5 || ru == 6) { // 티켓이 있을 경우 반값
			graph_T[v].push_back(make_pair(u, C / 2));
			graph_T[u].push_back(make_pair(v, C / 2));
		}
		else if (ru == 7 || ru == 8 || ru == 9) { 
			// 티켓이 있을 경우 무료
			graph_T[v].push_back(make_pair(u, 0));
			graph_T[u].push_back(make_pair(v, 0));
		}
		else {
			// 티켓이 있으나 없으나 적용
			graph_T[v].push_back(make_pair(u, C));
			graph_T[u].push_back(make_pair(v, C));
		}

	}


	for (int i = 0; i &lt; N; i++) {
		for (int j = 0; j &lt; graph_No_T[i].size(); j++) {
			int v = graph_No_T[i][j].first;
			float weight = graph_No_T[i][j].second;
			float weight2 = graph_T[i][j].second;
			if (disArr_No_T[i][v] > weight) disArr_No_T[i][v] = weight;
			if (disArr_T[i][v] > weight2) disArr_T[i][v] = weight2;
		}
	}

	// Floyd-Warshall
	for (int k = 0; k &lt; N; k++) {
		for (int i = 0; i &lt; N; i++) {
			for (int j = 0; j &lt; N; j++) {
				disArr_No_T[i][j] = min(disArr_No_T[i][j], disArr_No_T[i][k] + disArr_No_T[k][j]);;
				disArr_T[i][j] = min(disArr_T[i][j], disArr_T[i][k] + disArr_T[k][j]);;
			}
		}
	}


	float res_No_T = 0; // 티켓이 없을 때 비용
	float res_T = 0; // 티켓이 있을때 비용

	// 순차적으로 여행
	for (int i = 0; i &lt; M - 1; i++) {
		int S = i;
		int E = i + 1;
		res_No_T += disArr_No_T[travelCity[S]][travelCity[E]];
		res_T += disArr_T[travelCity[S]][travelCity[E]];
	}

	res_T += R; // 티켓의 비용 추가

	if (res_T >= res_No_T) cout &lt;&lt; "No"; // 같아도 No 출력
	else cout &lt;&lt; "Yes";

	return 0;
}
</pre>



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



<p class="has-medium-font-size">99% 에서 틀린다면 float 으로 변경을 해야 한다. </p>



<p class="has-medium-font-size">50% DC를 계산하는 부분에서 문제가 발생한다.</p>



<figure class="wp-block-image size-full is-resized"><img decoding="async" src="https://lycos7560.com/wp-content/uploads/2023/02/image-88.png" alt="" class="wp-image-33290" width="1089" height="100" srcset="https://lycos7560.com/wp-content/uploads/2023/02/image-88.png 825w, https://lycos7560.com/wp-content/uploads/2023/02/image-88-300x28.png 300w, https://lycos7560.com/wp-content/uploads/2023/02/image-88-768x71.png 768w" sizes="(max-width: 1089px) 100vw, 1089px" /></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-13168%eb%b2%88-%eb%82%b4%ec%9d%bc%eb%a1%9c-%ec%97%ac%ed%96%89-c-floyd-warshall-baekjoon/33286/">백준 13168번 (내일로 여행, C++, Floyd-Warshall) [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-13168%eb%b2%88-%eb%82%b4%ec%9d%bc%eb%a1%9c-%ec%97%ac%ed%96%89-c-floyd-warshall-baekjoon/33286/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>백준 2458번 (키 순서, C++, Floyd-Warshall) [BAEKJOON]</title>
		<link>https://lycos7560.com/cpp/%eb%b0%b1%ec%a4%80-2458%eb%b2%88-%ed%82%a4-%ec%88%9c%ec%84%9c-c-floyd-warshall-baekjoon/33260/</link>
					<comments>https://lycos7560.com/cpp/%eb%b0%b1%ec%a4%80-2458%eb%b2%88-%ed%82%a4-%ec%88%9c%ec%84%9c-c-floyd-warshall-baekjoon/33260/#respond</comments>
		
		<dc:creator><![CDATA[lycos7560]]></dc:creator>
		<pubDate>Thu, 23 Feb 2023 01:18:15 +0000</pubDate>
				<category><![CDATA[BaekjoonOnlineJudge]]></category>
		<category><![CDATA[C++/CPP]]></category>
		<category><![CDATA[2458]]></category>
		<category><![CDATA[2458번]]></category>
		<category><![CDATA[algorithm]]></category>
		<category><![CDATA[Baekjoon]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[cpp]]></category>
		<category><![CDATA[Floyd-Warshall]]></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[백준 2458]]></category>
		<category><![CDATA[백준 2458번]]></category>
		<category><![CDATA[알고리즘]]></category>
		<category><![CDATA[코딩테스트]]></category>
		<category><![CDATA[코테]]></category>
		<category><![CDATA[플로이드 워셜]]></category>
		<guid isPermaLink="false">https://lycos7560.com/?p=33260</guid>

					<description><![CDATA[<p>백준(BAEKJOON) 2458번 '키 순서' 문제에 대한 글입니다. Floyd-Warshall 알고리즘을 이용하여 해결하였습니다. This is an article about the 'Line up height' problem in BAEKJOON 2458. It was solved using the Floyd-Warshall algorithm. Line up by height</p>
<p>The post <a href="https://lycos7560.com/cpp/%eb%b0%b1%ec%a4%80-2458%eb%b2%88-%ed%82%a4-%ec%88%9c%ec%84%9c-c-floyd-warshall-baekjoon/33260/">백준 2458번 (키 순서, C++, Floyd-Warshall) [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-e364dede      "
					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></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/2458" target="_blank" rel="noreferrer noopener">https://www.acmicpc.net/problem/2458</a></p>



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



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



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



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



<p class="has-medium-font-size">1번부터 N번까지 번호가 붙여져 있는 학생들에 대하여 두 학생끼리 키를 비교한 결과의 일부가 주어져 있다. </p>



<p class="has-medium-font-size">단, N명의 학생들의 키는 모두 다르다고 가정한다. </p>



<p class="has-medium-font-size">예를 들어, 6명의 학생들에 대하여 6번만 키를 비교하였고, 그 결과가 다음과 같다고 하자.&nbsp;</p>



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



<p class="has-medium-font-size">1번 학생의 키 &lt; 5번 학생의 키</p>



<p class="has-medium-font-size">3번 학생의 키 &lt; 4번 학생의 키</p>



<p class="has-medium-font-size">5번 학생의 키 &lt; 4번 학생의 키</p>



<p class="has-medium-font-size">4번 학생의 키 &lt; 2번 학생의 키</p>



<p class="has-medium-font-size">4번 학생의 키 &lt; 6번 학생의 키</p>



<p class="has-medium-font-size">5번 학생의 키 &lt; 2번 학생의 키</p>



<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">a번 학생의 키가 b번 학생의 키보다 작다면, a에서 b로 화살표를 그려서 표현하였다.&nbsp;</p>



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



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



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



<p class="has-medium-font-size">1번은 5번보다 키가 작고, 5번은 4번보다 작기 때문에, 1번은 4번보다 작게 된다. </p>



<p class="has-medium-font-size">그러면 1번, 3번, 5번은 모두 4번보다 작게 된다. </p>



<p class="has-medium-font-size">또한 4번은 2번과 6번보다 작기 때문에, 4번 학생은 자기보다 작은 학생이 3명이 있고, </p>



<p class="has-medium-font-size">자기보다 큰 학생이 2명이 있게 되어 자신의 키가 몇 번째인지 정확히 알 수 있다. </p>



<p class="has-medium-font-size">그러나 4번을 제외한 학생들은 자신의 키가 몇 번째인지 알 수 없다.&nbsp;</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 (2 ≤ N ≤ 500)과 두 학생 키를 비교한 횟수 M (0 ≤ M ≤ N(N-1)/2)이 주어진다. </p>



<p class="has-medium-font-size">다음 M개의 각 줄에는 두 학생의 키를 비교한 결과를 나타내는 두 양의 정수 a와 b가 주어진다. </p>



<p class="has-medium-font-size">이는 번호가 a인 학생이 번호가 b인 학생보다 키가 작은 것을 의미한다.&nbsp;</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">자신이 키가 몇 번째인지 알 수 있는 학생이 모두 몇 명인지를 출력한다.&nbsp;</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="">6 6
1 5
3 4
5 4
4 2
4 6
5 2</pre>



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



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



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



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



<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="">6 7
1 3
1 5
3 4
5 4
4 2
4 6
5 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="">2</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="">6 3
1 2
2 3
4 5</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:60px" aria-hidden="true" class="wp-block-spacer"></div>



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



<p><a href="https://www.acmicpc.net/category/2" target="_blank" rel="noreferrer noopener">Olympiad</a>&nbsp;&gt;&nbsp;<a href="https://www.acmicpc.net/category/55" target="_blank" rel="noreferrer noopener">한국정보올림피아드</a>&nbsp;&gt;&nbsp;<a href="https://www.acmicpc.net/category/57" target="_blank" rel="noreferrer noopener">한국정보올림피아드시․도지역본선</a>&nbsp;&gt;&nbsp;<a href="https://www.acmicpc.net/category/60" target="_blank" rel="noreferrer noopener">지역본선 2011</a>&nbsp;&gt;&nbsp;<a href="https://www.acmicpc.net/category/detail/337" target="_blank" rel="noreferrer noopener">초등부</a>&nbsp;4번</p>



<ul class="wp-block-list">
<li>잘못된 조건을 찾은 사람:&nbsp;<a href="https://www.acmicpc.net/user/doju" target="_blank" rel="noreferrer noopener">doju</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/31" 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="1308" height="1920" src="https://lycos7560.com/wp-content/uploads/2023/02/KakaoTalk_20230223_101236959.jpg" alt="" class="wp-image-33266" srcset="https://lycos7560.com/wp-content/uploads/2023/02/KakaoTalk_20230223_101236959.jpg 1308w, https://lycos7560.com/wp-content/uploads/2023/02/KakaoTalk_20230223_101236959-204x300.jpg 204w, https://lycos7560.com/wp-content/uploads/2023/02/KakaoTalk_20230223_101236959-768x1127.jpg 768w, https://lycos7560.com/wp-content/uploads/2023/02/KakaoTalk_20230223_101236959-1046x1536.jpg 1046w" sizes="(max-width: 1308px) 100vw, 1308px" /></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>



<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 int INF = INT32_MAX / 2; // overflow 조심

constexpr int MAX = 501;

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

int disArr[MAX][MAX];

int N, M, U, V;

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

    cin >> N >> M;

   for (int i = 0; i &lt; M; i++) {
  
       cin >> U >> V;

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


   for (int i = 1; i &lt;= N; i++)
       for (int j = 1; j &lt;= N; j++) disArr[i][j] = INF;
       
         
   
   
   for (int i = 1; i &lt;= N; i++) {
       for (int j = 0; j &lt; graph[i].size(); j++) {

           int v = graph[i][j].first;
           int weight = graph[i][j].second;

           if (disArr[i][v] > weight) disArr[i][v] = weight;

       }
   }

    for (int k = 1; k &lt;= N; k++) {
        for (int i = 1; i &lt;= N; i++) {
            for (int j = 1; j &lt;= N; j++) {

                disArr[i][j] = min(disArr[i][j], disArr[i][k] + disArr[k][j]);

            }
        }
    }
     

    int ans = 0;

    for (int i = 1; i &lt;= N; i++) {

        int cnt = 0;

        for (int j = 1; j &lt;= N; j++) {

            // i보다 큰 사람이 존재하거나 i 보다 작은 사람이 존재한다는 의미
            if (disArr[i][j] != INF || disArr[j][i] != INF) {
                cnt++;
            }

        }

        // i번쨰 학생이 자신을 제외한 모든 학생과 연결되어 있다.
        // 키 순서를 확인이 가능하다는 의미
        if (cnt == N - 1) 
            ans++;
    }

    cout &lt;&lt; ans &lt;&lt; "\n";

  
    return 0;
}</pre>



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



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



<div style="margin-top:0;margin-bottom:0;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/%eb%b0%b1%ec%a4%80-2458%eb%b2%88-%ed%82%a4-%ec%88%9c%ec%84%9c-c-floyd-warshall-baekjoon/33260/">백준 2458번 (키 순서, C++, Floyd-Warshall) [BAEKJOON]</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://lycos7560.com/cpp/%eb%b0%b1%ec%a4%80-2458%eb%b2%88-%ed%82%a4-%ec%88%9c%ec%84%9c-c-floyd-warshall-baekjoon/33260/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>백준 1613번 (역사, C++, Floyd-Warshall) [BAEKJOON]</title>
		<link>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-1613%eb%b2%88-%ec%97%ad%ec%82%ac-c-floyd-warshall-baekjoon/33239/</link>
					<comments>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-1613%eb%b2%88-%ec%97%ad%ec%82%ac-c-floyd-warshall-baekjoon/33239/#respond</comments>
		
		<dc:creator><![CDATA[lycos7560]]></dc:creator>
		<pubDate>Sat, 18 Feb 2023 01:44:41 +0000</pubDate>
				<category><![CDATA[BaekjoonOnlineJudge]]></category>
		<category><![CDATA[C++/CPP]]></category>
		<category><![CDATA[1613]]></category>
		<category><![CDATA[1613번]]></category>
		<category><![CDATA[algorithm]]></category>
		<category><![CDATA[Baekjoon]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[cpp]]></category>
		<category><![CDATA[Floyd-Warshall]]></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[백준 1613]]></category>
		<category><![CDATA[백준 1613번]]></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=33239</guid>

					<description><![CDATA[<p>백준(BAEKJOON) 1613번 '역사' 문제에 대한 글입니다. Floyd-Warshall 알고리즘을 이용하여 해결하였습니다. (This is BAEKJOON's 1613 'History' issue. It was solved using the Floyd-Warshall algorithm.)</p>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-1613%eb%b2%88-%ec%97%ad%ec%82%ac-c-floyd-warshall-baekjoon/33239/">백준 1613번 (역사, C++, Floyd-Warshall) [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-6b88ed7e      "
					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></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/1613" target="_blank" rel="noreferrer noopener">https://www.acmicpc.net/problem/1613</a></p>



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



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



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



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



<p class="has-medium-font-size">역사, 그 중에서도 한국사에 해박한 세준이는 많은 역사적 사건들의 전후 관계를 잘 알고 있다. </p>



<p class="has-medium-font-size">즉, 임진왜란이 병자호란보다 먼저 일어났으며, 무오사화가 기묘사화보다 먼저 일어났다는 등의 지식을 알고 있는 것이다.</p>



<p class="has-medium-font-size">세준이가 알고 있는 일부 사건들의 전후 관계들이 주어질 때, 주어진 사건들의 전후 관계도 알 수 있을까? </p>



<p class="has-medium-font-size">이를 해결하는 프로그램을 작성해 보도록 하자.</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(400 이하의 자연수)과 알고 있는 사건의 전후 관계의 개수 k(50,000 이하의 자연수)가 주어진다. </p>



<p class="has-medium-font-size">다음 k줄에는 전후 관계를 알고 있는 두 사건의 번호가 주어진다. </p>



<p class="has-medium-font-size">이는 앞에 있는 번호의 사건이 뒤에 있는 번호의 사건보다 먼저 일어났음을 의미한다.</p>



<p class="has-medium-font-size"> 물론 사건의 전후 관계가 모순인 경우는 없다. </p>



<p class="has-medium-font-size">다음에는 사건의 전후 관계를 알고 싶은 사건 쌍의 수 s(50,000 이하의 자연수)이 주어진다. </p>



<p class="has-medium-font-size">다음 s줄에는 각각 서로 다른 두 사건의 번호가 주어진다. </p>



<p class="has-medium-font-size">사건의 번호는 1보다 크거나 같고, N보다 작거나 같은 자연수이다.</p>



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



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



<p class="has-medium-font-size">s줄에 걸쳐 물음에 답한다. </p>



<p class="has-medium-font-size">각 줄에 만일 앞에 있는 번호의 사건이 먼저 일어났으면 -1, </p>



<p class="has-medium-font-size">뒤에 있는 번호의 사건이 먼저 일어났으면 1, 어떤지 모르면(유추할 수 없으면) 0을 출력한다.</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="">5 5
1 2
1 3
2 3
3 4
2 4
3
1 5
2 4
3 1</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="">0
-1
1</pre>



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



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



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



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



<li>문제의 오타를 찾은 사람:&nbsp;<a href="https://www.acmicpc.net/user/joonas" target="_blank" rel="noreferrer noopener">joonas</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/31" 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="1600" height="1306" src="https://lycos7560.com/wp-content/uploads/2023/02/KakaoTalk_20230218_104211238.jpg" alt="" class="wp-image-33244" srcset="https://lycos7560.com/wp-content/uploads/2023/02/KakaoTalk_20230218_104211238.jpg 1600w, https://lycos7560.com/wp-content/uploads/2023/02/KakaoTalk_20230218_104211238-300x245.jpg 300w, https://lycos7560.com/wp-content/uploads/2023/02/KakaoTalk_20230218_104211238-768x627.jpg 768w, https://lycos7560.com/wp-content/uploads/2023/02/KakaoTalk_20230218_104211238-1536x1254.jpg 1536w" sizes="(max-width: 1600px) 100vw, 1600px" /></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>



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

using namespace std;

int n, k, f, b, s;

constexpr int MAX = 401;

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

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

int disArr[MAX][MAX];

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

	cin >> n >> k;

	// 사건의 전후 관계 입력
	for (int i = 0; i &lt; k; i++) {
		cin >> f >> b;
		graph[f].push_back(make_pair(b, -1));
		graph[b].push_back(make_pair(f, 1)); // 역방향 가능
	}

	// 질문을 입력 받는다.
	cin >> s;

	pair&lt;int, int> tempP;

	while (s--) { // 문제를 입력받는다.
		cin >> tempP.first >> tempP.second;
		myList.push_back(tempP);
	}

	for (int i = 1; i &lt;= n; i++) {
		for (int j = 0; j &lt; graph[i].size(); j++) {
			int v = graph[i][j].first;
			int weight = graph[i][j].second;
			disArr[i][v] = weight;
		}
	}


	for (int k = 1; k &lt;= n; k++) {
		for (int i = 1; i &lt;= n; i++) {
			for (int j = 1; j &lt;= n; j++) {
				if (i == j) disArr[i][j] = 0;
				else if (disArr[i][k] == 1 &amp;&amp; disArr[k][j] == 1) disArr[i][j] = 1;
				else if (disArr[i][k] == -1 &amp;&amp; disArr[k][j] == -1) disArr[i][j] = -1;
			}
		}
	}

	for (auto it = myList.begin(); it != myList.end(); it++) {
		cout &lt;&lt; disArr[it->first][it->second] &lt;&lt; "\n";

	}

	return 0;
}</pre>



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



<figure class="wp-block-image size-full"><img decoding="async" width="912" height="126" src="https://lycos7560.com/wp-content/uploads/2023/02/image-83.png" alt="" class="wp-image-33242" srcset="https://lycos7560.com/wp-content/uploads/2023/02/image-83.png 912w, https://lycos7560.com/wp-content/uploads/2023/02/image-83-300x41.png 300w, https://lycos7560.com/wp-content/uploads/2023/02/image-83-768x106.png 768w" sizes="(max-width: 912px) 100vw, 912px" /></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-1613%eb%b2%88-%ec%97%ad%ec%82%ac-c-floyd-warshall-baekjoon/33239/">백준 1613번 (역사, C++, Floyd-Warshall) [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-1613%eb%b2%88-%ec%97%ad%ec%82%ac-c-floyd-warshall-baekjoon/33239/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>백준 1507번 (궁금한 민호, C++, Floyd-Warshall) [BAEKJOON]</title>
		<link>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-1507%eb%b2%88-%ea%b6%81%ea%b8%88%ed%95%9c-%eb%af%bc%ed%98%b8-c-floyd-warshall-baekjoon/33230/</link>
					<comments>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-1507%eb%b2%88-%ea%b6%81%ea%b8%88%ed%95%9c-%eb%af%bc%ed%98%b8-c-floyd-warshall-baekjoon/33230/#respond</comments>
		
		<dc:creator><![CDATA[lycos7560]]></dc:creator>
		<pubDate>Fri, 17 Feb 2023 07:43:51 +0000</pubDate>
				<category><![CDATA[BaekjoonOnlineJudge]]></category>
		<category><![CDATA[C++/CPP]]></category>
		<category><![CDATA[1507]]></category>
		<category><![CDATA[1507번]]></category>
		<category><![CDATA[algorithm]]></category>
		<category><![CDATA[Baekjoon]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[cpp]]></category>
		<category><![CDATA[Floyd-Warshall]]></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[백준 1507]]></category>
		<category><![CDATA[백준 1507번]]></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=33230</guid>

					<description><![CDATA[<p>백준(BAEKJOON) 1507번 '궁금한 민호' 문제에 대한 글입니다. 'Floyd-Warshall' 알고리즘을 이용하여 해결하였습니다.  (This is BAEKJOON's 1507 "Curious Minho" question. Solved using the 'Floyd-Warshall' algorithm.)</p>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-1507%eb%b2%88-%ea%b6%81%ea%b8%88%ed%95%9c-%eb%af%bc%ed%98%b8-c-floyd-warshall-baekjoon/33230/">백준 1507번 (궁금한 민호, C++, Floyd-Warshall) [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-54dab500      "
					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></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/1507" target="_blank" rel="noreferrer noopener">https://www.acmicpc.net/problem/1507</a></p>



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



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



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



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



<p class="has-medium-font-size">강호는 N개의 도시로 이루어진 나라에 살고 있다. </p>



<p class="has-medium-font-size">각 도시는 M개의 도로로 연결되어 있으며, 각 도로를 지날 때 필요한 시간이 존재한다.&nbsp;</p>



<p class="has-medium-font-size">도로는 잘 연결되어 있기 때문에, 도시 A에서 B로 이동할 수 없는 경우는 존재하지 않는다.</p>



<p class="has-medium-font-size">도시 A에서 도시 B로 바로 갈 수 있는 도로가 있거나, </p>



<p class="has-medium-font-size">다른 도시를 거쳐서 갈 수 있을 때, 도시 A에서 B를 갈 수 있다고 한다.</p>



<p class="has-medium-font-size">강호는 모든 쌍의 도시에 대해서 최소 이동 시간을 구해놓았다. </p>



<p class="has-medium-font-size">민호는 이 표를 보고 원래 도로가 몇 개 있는지를 구해보려고 한다.</p>



<p class="has-medium-font-size">예를 들어, 예제의 경우에 모든 도시 사이에 강호가 구한 값을 가지는 도로가 존재한다고 해도 된다. </p>



<p class="has-medium-font-size">하지만, 이 도로의 개수는 최솟값이 아니다. </p>



<p class="has-medium-font-size">예를 들어, 도시 1-2, 2-3, 1-4, 3-4, 4-5, 3-5를 연결하는 도로만 있다고 가정해도, 강호가 구한 모든 쌍의 최솟값을 구할 수 있다. </p>



<p class="has-medium-font-size">이 경우 도로의&nbsp;개수는 6개이고, 모든 도로의 시간의 합은 55이다.</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(1 ≤ N ≤ 20)이 주어진다. </p>



<p class="has-medium-font-size">둘째 줄부터 N개의 줄에 각각의 도시 사이에 이동하는데 필요한 시간이&nbsp;주어진다. </p>



<p class="has-medium-font-size">A에서 B로 가는 시간과 B에서 A로 가는 시간은 같다. 또, A와 B가 같은 경우에는&nbsp;0이 주어지고, </p>



<p class="has-medium-font-size">그 외의 경우에 필요한 시간은 2500보다 작거나 같은 자연수이다.</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">첫째 줄에 도로 개수가&nbsp;최소일 때, 모든 도로의 시간의 합을 출력한다. </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="">5
0 6 15 2 6
6 0 9 8 12
15 9 0 16 18
2 8 16 0 4
6 12 18 4 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="">55</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
0 2 2
2 0 2
2 2 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="">6</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="">8
0 1 6 17 26 13 7 16
1 0 5 16 25 12 7 15
6 5 0 21 21 8 12 11
17 16 21 0 41 28 23 31
26 25 21 41 0 13 32 10
13 12 8 28 13 0 19 3
7 7 12 23 32 19 0 22
16 15 11 31 10 3 22 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="">69</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="">3
0 1 3
1 0 1
3 1 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="">-1</pre>



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



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



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



<li>빠진 조건을 찾은 사람:&nbsp;<a href="https://www.acmicpc.net/user/medic_programmer" target="_blank" rel="noreferrer noopener">medic_programmer</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/31" 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 class="has-medium-font-size">i -&gt; j로 가는 최단경로와 i -&gt; k, k -&gt; j 로 가는 최단경로의 합이 같다면 i -&gt; j경로는 없애준다.</p>



<p class="has-medium-font-size">i -&gt; j의 최단경로가 더 크다면 입력이 잘못된 것이므로 result를 -1로 저장해준다.</p>



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



<figure class="wp-block-image size-full"><img decoding="async" width="1600" height="1343" src="https://lycos7560.com/wp-content/uploads/2023/02/KakaoTalk_20230217_163646475.jpg" alt="" class="wp-image-33235" srcset="https://lycos7560.com/wp-content/uploads/2023/02/KakaoTalk_20230217_163646475.jpg 1600w, https://lycos7560.com/wp-content/uploads/2023/02/KakaoTalk_20230217_163646475-300x252.jpg 300w, https://lycos7560.com/wp-content/uploads/2023/02/KakaoTalk_20230217_163646475-768x645.jpg 768w, https://lycos7560.com/wp-content/uploads/2023/02/KakaoTalk_20230217_163646475-1536x1289.jpg 1536w" sizes="(max-width: 1600px) 100vw, 1600px" /></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>



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

constexpr int MAX = 21;

int disArr[MAX][MAX];

int route[MAX][MAX];

int N, result;

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

	for (int i = 1; i &lt;= N; i++) {
		for (int j = 1; j &lt;= N; j++) {
			for (int k = 1; k &lt;= N; k++) {

				if (j == k || i == k) continue;

				// 불가능한 경우이므로 -1 출력
				if (disArr[i][j] > disArr[i][k] + disArr[k][j]) {
					cout &lt;&lt; "-1";
					return 0;
				}

				// 경로를 지워준다.
				if (disArr[i][j] == disArr[i][k] + disArr[k][j]) route[i][j] = 0;
			}
		}
	}


	for (int i = 1; i &lt;= N; i++) 
		for (int j = 1; j &lt;= N; j++) result += route[i][j];
	
	// 반으로 나누어준다.
	cout &lt;&lt; result / 2;

	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/02/image-82.png" alt="" class="wp-image-33232" width="1151" height="112" srcset="https://lycos7560.com/wp-content/uploads/2023/02/image-82.png 909w, https://lycos7560.com/wp-content/uploads/2023/02/image-82-300x29.png 300w, https://lycos7560.com/wp-content/uploads/2023/02/image-82-768x75.png 768w" sizes="(max-width: 1151px) 100vw, 1151px" /></figure>



<div style="height:50px" 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-1507%eb%b2%88-%ea%b6%81%ea%b8%88%ed%95%9c-%eb%af%bc%ed%98%b8-c-floyd-warshall-baekjoon/33230/">백준 1507번 (궁금한 민호, C++, Floyd-Warshall) [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-1507%eb%b2%88-%ea%b6%81%ea%b8%88%ed%95%9c-%eb%af%bc%ed%98%b8-c-floyd-warshall-baekjoon/33230/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>백준 11562번 (백양로 브레이크, C++, Floyd-Warshall) [BAEKJOON]</title>
		<link>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-11562%eb%b2%88-%eb%b0%b1%ec%96%91%eb%a1%9c-%eb%b8%8c%eb%a0%88%ec%9d%b4%ed%81%ac-c-floyd-warshall-baekjoon/33218/</link>
					<comments>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-11562%eb%b2%88-%eb%b0%b1%ec%96%91%eb%a1%9c-%eb%b8%8c%eb%a0%88%ec%9d%b4%ed%81%ac-c-floyd-warshall-baekjoon/33218/#respond</comments>
		
		<dc:creator><![CDATA[lycos7560]]></dc:creator>
		<pubDate>Fri, 17 Feb 2023 04:38:02 +0000</pubDate>
				<category><![CDATA[BaekjoonOnlineJudge]]></category>
		<category><![CDATA[C++/CPP]]></category>
		<category><![CDATA[11562]]></category>
		<category><![CDATA[11562번]]></category>
		<category><![CDATA[algorithm]]></category>
		<category><![CDATA[Baekjoon]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[cpp]]></category>
		<category><![CDATA[Floyd-Warshall]]></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[백준 11562]]></category>
		<category><![CDATA[백준 11562번]]></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=33218</guid>

					<description><![CDATA[<p>백준(BAEKJOON) 11562번 '백양로 브레이크' 문제에 대한 글입니다. 'Floyd-Warshall' 알고리즘을 이용하여 해결하였습니다. (This is an article on the issue of Baekyang-ro Brake, BAEKJOON No. 11562. Solved using the 'Floyd-Warshall' algorithm.)</p>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-11562%eb%b2%88-%eb%b0%b1%ec%96%91%eb%a1%9c-%eb%b8%8c%eb%a0%88%ec%9d%b4%ed%81%ac-c-floyd-warshall-baekjoon/33218/">백준 11562번 (백양로 브레이크, C++, Floyd-Warshall) [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-9a3904cd      "
					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="#시간-초과-코드-dfs" class="uagb-toc-link__trigger">시간 초과 코드 (DFS)</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/11562" target="_blank" rel="noreferrer noopener">https://www.acmicpc.net/problem/11562</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">2698</td><td class="has-text-align-left" data-align="left">1354</td><td class="has-text-align-left" data-align="left">1024</td><td class="has-text-align-left" data-align="left">47.828%</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">서울 소재 Y모 대학교에서 대규모 공사를 진행하면서, 학교가 마치 미로처럼 변해버리고 말았다. </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일 밤낮을 헤매다가 공학관에서 종합관으로 가는 길은 존재하지 않는다는 결론을 내렸다.</p>



<p class="has-medium-font-size">3일 사이에 과제도 내지 못하고 출석도 하지 못해 학사경고 위기에 처한 남규는 전공을 살려 </p>



<p class="has-medium-font-size">현재 일방통행인 길들 중 반드시 양방향으로 바꿔야만 하는 길이 몇 개인지 조사해 학교에 건의하기로 마음을 먹었다.</p>



<p class="has-medium-font-size">남규는 여러 건물들 사이를 직접 잇는 길들을 모두 조사했고, </p>



<p class="has-medium-font-size">그 중 어떤 길들이 일방통행인지, 또는 양방향 통행이 가능한지를 모두 체크했다.</p>



<p class="has-medium-font-size">남규의 프로그램은 간단하다. </p>



<p class="has-medium-font-size">출발지와 도착지를 입력하면 도착지까지 가기 위해 최소 몇 개의 길을 양방향으로 바꿔야만 하는지를 출력해준다. </p>



<p class="has-medium-font-size">프로그램이 완성되었다는 소문이 퍼지자, 남규처럼 길을 잃고 헤맨 경험이 있는 학생들은 남규에게 묻기 시작했다.</p>



<p class="has-medium-font-size">&#8220;공학관에서 대강당 갈 수 있어?&#8221;</p>



<p class="has-medium-font-size">&#8220;상경대 별관에서 학관으로는?&#8221;</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">첫 줄에 Y대학교 건물의 수 n과 길의 수 m이 주어진다. (n ≤ 250, m ≤ n * (n &#8211; 1) / 2 )</p>



<p class="has-medium-font-size">다음 m줄에 걸쳐, u v b (1 ≤ u ≤ n, 1 ≤ v ≤ n, u != v, b = 0 또는 1) 의 형태로 길에 대한 정보가 주어진다.</p>



<p class="has-medium-font-size">b가 0일 경우 u에서 v로 가는 일방통행 길인 것이고, b가 1일 경우 u와 v를 잇는 양방향 길이다.</p>



<p class="has-medium-font-size">어떤 두 건물 사이를 잇는 길은 최대 한 개이다.</p>



<p class="has-medium-font-size">다음 줄에 학생들의 질문의 수 k가 주어진다. (1 ≤ k ≤ 30,000)</p>



<p class="has-medium-font-size">다음 k줄에 걸쳐 s e (1 ≤ s ≤ n, 1 ≤ e ≤ n)의 형태로 학생들의 질문들이 주어진다.</p>



<p class="has-medium-font-size">이는 질문한 학생이 건물 s에서 건물 e로 가고 싶다는 의미이다.</p>



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



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



<p class="has-medium-font-size">출력은 k줄에 걸쳐 이루어진다.</p>



<p class="has-medium-font-size">각 질문에 대해, 최소 몇 개의 일방통행인 길을 양방향 통행으로 바꿔야 출발지에서 도착지로 갈 수 있는지를 출력한다.</p>



<p class="has-medium-font-size">모든 길을 양방향으로 바꾸더라도 서로 도달 불가능한 건물은 없다.</p>



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



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



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



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



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



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



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



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



<p><a href="https://www.acmicpc.net/category/5" target="_blank" rel="noreferrer noopener">University</a>&nbsp;&gt;&nbsp;<a href="https://www.acmicpc.net/category/334" target="_blank" rel="noreferrer noopener">연세대학교</a>&nbsp;&gt;&nbsp;<a href="https://www.acmicpc.net/category/detail/1419" target="_blank" rel="noreferrer noopener">2015 연세대학교 프로그래밍 경시대회</a>&nbsp;F번</p>



<ul class="wp-block-list">
<li>문제를 만든 사람:&nbsp;<a href="https://www.acmicpc.net/user/portableangel" target="_blank" rel="noreferrer noopener">portableangel</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/31" 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="1600" height="1826" src="https://lycos7560.com/wp-content/uploads/2023/02/KakaoTalk_20230217_133534115.jpg" alt="" class="wp-image-33225" srcset="https://lycos7560.com/wp-content/uploads/2023/02/KakaoTalk_20230217_133534115.jpg 1600w, https://lycos7560.com/wp-content/uploads/2023/02/KakaoTalk_20230217_133534115-263x300.jpg 263w, https://lycos7560.com/wp-content/uploads/2023/02/KakaoTalk_20230217_133534115-768x876.jpg 768w, https://lycos7560.com/wp-content/uploads/2023/02/KakaoTalk_20230217_133534115-1346x1536.jpg 1346w" sizes="(max-width: 1600px) 100vw, 1600px" /></figure>



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



<h1 class="wp-block-heading">시간 초과 코드 (DFS)</h1>



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



<p class="has-medium-font-size">DFS 탐색을 이용하여 도착지부터 출발지까지 최소 거리를 카운트하여 탐색한 코드 </p>



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



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

using namespace std;

constexpr int INF = INT32_MAX / 2;

constexpr int MAX = 251;

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

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

int disArr[MAX][MAX];
bool check[MAX];

// N : 도시의 수, M : 길의 수
int N, M, s, d, w, b, K, result;

void Tracking(int start, int now, int cnt)
{
	if (start == now || disArr[now][start] == 0) {
		result = min(result, cnt);
		return;
	}

	for (int j = 0; j &lt; graph[now].size(); j++) {
		int v = graph[now][j].first; // 도착점
		int weight = graph[now][j].second; // 가중치

		if (check[v] == true) continue;

		if (weight == INF) {
			cnt++;
			check[v] = true;
			Tracking(start, v, cnt);
			check[v] = false;
			cnt--;	
		}
		else
		{
			check[v] = true;
			Tracking(start, v, cnt);
			check[v] = false;
		}
	}
}


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

	cin >> N >> M;

	// 길의 정보를 입력
	for (int i = 0; i &lt; M; i++) {

		// 출발 s, 도착 d, b 양/단 방향
		cin >> s >> d >> b;

		if (b == 0) { // 단방향
			graph[s].push_back(make_pair(d, 0));
			graph[d].push_back(make_pair(s, INF));
		}
		else { // 양방향
			graph[s].push_back(make_pair(d, 0));
			graph[d].push_back(make_pair(s, 0));
		}

	}

	// 질문을 입력 받는다.
	cin >> K;
	pair&lt;int, int> tempP;
	while (K--) {
		cin >> tempP.first >> tempP.second;
		myList.push_back(tempP);
	}




	// 최단 거리 배열 disArr 배열을 INF 초기화
	for (int i = 1; i &lt;= N; i++) {
		for (int j = 1; j &lt;= N; j++) {

			if (i == j) disArr[i][j] = 0;
			else disArr[i][j] = INF;

		}
	}

	for (int i = 1; i &lt;= N; i++) { // 시작 정점
		for (int j = 0; j &lt; graph[i].size(); j++) {

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

			if (disArr[i][v] > weight) 	disArr[i][v] = weight;

		}
	}

	// 무지성 플로이드
	for (int k = 1; k &lt;= N; k++) {
		for (int i = 1; i &lt;= N; i++) {
			for (int j = 1; j &lt;= N; j++) {

				if (i == j) disArr[i][j] = 0;
				else disArr[i][j] = min(disArr[i][j], disArr[i][k] + disArr[k][j]);

			}
		}
	}

	for (auto it = myList.begin(); it != myList.end(); it++) {
		result = INF;
		if (disArr[it->first][it->second] == 0) cout &lt;&lt; disArr[it->first][it->second] &lt;&lt; "\n";
		else {
			Tracking(it->second, it->first, 0);
			cout &lt;&lt; result &lt;&lt; "\n";
		}
	}

	return 0;
}</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">Floyd-Warshall 알고리즘이 어떤 식으로 작동하는지 안다면 뒤집는 부분에 왜 1 이 들어가는지 알 수 있다.</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;list>

using namespace std;

constexpr int INF = INT32_MAX / 2;

constexpr int MAX = 251;

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

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

int disArr[MAX][MAX];

// N : 도시의 수, M : 길의 수
int N, M, s, d, w, b, K, result;

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

	cin >> N >> M;

	// 최단 거리 배열 disArr 배열을 INF 초기화
	for (int i = 1; i &lt;= N; i++) {
		for (int j = 1; j &lt;= N; j++) {
			disArr[i][j] = INF;
		}
	}

	// 길의 정보를 입력
	for (int i = 0; i &lt; M; i++) {

		// 출발 s, 도착 d, b 양/단 방향
		cin >> s >> d >> b;

		if (b == 0) { // 단방향
			graph[s].push_back(make_pair(d, 0));
			disArr[d][s] = 1; // (disArr[i][k] + disArr[k][j] vs INF)
		}
		else { // 양방향
			graph[s].push_back(make_pair(d, 0));
			graph[d].push_back(make_pair(s, 0));
		}

	}

	// 질문을 입력 받는다.
	cin >> K;

	pair&lt;int, int> tempP;

	while (K--) {
		cin >> tempP.first >> tempP.second;
		myList.push_back(tempP);
	}

	for (int i = 1; i &lt;= N; i++) { // 시작 정점
		for (int j = 0; j &lt; graph[i].size(); j++) {

			int v = graph[i][j].first; // 도착점
			int weight = graph[i][j].second; // 가중치
			if (disArr[i][v] > weight) disArr[i][v] = weight;
		}
	}

	// 무지성 플로이드
	for (int k = 1; k &lt;= N; k++) {
		for (int i = 1; i &lt;= N; i++) {
			for (int j = 1; j &lt;= N; j++) {

				if (i == j) disArr[i][j] = 0;
				else disArr[i][j] = min(disArr[i][j], disArr[i][k] + disArr[k][j]);

			}
		}
	}

	for (auto it = myList.begin(); it != myList.end(); it++) {
		cout &lt;&lt; disArr[it->first][it->second] &lt;&lt; "\n";		
	}

	return 0;
}</pre>



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



<figure class="wp-block-image size-full is-resized"><img decoding="async" src="https://lycos7560.com/wp-content/uploads/2023/02/image-81.png" alt="" class="wp-image-33220" width="1192" height="166" srcset="https://lycos7560.com/wp-content/uploads/2023/02/image-81.png 917w, https://lycos7560.com/wp-content/uploads/2023/02/image-81-300x42.png 300w, https://lycos7560.com/wp-content/uploads/2023/02/image-81-768x107.png 768w" sizes="(max-width: 1192px) 100vw, 1192px" /></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-11562%eb%b2%88-%eb%b0%b1%ec%96%91%eb%a1%9c-%eb%b8%8c%eb%a0%88%ec%9d%b4%ed%81%ac-c-floyd-warshall-baekjoon/33218/">백준 11562번 (백양로 브레이크, C++, Floyd-Warshall) [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-11562%eb%b2%88-%eb%b0%b1%ec%96%91%eb%a1%9c-%eb%b8%8c%eb%a0%88%ec%9d%b4%ed%81%ac-c-floyd-warshall-baekjoon/33218/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>백준 11404번 (플로이드, C++, Floyd-Warshall) [BAEKJOON]</title>
		<link>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-11404%eb%b2%88-%ed%94%8c%eb%a1%9c%ec%9d%b4%eb%93%9c-c-floyd-warshall-baekjoon/33213/</link>
					<comments>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-11404%eb%b2%88-%ed%94%8c%eb%a1%9c%ec%9d%b4%eb%93%9c-c-floyd-warshall-baekjoon/33213/#respond</comments>
		
		<dc:creator><![CDATA[lycos7560]]></dc:creator>
		<pubDate>Fri, 17 Feb 2023 02:12:37 +0000</pubDate>
				<category><![CDATA[BaekjoonOnlineJudge]]></category>
		<category><![CDATA[C++/CPP]]></category>
		<category><![CDATA[11404]]></category>
		<category><![CDATA[11404번]]></category>
		<category><![CDATA[algorithm]]></category>
		<category><![CDATA[Baekjoon]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[cpp]]></category>
		<category><![CDATA[Floyd-Warshall]]></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[백준 11404]]></category>
		<category><![CDATA[백준 11404번]]></category>
		<category><![CDATA[알고리즘]]></category>
		<category><![CDATA[코딩테스트]]></category>
		<category><![CDATA[코테]]></category>
		<category><![CDATA[플로이드]]></category>
		<category><![CDATA[플로이드 워셜]]></category>
		<guid isPermaLink="false">https://lycos7560.com/?p=33213</guid>

					<description><![CDATA[<p>백준(BAEKJOON) 11404번 '플로이드' 문제에 대한 글입니다. 'Floyd-Warshall' 알고리즘을 이용하여 해결하였습니다. (This is an article about the 'Floyd' problem in BAEKJOON 11404. Solved using the 'Floyd-Warshall' algorithm.)</p>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-11404%eb%b2%88-%ed%94%8c%eb%a1%9c%ec%9d%b4%eb%93%9c-c-floyd-warshall-baekjoon/33213/">백준 11404번 (플로이드, C++, Floyd-Warshall) [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-9bb9666b      "
					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></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/11404" target="_blank" rel="noreferrer noopener">https://www.acmicpc.net/problem/11404</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">50776</td><td class="has-text-align-left" data-align="left">21108</td><td class="has-text-align-left" data-align="left">14907</td><td class="has-text-align-left" data-align="left">41.664%</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(2 ≤ n ≤ 100)개의 도시가 있다. </p>



<p class="has-medium-font-size">그리고 한 도시에서 출발하여 다른 도시에 도착하는 m(1 ≤ m ≤ 100,000)개의 버스가 있다. </p>



<p class="has-medium-font-size">각 버스는 한 번 사용할 때 필요한 비용이 있다.</p>



<p class="has-medium-font-size">모든 도시의 쌍 (A, B)에 대해서 도시 A에서 B로 가는데 필요한 비용의 최솟값을 구하는 프로그램을 작성하시오.</p>



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



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



<p class="has-medium-font-size">첫째 줄에 도시의 개수 n이 주어지고 둘째 줄에는 버스의 개수 m이 주어진다. </p>



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



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



<p class="has-medium-font-size">버스의 정보는 버스의 시작 도시 a, 도착 도시 b, 한 번 타는데 필요한 비용 c로 이루어져 있다. </p>



<p class="has-medium-font-size">시작 도시와 도착 도시가 같은 경우는 없다. </p>



<p class="has-medium-font-size">비용은 100,000보다 작거나 같은 자연수이다.</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개의 줄을 출력해야 한다. </p>



<p class="has-medium-font-size">i번째 줄에 출력하는 j번째 숫자는 도시 i에서 j로 가는데 필요한 최소 비용이다.</p>



<p class="has-medium-font-size">만약, i에서 j로 갈 수 없는 경우에는 그 자리에 0을 출력한다.</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="">5
14
1 2 2
1 3 3
1 4 1
1 5 10
2 4 2
3 4 1
3 5 1
4 5 3
3 5 10
3 1 8
1 4 2
5 1 7
3 4 2
5 2 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="">0 2 3 1 4
12 0 15 2 5
8 5 0 1 1
10 7 13 0 3
7 4 10 6 0</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/algoshipda" target="_blank" rel="noreferrer noopener">algoshipda</a></li>



<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/dbfldkfdbgml" target="_blank" rel="noreferrer noopener">dbfldkfdbgml</a>, <a href="https://www.acmicpc.net/user/koosaga" target="_blank" rel="noreferrer noopener">koosaga</a></li>



<li>데이터를 추가한 사람: <a href="https://www.acmicpc.net/user/dlwocks31" target="_blank" rel="noreferrer noopener">dlwocks31</a>, <a href="https://www.acmicpc.net/user/kyaryunha_cpp" target="_blank" rel="noreferrer noopener">kyaryunha_cpp</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/31" 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="1600" height="1456" src="https://lycos7560.com/wp-content/uploads/2023/02/KakaoTalk_20230217_110728324.jpg" alt="" class="wp-image-33216" srcset="https://lycos7560.com/wp-content/uploads/2023/02/KakaoTalk_20230217_110728324.jpg 1600w, https://lycos7560.com/wp-content/uploads/2023/02/KakaoTalk_20230217_110728324-300x273.jpg 300w, https://lycos7560.com/wp-content/uploads/2023/02/KakaoTalk_20230217_110728324-768x699.jpg 768w, https://lycos7560.com/wp-content/uploads/2023/02/KakaoTalk_20230217_110728324-1536x1398.jpg 1536w" sizes="(max-width: 1600px) 100vw, 1600px" /></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>



<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 int INF = INT32_MAX/2; // overflow 조

constexpr int MAX = 101;

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

int disArr[MAX][MAX];

// N : 도시의 수, M : 버스의 수
int N, M, s, d, w;

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

	cin >> N >> M;

	// 노선을 입력
	for (int i = 0; i &lt; M; i++) {

		// 단방향 교통수단 출발 s, 도착 d, 비용 w
		cin >> s >> d >> w;

		graph[s].push_back(make_pair(d, w));
	}

	// 최단 거리 배열 disArr 배열을 INF 초기화
	for (int i = 1; i &lt;= N; i++) {
		for (int j = 1; j &lt;= N; j++) {

			if (i == j) disArr[i][j] = 0;
			else disArr[i][j] = INF;

		}
	}

	for (int i = 1; i &lt;= N; i++) { // 시작 정점
		for (int j = 0; j &lt; graph[i].size(); j++) {

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

			if (disArr[i][v] > weight) 	disArr[i][v] = weight;

		}
	}
	
	// 무지성 플로이드
	for (int k = 1; k &lt;= N; k++) {
		for (int i = 1; i &lt;= N; i++) {
			for (int j = 1; j &lt;= N; j++) {

				if (i == j) disArr[i][j] = 0;
				else disArr[i][j] = min(disArr[i][j], disArr[i][k] + disArr[k][j]);

			}
		}
	}

	// 결과 출력
	for (int i = 1; i &lt;= N; i++) {
		for (int j = 1; j &lt;= N; j++) {

			if (disArr[i][j] == INF) cout &lt;&lt; 0 &lt;&lt; " ";
			else cout &lt;&lt; disArr[i][j] &lt;&lt; " ";

		}

		cout &lt;&lt; "\n";

	}

	return 0;
}</pre>



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



<figure class="wp-block-image size-full is-resized"><img decoding="async" src="https://lycos7560.com/wp-content/uploads/2023/02/image-80.png" alt="" class="wp-image-33214" width="982" height="89" srcset="https://lycos7560.com/wp-content/uploads/2023/02/image-80.png 912w, https://lycos7560.com/wp-content/uploads/2023/02/image-80-300x27.png 300w, https://lycos7560.com/wp-content/uploads/2023/02/image-80-768x70.png 768w" sizes="(max-width: 982px) 100vw, 982px" /></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-11404%eb%b2%88-%ed%94%8c%eb%a1%9c%ec%9d%b4%eb%93%9c-c-floyd-warshall-baekjoon/33213/">백준 11404번 (플로이드, C++, Floyd-Warshall) [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-11404%eb%b2%88-%ed%94%8c%eb%a1%9c%ec%9d%b4%eb%93%9c-c-floyd-warshall-baekjoon/33213/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>백준 1389번 (케빈 베이컨의 6단계 법칙, C++) [BAEKJOON]</title>
		<link>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-1389%eb%b2%88-%ec%bc%80%eb%b9%88-%eb%b2%a0%ec%9d%b4%ec%bb%a8%ec%9d%98-6%eb%8b%a8%ea%b3%84-%eb%b2%95%ec%b9%99-c-baekjoon/6182/</link>
					<comments>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-1389%eb%b2%88-%ec%bc%80%eb%b9%88-%eb%b2%a0%ec%9d%b4%ec%bb%a8%ec%9d%98-6%eb%8b%a8%ea%b3%84-%eb%b2%95%ec%b9%99-c-baekjoon/6182/#respond</comments>
		
		<dc:creator><![CDATA[lycos7560]]></dc:creator>
		<pubDate>Tue, 07 Feb 2023 16:35:58 +0000</pubDate>
				<category><![CDATA[BaekjoonOnlineJudge]]></category>
		<category><![CDATA[C++/CPP]]></category>
		<category><![CDATA[1389]]></category>
		<category><![CDATA[1389번]]></category>
		<category><![CDATA[Baekjoon]]></category>
		<category><![CDATA[BFS]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[cin]]></category>
		<category><![CDATA[cin.tie(NULL)]]></category>
		<category><![CDATA[cout]]></category>
		<category><![CDATA[cout.tie(NULL)]]></category>
		<category><![CDATA[cpp]]></category>
		<category><![CDATA[ios_base::sync_with_stdio(false)]]></category>
		<category><![CDATA[ios_base::sync_with_stdio(false);]]></category>
		<category><![CDATA[std]]></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[백준 1389]]></category>
		<category><![CDATA[백준 1389번]]></category>
		<category><![CDATA[알고리즘]]></category>
		<category><![CDATA[케빈 베이컨의 6단계 법칙]]></category>
		<category><![CDATA[코딩테스트]]></category>
		<category><![CDATA[코테]]></category>
		<category><![CDATA[플로이드 워셜]]></category>
		<guid isPermaLink="false">https://lycos7560.com/?p=6182</guid>

					<description><![CDATA[<p>백준(BAEKJOON) 1389번 '케빈 베이컨의 6단계 법칙' 문제에 대한 글입니다. BFS 탐색을 이용하여 문제를 해결하였습니다. (This article is about the problem of Kevin Bacon's Six-Step Law, BAEKJOON No. 1389. BFS Search was used to resolve the issue.)</p>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-1389%eb%b2%88-%ec%bc%80%eb%b9%88-%eb%b2%a0%ec%9d%b4%ec%bb%a8%ec%9d%98-6%eb%8b%a8%ea%b3%84-%eb%b2%95%ec%b9%99-c-baekjoon/6182/">백준 1389번 (케빈 베이컨의 6단계 법칙, 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-362642f8      "
					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="#케빈-베이컨의-6단계-법칙" class="uagb-toc-link__trigger">케빈 베이컨의 6단계 법칙</a><li class="uagb-toc__list"><a href="#풀이-방법" class="uagb-toc-link__trigger">풀이 방법</a><li class="uagb-toc__list"><a href="#통과된-코드" class="uagb-toc-link__trigger">통과된 코드</a></ol>					</div>
									</div>
				</div>
			


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



<h1 class="wp-block-heading">케빈 베이컨의 6단계 법칙</h1>



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



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



<figure id="problem-info" class="wp-block-table"><table class="has-fixed-layout"><thead><tr><th class="has-text-align-left" data-align="left">시간 제한</th><th class="has-text-align-left" data-align="left">메모리 제한</th><th class="has-text-align-left" data-align="left">제출</th><th class="has-text-align-left" data-align="left">정답</th><th class="has-text-align-left" data-align="left">맞힌 사람</th><th class="has-text-align-left" data-align="left">정답 비율</th></tr></thead><tbody><tr><td class="has-text-align-left" data-align="left">2 초</td><td class="has-text-align-left" data-align="left">128 MB</td><td class="has-text-align-left" data-align="left">28386</td><td class="has-text-align-left" data-align="left">15830</td><td class="has-text-align-left" data-align="left">12440</td><td class="has-text-align-left" data-align="left">55.747%</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">케빈 베이컨의 6단계 법칙에 의하면&nbsp;지구에 있는 모든 사람들은 최대 6단계 이내에서 서로 아는 사람으로 연결될 수 있다. </p>



<p class="has-medium-font-size">케빈 베이컨 게임은 임의의 두 사람이 최소 몇 단계 만에 이어질 수 있는지 계산하는 게임이다.</p>



<p class="has-medium-font-size">예를 들면, 전혀 상관없을 것 같은 인하대학교의 이강호와 서강대학교의 민세희는 몇 단계만에 이어질 수 있을까?</p>



<p class="has-medium-font-size">천민호는 이강호와 같은 학교에 다니는 사이이다.  천민호와 최백준은 Baekjoon Online Judge를 통해 알게 되었다. </p>



<p class="has-medium-font-size">최백준과 김선영은 같이 Startlink를 창업했다. 김선영과 김도현은 같은 학교 동아리 소속이다. </p>



<p class="has-medium-font-size">김도현과 민세희는 같은 학교에 다니는 사이로 서로 알고 있다. </p>



<p class="has-medium-font-size">즉, 이강호-천민호-최백준-김선영-김도현-민세희 와 같이 5단계만 거치면 된다.</p>



<p class="has-medium-font-size">케빈 베이컨은 미국 헐리우드 영화배우들 끼리 케빈 베이컨 게임을 했을때 나오는 단계의 총 합이 가장 적은 사람이라고 한다.</p>



<p class="has-medium-font-size">오늘은 Baekjoon Online Judge의 유저 중에서 케빈 베이컨의 수가 가장 작은 사람을 찾으려고 한다. </p>



<p class="has-medium-font-size">케빈 베이컨 수는 모든 사람과 케빈 베이컨 게임을 했을 때, 나오는 단계의 합이다.</p>



<p class="has-medium-font-size">예를 들어, BOJ의 유저가 5명이고, 1과 3, 1과 4, 2와 3, 3과 4, 4와 5가 친구인 경우를 생각해보자.</p>



<p class="has-medium-font-size">1은 2까지 3을 통해 2단계 만에, 3까지 1단계, 4까지 1단계, 5까지 4를 통해서 2단계 만에 알 수 있다. </p>



<p class="has-medium-font-size">따라서, 케빈 베이컨의 수는 2+1+1+2&nbsp;= 6이다.</p>



<p class="has-medium-font-size">2는 1까지 3을 통해서 2단계 만에, 3까지 1단계 만에, 4까지&nbsp;3을 통해서 2단계 만에, 5까지 3과 4를 통해서 3단계 만에 알 수 있다. </p>



<p class="has-medium-font-size">따라서, 케빈 베이컨의 수는 2+1+2+3 = 8이다.</p>



<p class="has-medium-font-size">3은 1까지 1단계, 2까지 1단계, 4까지 1단계, 5까지 4를&nbsp;통해 2단계 만에 알 수 있다. </p>



<p class="has-medium-font-size">따라서, 케빈 베이컨의 수는 1+1+1+2 = 5이다.</p>



<p class="has-medium-font-size">4는 1까지 1단계, 2까지 3을 통해 2단계, 3까지 1단계, 5까지 1단계 만에 알 수 있다. </p>



<p class="has-medium-font-size">4의 케빈 베이컨의 수는 1+2+1+1 = 5가 된다.</p>



<p class="has-medium-font-size">마지막으로 5는 1까지 4를 통해 2단계, 2까지 4와 3을 통해 3단계, 3까지 4를 통해 2단계, 4까지 1단계 만에 알 수 있다. </p>



<p class="has-medium-font-size">5의 케빈 베이컨의 수는 2+3+2+1 = 8이다.</p>



<p class="has-medium-font-size">5명의 유저 중에서 케빈 베이컨의 수가 가장 작은 사람은 3과 4이다.</p>



<p class="has-medium-font-size">BOJ 유저의 수와 친구 관계가 입력으로 주어졌을 때, </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 (2 ≤ N ≤ 100)과 친구 관계의 수 M (1 ≤ M ≤ 5,000)이 주어진다. </p>



<p class="has-medium-font-size">둘째 줄부터 M개의 줄에는 친구 관계가 주어진다. </p>



<p class="has-medium-font-size">친구 관계는 A와 B로 이루어져 있으며, A와 B가 친구라는 뜻이다. </p>



<p class="has-medium-font-size">A와 B가 친구이면, B와 A도 친구이며, A와 B가 같은 경우는 없다. </p>



<p class="has-medium-font-size">친구 관계는 중복되어 들어올 수도 있으며, 친구가 한 명도 없는 사람은 없다. </p>



<p class="has-medium-font-size">또, 모든 사람은 친구 관계로 연결되어져 있다. </p>



<p class="has-medium-font-size">사람의 번호는 1부터 N까지이며, 두 사람이 같은 번호를 갖는 경우는 없다.</p>



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



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



<p class="has-medium-font-size">첫째 줄에 BOJ의 유저 중에서 케빈 베이컨의 수가 가장 작은 사람을 출력한다. </p>



<p class="has-medium-font-size">그런 사람이 여러 명일 경우에는 번호가 가장 작은 사람을 출력한다.</p>



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



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



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



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



<div style="height:60px" 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>문제를 만든 사람:&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/cscandkswon" target="_blank" rel="noreferrer noopener">cscandkswon</a>,&nbsp;<a href="https://www.acmicpc.net/user/harosh" target="_blank" rel="noreferrer noopener">harosh</a>,&nbsp;<a href="https://www.acmicpc.net/user/kjo7811" target="_blank" rel="noreferrer noopener">kjo7811</a>,&nbsp;<a href="https://www.acmicpc.net/user/onjo0127" target="_blank" rel="noreferrer noopener">onjo0127</a>,&nbsp;<a href="https://www.acmicpc.net/user/semtax" target="_blank" rel="noreferrer noopener">semtax</a></li>



<li>빠진 조건을 찾은 사람:&nbsp;<a href="https://www.acmicpc.net/user/kipa00" target="_blank" rel="noreferrer noopener">kipa00</a>,&nbsp;<a href="https://www.acmicpc.net/user/minjoonist" target="_blank" rel="noreferrer noopener">minjoonist</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/126" target="_blank" rel="noreferrer noopener">너비 우선 탐색</a></li>



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



<figure class="wp-block-image size-full"><img decoding="async" width="1600" height="1038" src="https://lycos7560.com/wp-content/uploads/2023/02/KakaoTalk_20230208_013050682.jpg" alt="" class="wp-image-6191" srcset="https://lycos7560.com/wp-content/uploads/2023/02/KakaoTalk_20230208_013050682.jpg 1600w, https://lycos7560.com/wp-content/uploads/2023/02/KakaoTalk_20230208_013050682-300x195.jpg 300w, https://lycos7560.com/wp-content/uploads/2023/02/KakaoTalk_20230208_013050682-768x498.jpg 768w, https://lycos7560.com/wp-content/uploads/2023/02/KakaoTalk_20230208_013050682-1536x996.jpg 1536w" sizes="(max-width: 1600px) 100vw, 1600px" /></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>



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

using namespace std;

int N, M, tempO, tempT;

int cntCB[101];

bool isVisted[101];

queue&lt;pair&lt;int, int>> myQ;

vector&lt;int> graph[101];

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

	cin >> N >> M;

	while (M--) {

		cin >> tempO >> tempT;
		// 양방향으로 경로를 입력받는다.
		graph[tempO].push_back(tempT);
		graph[tempT].push_back(tempO);
	}

	// 1번 부터 N번까지 전부 확인
	for (int i = 1; i &lt;= N; i++) {
		// 방문 처리를 초기화
		fill(isVisted, isVisted + 101, false);

		myQ.push(make_pair(i, 0));
		isVisted[i] = true;

		while (!myQ.empty()) {
			int now = myQ.front().first;
			int cnt = myQ.front().second;
			myQ.pop();

			for (int j = 0; j &lt; graph[now].size(); j++) {
				
				if (isVisted[graph[now][j]] == true) continue;
				else {
					isVisted[graph[now][j]] = true;

					myQ.push(make_pair(graph[now][j], cnt + 1));
					cntCB[i] += cnt + 1;
				}
			}
		}
	}

	pair&lt;int, int> resultP;
	resultP.second = INT32_MAX;
	for (int i = 1; i &lt;= N; i++) {
		if (resultP.second > cntCB[i]) {
			resultP.second = cntCB[i];
			resultP.first = i;
		}
	}

	cout &lt;&lt; resultP.first;

	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/02/image-53.png" alt="" class="wp-image-6184" width="1121" height="111" srcset="https://lycos7560.com/wp-content/uploads/2023/02/image-53.png 1034w, https://lycos7560.com/wp-content/uploads/2023/02/image-53-300x30.png 300w, https://lycos7560.com/wp-content/uploads/2023/02/image-53-768x76.png 768w" sizes="(max-width: 1121px) 100vw, 1121px" /></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-1389%eb%b2%88-%ec%bc%80%eb%b9%88-%eb%b2%a0%ec%9d%b4%ec%bb%a8%ec%9d%98-6%eb%8b%a8%ea%b3%84-%eb%b2%95%ec%b9%99-c-baekjoon/6182/">백준 1389번 (케빈 베이컨의 6단계 법칙, 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-1389%eb%b2%88-%ec%bc%80%eb%b9%88-%eb%b2%a0%ec%9d%b4%ec%bb%a8%ec%9d%98-6%eb%8b%a8%ea%b3%84-%eb%b2%95%ec%b9%99-c-baekjoon/6182/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
