File size: 945 Bytes
c4b0eef |
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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <climits>
#include <cstring>
using namespace std;
#define MAX 100
int n, l;
int dp[MAX][MAX];
int minCost(vector<int> a, int left, int right)
{
if (right - left == 1)
{
return 0;
}
if (dp[left][right] != -1)
{
return dp[left][right];
}
int ans = 999999999;
for (int i = left + 1; i < right; i++)
{
ans = min(ans, minCost(a, left, i) + minCost(a, i, right) + (a[right] - a[left]));
}
return dp[left][right] = ans;
}
int main()
{
cin >> l;
while (l != 0)
{
cin >> n;
vector<int> points;
memset(dp, -1, sizeof dp);
points.push_back(0);
for (int i = 0; i < n; i++)
{
int x;
cin >> x;
points.push_back(x);
}
points.push_back(l);
int ans = minCost(points, 0, n + 1);
printf("The minimum cutting is %d.\n", ans);
cin >> l;
}
} |