문제) 백준 - 동적 계획법 - 서울에서 경산까지
https://www.acmicpc.net/problem/14863
14863번: 서울에서 경산까지
표준 입력으로 다음 정보가 주어진다. 첫 번째 줄에는 두 자연수 N과 K가 공백으로 분리되어 주어진다(3 ≤ N ≤ 100, 0 < K ≤ 100,000). 두 번째 줄에는 구간 1을 도보로 이동할 때 걸리는 시간(분), 이
www.acmicpc.net
도보로 이동하거나 자전거를 이용하는 방법을 택하여 제한 시간 이내에 얻을 수 있는 최대 모금액을 구하는 문제였습니다. 동적 계획법을 통해 해당 구간과 제한 시간을 memoization 하여 해결합니다.
C++ 소스코드)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int cache[MAX][100001]; | |
int solve(int now, int t){ | |
if(t > K) return -INF; | |
if(now == N + 1) return 0; | |
int &ret = cache[now][t]; | |
if(ret != -1) return ret; | |
ret = solve(now + 1, t + walk[now].first) + walk[now].second; | |
if(t + bycicle[now].first <= K) | |
ret = max(ret, solve(now + 1, t + bycicle[now].first) + bycicle[now].second); | |
return ret; | |
} |
Full Code)
GitHub - Chocochip101/BOJ_Solution: BOJ Solutions
BOJ Solutions. Contribute to Chocochip101/BOJ_Solution development by creating an account on GitHub.
github.com
반응형
'PS(Problem Solving) > 백준_BOJ' 카테고리의 다른 글
[백준] 21920번 - 서로소 평균 (C++) 문제 및 풀이 (0) | 2022.03.11 |
---|---|
[백준] 13164번 - 행복 유치원 (C++) 문제 및 풀이 (0) | 2022.03.11 |
[백준] 9024번 - 두 수의 합 (C++) 문제 및 풀이 (0) | 2022.03.07 |
[백준] 21940번 - 가운데에서 만나기 (C++) 문제 및 풀이 (0) | 2022.03.06 |
[백준] 1251번 - 단어 나누기 (Python) 문제 및 풀이 (0) | 2022.03.04 |
댓글