λ¬Έμ λ§ν¬
10251λ²: μ΄μ λ©΄ν μν (acmicpc.net)
νμ΄
μ²μμλ κ·Έλν νμμΌλ‘ νμ΄λ³΄λ €κ³ νλ€. λ³΄ν΅ κ·Έλννμμ μνμ’μ°λ‘ νμνμ§λ§ μ΄κ±΄ νΉλ³νκ² μ€λ₯Έμͺ½, μλλ‘λ§ νμνλ κ·Έλν νμμΈ μ€ μμλ€. κ·Έλμ μ°μ μμ νλ₯Ό λ§λ€μ΄μ DPλ°°μ΄μ κΈ°λ¦μ μκ³Ό, κ±Έλ¦°μκ°μ λͺ¨λ μ μ₯νλ€.νμλμ€ κΈ°λ¦μ μμ΄ Gλ₯Ό λμ΄κ°λ©΄ κ·Έ κ²½λ‘λ‘λ λμ΄μ νμμ νμ§ μλλ€.
μ΄λ κ² νλ €νλλ μ°μ μμ νμ λ€μ΄κ°μΌ ν μ 보λ€μ΄ λ무 λ§μλ€ 1. μ’ν, 2. κΈ°λ¦μ μ΄λ, 3. κ±Έλ¦° μκ°, 4. λ°©ν₯ μ μ μ₯ν΄μΌνλ€. κ·Έλμ μμ λ λ΅μ΄ λμμ§λ§ μ μΆ κ²°κ³Ό λ©λͺ¨λ¦¬ μ΄κ³Όκ° λ¬λ€. μλͺ»λμμ κΉ¨λ«κ³ μκ³ λ¦¬μ¦ λΆλ₯λ₯Ό λ΄€λλ DPλ€. μ΄κ² μ DPμ§ μμνλλ° DPλ°°μ΄μ DP[yμ’ν][xμ’ν][λ°©ν₯μ ν νμ][λ°©ν₯] μ΄λ κ² λ§λ€λ©΄ μ΄ν΄κ° λλ€. μ¬κΈ°μ λ°©ν₯μ 0 λλ 1λ‘ νμνκ³ μμμ μλμ§ μΌμͺ½μμ μλμ§λ₯Ό νμνλ μΈλ±μ€μ΄λ€. λλ 0μ μμμ 1μ μΌμͺ½μμ μ¨κ²μΌλ‘ κ°μ£Όνλ€.
μλ₯Ό λ€μ΄, DP[y][x][k][0] μ μλ λλ μ€λ₯Έμͺ½μΌλ‘ ν μΉΈ κ° μ μλ€. μ¬κΈ°μ 0μ μμͺ½ λ°©ν₯μμ μλ€λ λ»μ΄λ―λ‘ μλμͺ½μΌλ‘ μ΄λν κ²½μ° λ°©ν₯μ νμ μκΈ°μ§ μλλ€. κ·Έλ¬λ μ€λ₯Έμͺ½μΌλ‘ κ° κ²½μ°λ λ°©ν₯μ νμ ν΄μΌνλ€.
μ¦ DP[y+1][x][k][0]μ΄ λκ±°λ DP[y][x+1][k+1]][1]μ΄ λκ±°λ λμ€ νλμ΄λ€.
μ νμμ μΈμ보면 μλμ κ°λ€.
DP[i + 1][j][k][0] = min(DP[i + 1][j][k][0], DP[i][j][k][0] + Down[i][j]);
DP[i + 1][j][k + 1][0] = min(DP[i + 1][j][k + 1][0], DP[i][j][k][1] + Down[i][j]);
DP[i][j + 1][k][1] = min(DP[i][j + 1][k][1], DP[i][j][k][1] + Right[i][j]);
DP[i][j + 1][k + 1][1] = min(DP[i][j + 1][k + 1][1], DP[i][j][k][0] + Right[i][j]);
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
|
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <string.h>
#include <string>
#include <stack>
#include <vector>
#include <math.h>
#include <queue>
#include <climits>
#include <algorithm>
#include <iomanip>
#include <map>
#define mod 1000000007
using namespace std;
typedef long long ll;
typedef priority_queue<pair<pair<int, int>, pair<pair<int, int>, bool>>, vector<pair<pair<int, int>, pair<pair<int, int>, bool>>>, greater<pair<pair<int, int>, pair<pair<int, int>, bool>>>> Priority_queue;
const int INF = INT_MAX;
const int MAX_N = 100 + 5;
int T;
int M, N, L, G;
int Right[MAX_N][MAX_N];
int Down[MAX_N][MAX_N];
int DP[MAX_N][MAX_N][2 * MAX_N][2];
bool is_ok(int y, int x) {
if (y <= 0 || y > M || x <= 0 || x > N)
return false;
return true;
}
void Init() {
memset(DP, 0x3f, sizeof(DP));
DP[1][1][0][0] = DP[1][1][0][1] = 0;
}
int main(void) {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> T;
while (T--) {
cin >> M >> N >> L >> G;
Init();
for (int i = 1; i <= M; i++) {
for (int j = 1; j < N; j++) {
cin >> Right[i][j];
}
}
for (int i = 1; i < M; i++) {
for (int j = 1; j <= N; j++) {
cin >> Down[i][j];
}
}
for (int i = 1; i <= M; i++) {
for (int j = 1; j <= N; j++) {
for (int k = 0; k < 2 * max(M,N); k++) {
if (i + 1 <= M) {//μλλ‘ μ΄λ
DP[i + 1][j][k][0] = min(DP[i + 1][j][k][0], DP[i][j][k][0] + Down[i][j]);
DP[i + 1][j][k + 1][0] = min(DP[i + 1][j][k + 1][0], DP[i][j][k][1] + Down[i][j]);
}
if (j + 1 <= N) {//μ€λ₯Έμͺ½ μ΄λ
DP[i][j + 1][k][1] = min(DP[i][j + 1][k][1], DP[i][j][k][1] + Right[i][j]);
DP[i][j + 1][k + 1][1] = min(DP[i][j + 1][k + 1][1], DP[i][j][k][0] + Right[i][j]);
}
}
}
}
int ans = INF;
for (int i = 0; i <= 2 * max(M,N); i++) {
if (DP[M][N][i][0] <= G)
ans = min(ans, (M + N - 2) * L + i);
if (DP[M][N][i][1] <= G)
ans = min(ans, (M + N - 2) * L + i);
}
if (ans == INF)
cout << -1 << "\n";
else cout << ans << "\n";
}
return 0;
}
|
cs |
μ΄ λ¬Έμ λ₯Ό νλ©΄μ μλ‘κ² μκ²λ μ λ³΄κ° μλ€. memsetμ ν λ 무νμΌλ‘ μ΄κΈ°ν νκ³ μΆμλλ μ΄λ»κ² νλ κΆκΈνλλ° λ³΄ν΅ 0x3f λ‘ μ΄κΈ°νλ₯Ό μν¨λ€κ³ νλ€. λ¬Όλ‘ 0x3f λ₯Ό λ λ°°ν΄λ INT_MAXκ° λ³΄λ€λ μλ€κ³ νλ€.
'λ°±μ€ > Platinum' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[BOJ] 23289 : μ¨νκΈ° μλ ! (0) | 2022.06.09 |
---|---|
[BOJ] 5373 : νλΉ (0) | 2022.05.16 |
[BOJ] 1028 : λ€μ΄μλͺ¬λ κ΄μ° (0) | 2022.03.23 |
[BOJ] 13308 : μ£Όμ μ (0) | 2022.03.15 |
[BOJ] 10272 : νμκΈ μ¬λ₯κΎΌ κΉμ μ (0) | 2022.03.11 |