Created
April 10, 2018 13:16
-
-
Save henrybear327/53610debb12f30d3215b05e333145966 to your computer and use it in GitHub Desktop.
uva10003.cpp
This file contains hidden or 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
#include <bits/stdc++.h> | |
using namespace std; | |
int dp[55][55]; | |
int dfs(int inp[], int n, int l, int r) // (l, r) | |
{ | |
if (r - l <= 1) | |
return 0; | |
if (dp[l][r] != -1) | |
return dp[l][r]; | |
int mn = INT_MAX; | |
for (int i = l + 1; i < r; i++) { | |
// 0 ...... l | |
mn = min(mn, inp[r] - inp[l] + dfs(inp, n, l, i) + dfs(inp, n, i, r)); | |
} | |
return dp[l][r] = mn; | |
} | |
int main() | |
{ | |
int l; | |
while (scanf("%d", &l) == 1 && l) { | |
int n; | |
scanf("%d", &n); | |
int inp[n + 2]; | |
// 0 ...... l | |
inp[0] = 0; | |
inp[n + 1] = l; | |
for (int i = 1; i <= n; i++) | |
scanf("%d", &inp[i]); | |
memset(dp, -1, sizeof(dp)); | |
printf("The minimum cutting is %d.\n", dfs(inp, n + 2, 0, n + 1)); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What do you want to do with this? I don't understand this code. Can you help me to understand it?