BOJ 11049: 행렬 곱셈 순서

https://www.acmicpc.net/problem/11049

  • 2월동안 187문제 풀기(지난달 누적 87개) (24/187)
  • 어이없는 이유로 시간초과가 났음.. int input() 함수는 BOJ에서는 선언해서 쓰면 안될듯.
    • 이게 오버로딩이 되는지 딴데서 불려버림. 그래서 시간초과가 남
  • 언젠가 풀어봤던것 같은 DP라서, top-down, bottom-up 모두로 풀어봄

먼저 Bottom-up 방식

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
#include <bits/stdc++.h>
using namespace std;

const int SZ = 500;
struct Matrix {
  int r,c;
} arr[SZ];
int N, dp[SZ+1][SZ+1];

void input() {
  ios::sync_with_stdio(false);
  cin >> N;
  for (int i = 0; i < N; i++)
    cin >> arr[i].r >> arr[i].c;
}

int main() {
  input();
  for(int i = 1; i < N; i++) {
    for(int s = 0; s+i < N; s++) {
      dp[s][s+i] = INT_MAX;
      for(int m = s; m <= s+i; m++) {
        int calc = arr[s].r * arr[m].c * arr[s+i].c;
        dp[s][s+i] = min(dp[s][s+i], dp[s][m] + dp[m+1][s+i] + calc);
      }
    }
  }
  cout << dp[0][N-1];
}

Top-down 방식

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
#include <bits/stdc++.h>
using namespace std;

const int SZ = 500;
struct Matrix{
  int r,c;
} arr[SZ];
int N, dp[SZ+1][SZ+1];

void input() {
  scanf("%d", &N);
  for (int i = 0; i < N; i++)
    scanf("%d%d", &arr[i].r, &arr[i].c);
}

int go(int lb, int ub) {
  if(lb == ub) return 0;
  if(lb == ub-1) return arr[lb].r * arr[lb].c * arr[ub].c;
  if(dp[lb][ub]) return dp[lb][ub];

  int ans = INT_MAX;
  for(int i = lb; i < ub; i++) {
    int calc = arr[lb].r * arr[ub].c * arr[i+1].r;
    ans = min(ans, go(lb,i) + go(i+1,ub) + calc);
  }
  return dp[lb][ub] = ans;
}

int main() {
  input();
  printf("%d", go(0,N-1));
}