8000 Merge pull request #481 from JM911/sol1 · hackerman91/basic-algo-lecture@9349952 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9349952

Browse files
Merge pull request encrypted-def#481 from JM911/sol1
Update 23258.cpp
2 parents 992067b + 6fa6bac commit 9349952

File tree

1 file changed

+41
-4
lines changed

1 file changed

+41
-4
lines changed

0x1C/solutions/23258.cpp

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,48 @@
1-
// Authored by : BaaaaaaaaaaarkingDog
1+
// Authored by : JM911
22
// Co-authored by : -
3-
// http://boj.kr/****************
3+
// http://boj.kr/c7820ba2110643c0b84124b50b732e94
44
#include <bits/stdc++.h>
55
using namespace std;
66

7+
#define MAX_SIZE 302
8+
int N, Q;
9+
int D[MAX_SIZE][MAX_SIZE][MAX_SIZE];
10+
711
int main(void){
812
ios::sync_with_stdio(0);
913
cin.tie(0);
10-
11-
}
14+
15+
cin >> N >> Q;
16+
17+
for(int i=1; i<=N; i++)
18+
for(int j=1; j<=N; j++){
19+
cin >> D[0][i][j];
20+
if(D[0][i][j]==0 && i!=j) // i!=j 인데 0을 입력 받은 경우 문제에서 준 최대 크기보다 큰 값으로 대입
21+
D[0][i][j] = 180000;
22+
}
23+
24+
// 3차원 배열 D의 첫 인덱스는 1, 2, ..., i번 도시까지 거쳐 갈 수 있을 때의 최소 거리값을 뜻함
25+
// 즉, D[i][s][e] 는 중간 도시의 번호가 i 이하임을 보장하면서 s 부터 e 까지 도달하는 거리의 최솟값이다.
26+
for(int i=1; i<=N; i++)
27+
for(int s=1; s<=N; s++)
28+
for(int e=1; e<=N; e++)
29+
D[i][s][e] = min(D[i-1][s][e], D[i-1][s][i] + D[i-1][i][e]);
30+
31+
while(Q--){
32+
int C, s, e;
33+
cin >> C >> s >> e;
34+
35+
int ans = D[C-1][s][e];
36+
if(ans > 175000)
37+
cout << -1 << '\n';
38+
else
39+
cout << ans << '\n';
40+
}
41+
}
42+
/*
43+
핵심 아이디어는 1 + 2 + 4 + ... + 2^(C-1) = 2^C - 1 을 이용하는 것
44+
즉, 아무리 돌아다녀도 C 이상의 도시에 방문하지만 않으면 총 이슬의 양은 2^C 이상이 될 수 없다.
45+
위 식에 의해 C-1 이하의 도시는 어디든 자유롭게 다닐 수 있다.
46+
D를 삼차원 배열로 선언하여 각 층에 1, 2, 3, ..., i번 도시까지 방문할 수 있는 경우의 플로이드 알고리즘 결괏값을 저장한다.
47+
이후 문제에서 원하는 값을 받아 출력만 하면 된다.
48+
*/

0 commit comments

Comments
 (0)
0