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
|
#include <iostream>
#include <vector>
#define MAXX 501
using namespace std;
typedef pair<int, int> pp;
typedef long long ll;
int dp[MAXX][MAXX];
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector<pp> matrix[MAXX];
for (int i = 1; i <= n; i++) {
int r, c;
cin >> r >> c;
matrix[i].push_back(pp(r, c));
}
for (int i = 1; i < n; i++) {
dp[i][i+1] = matrix[i].front().first * matrix[i].front().second * matrix[i+1].front().second;
}
//d[i][j] = i부터 j까지 곱했을 때 최소 연산 수
for (int len = 2; len <= n; len++) {
for (int i = 1; i <= n-len; i++) {
int j = i + len;
for (int k = i; k < j; k++) {
int tmp = dp[i][k]+dp[k+1][j]+ matrix[i].front().first * matrix[k].front().second * matrix[j].front().second;
if (!dp[i][j] || tmp < dp[i][j]) dp[i][j] = tmp;
}
}
}
cout << dp[1][n] << '\n';
return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
|
BOJ/C++