Just For Dream

# uva:10003 - Cutting Sticks

#include<iostream>
#include<stdio.h>
#include<string.h>

using namespace std ;

const int INF = 1000000 ;

int num[55][55] ;
int len[55] ;
int n ;
int length ;

int dp(int  , int ) ;
int dist(int , int) ;

int main()
{
//	freopen("in.txt" , "r" , stdin) ;
while(cin>>length && length)
{
int i ;
int temp ;
int k ;

temp = 0 ;
scanf("%d" , &n) ;
//转化为段存储，每一段的长度可以求出
for(i = 0 ; i < n ; i ++)
{
scanf("%d" , &len[i]) ;

k = len[i] ;

len[i] = len[i] - temp ;

temp = k ;
}
len[i] =  length - temp ;

for(i = 0 ; i <= n ; i ++)
{
for(int j = 0 ; j <= n ; j ++)
{
num[i][j] = INF ;
}
}
cout<<"The minimum cutting is ";
cout<<dp(0 , n)<<"."<<endl ;
}
return 0 ;
}

int dp(int left , int right)//记忆化搜索
{
if(left==right)
return 0 ;

int & ans = num[left][right] ;

if(ans < INF)
return ans ;

for(int i = left ; i <= right ; i ++)
{
if(ans > dp(left , i) + dp(i+1 , right) + dist(left , right) )
ans = dp(left , i) + dp(i+1 ,right) + dist(left , right) ;
}

return ans ;
}

int dist(int i , int j)//求这一区间的总和
{
int sum = 0 ;

while(i <= j)
{
sum += len[i] ;
i ++ ;
}

return sum ;
}

#include <iostream>
#include <stdio.h>
#include <string.h>

using namespace std;
const int INF = 1000000 ;

int num[55][55] ;
int len[55] ;
int n ;
int length ;

int dist(int , int );

int main()
{
//freopen("uva10003.txt" , "r" , stdin) ;

int i ;
int j ;
int k ;
int temp ;

while(cin>>length && length)
{
temp = 0 ;
scanf("%d" , &n) ;

for(i = 0 ; i < n ; i ++)
{
scanf("%d" , &len[i]) ;

k = len[i] ;

len[i] = len[i] - temp ;

temp = k ;
}

len[i] =  length - temp ;

for(i = 0 ; i <= n ; i ++)
{
for(j = 0 ; j <= n ; j ++)
{
if(i!=j)
num[i][j] = INF ;
else
num[i][j] =  0  ;
}
}

for(temp = 0 ; temp <= n ; temp ++)//表示j-i的值，从0到n
{

for(i = 0 ; i <= n - temp ; i ++)
{
j = i + temp ;

for(k = 0 ; k <= j ; k ++)
{
if(num[i][j] > num[i][k] + num[k+1][j] + dist(i , j))
num[i][j] = num[i][k] + num[k+1][j] + dist(i , j) ;
}
}
}

printf("The minimum cutting is %d.\n" , num[0][n]) ;
}
return 0;
}

int dist(int i , int j)
{
int sum = 0 ;

while(i <= j)
{
sum += len[i] ;
i ++ ;
}

return sum ;
}


#### UVA 10003 Cutting Sticks（切棍子）

2016-08-02 13:45:39

#### UVA10003切木棍DP

2016-04-26 08:45:10

#### UVa 10003 - Cutting Sticks

2014-09-15 10:30:16

#### UVA 10003 Cutting Sticks

2017-07-16 09:11:10

#### UVa 10003 Cutting Sticks

2014-08-08 17:59:55

#### uva 10003 - Cutting Sticks

2014-08-07 21:04:28

#### UVA 10003 Cutting Sticks

2014-07-15 13:23:22

#### uva 10003——Cutting Sticks

2015-11-26 21:58:42

#### uva 10003 Cutting Sticks

2014-05-14 22:43:16