使用动态规划解花店问题 两种思考方法分析

问题描述:

LITTLE SHOP OF FLOWERS
Description
You want to arrange the window of your flower shop in a most pleasant way. You have F bunches of flowers, each being of a different kind, and at least as many vases ordered in a row. The vases are glued onto the shelf and are numbered consecutively 1 through V, where V is the number of vases, from left to right so that the vase 1 is the leftmost, and the vase V is the rightmost vase. The bunches are moveable and are uniquely identified by integers between 1 and F. These id-numbers have a significance: They determine the required order of appearance of the flower bunches in the row of vases so that the bunch i must be in a vase to the left of the vase containing bunch j whenever i < j. Suppose, for example, you have bunch of azaleas (id-number=1), a bunch of begonias (id-number=2) and a bunch of carnations (id-number=3). Now, all the bunches must be put into the vases keeping their id-numbers in order. The bunch of azaleas must be in a vase to the left of begonias, and the bunch of begonias must be in a vase to the left of carnations. If there are more vases than bunches of flowers then the excess will be left empty. A vase can hold only one bunch of flowers.

Each vase has a distinct characteristic (just like flowers do). Hence, putting a bunch of flowers in a vase results in a certain aesthetic value, expressed by an integer. The aesthetic values are presented in a table as shown below. Leaving a vase empty has an aesthetic value of 0. 

 

V A S E S

12345

Bunches

1 (azaleas)

723-5-2416

2 (begonias)

521-410

23

3 (carnations)

-215-4-2020

According to the ta ble, azaleas, for example, would look great in vase 2, but they would look awful in vase 4.

To achieve the most pleasant effect you have to maximize the sum of aesthetic values for the arrangement while keeping the required ordering of the flowers. If more than one arrangement has the maximal sum value, any one of them will be acceptable. You have to produce exactly one arrangement.

Input

The first line contains two numbers: F, V.
The following F lines: Each of these lines contains V integers, so that Aij is given as the jth number on the (i+1)st line of the input file.

1 <= F <= 100 where F is the number of the bunches of flowers. The bunches are numbered 1 through F.

F <= V <= 100 where V is the number of vases.

-50 <= Aij <= 50 where Aij is the aesthetic value obtained by putting the flower bunch i into the vase j.

Output
The first line will contain the sum of aesthetic values for your arrangement.

Sample Input

3 5
7 23 -5 -24 16
5 21 -4 10 23
-21 5 -4 -20 20

Sample Output

53

分析:
这个问题简单描述一下,就是F束花,插在V个花瓶中,花和花瓶都有编号,分别为 1..F和1..V,花插在花瓶中,
需要按照编号的顺序插,例如1号不能插在2号的后边,每个花瓶只能插一束花。插在不同的瓶子上,都有不同的美观的值。(F<=V)
问题要求把F朵花,插在V个花瓶中有最大的美观值。

第一种方法的建立递推关系:
建立递推关系:
v[i[j]为第i朵花插到第j个花瓶上的美观值
most_b[i][j]表示,最后一朵花i,插在了第j个花瓶上的最大美观值。
most_b[i][j] = max{most_b[i-1][k]} + v[i][j] k = i-1...j-1
这个递推是的含义是:把i朵花插到j个花瓶上,并且第i朵插到第j个花瓶上了,他的值为把前i-1多花插到了
前k个花瓶上,并且第i-1朵插到第k个花瓶上了(由于顺序的限制,k的值只能为i-1到j-1)的最大值再加上把
第i朵插到第j个花瓶上的美观值。
得到这个递推关系就很容易写程序了:

cpp 代码
  1. #include<iostream></iostream>   
  2. using namespace std;   
  3.   
  4. int main(){   
  5.     int i,j,k;   
  6.     int most_b[100][100];//most_b[i][j]表示,最后一朵花i,插在了j个花瓶上的最大美观值   
  7.     int v[100][100];//v[i[j]为第i朵花插到第j个花瓶上的美观值   
  8.     const int MIN = -10000;   
  9.     int F,V;//F多花,V个花瓶   
  10.     int max = MIN;//最终的最大美观值   
  11.   
  12.     cin >> F >> V;   
  13.     for(i = 0; i < F; i++)   
  14.         for(j = 0; j < V; j++){   
  15.            cin >> v[i][j];   
  16.            most_b[i][j] = MIN;   
  17.         }   
  18.   
  19.       for(i = 0; i < V; i++)//初始化,把一朵花插到V个花瓶的最大美观值,为v[0][i]   
  20.         most_b[0][i] = v[0][i];   
  21.   
  22.     for(i = 1; i < F; i++)   
  23.       for(j = i; j < V; j++)   
  24.           for(k = i-1; k < j; k++)//递推求最大美观值   
  25.               if(most_b[i-1][k] + v[i][j] > most_b[i][j])   
  26.                   most_b[i][j] = most_b[i-1][k] + v[i][j];   
  27.        
  28.     for(i = F-1; i < V; i++)//最后一朵花插到了F-1到V那么多种可能的最大值即为所求的最大美观值   
  29.       if(max < most_b[F-1][i])   
  30.          max = most_b[F-1][i];   
  31.   
  32.     cout << max << endl;   
  33.     return 0;   
  34. }  

 第二种方法建立递推关系:
v[i[j]为第i朵花插到第j个花瓶上的美观值
我们换一个角度来考虑:
现在用most_b[i][j]表示把i朵花插到j个花瓶的最大美观值,我们想以此建立递推关系:
most_b[i][j] = max{most_b[i][j-1],most_b[i-1][j-1] + v[i][j]}
这个递推关系的含义是:
把i朵花插到j个花瓶的最大美观值=
max{把i朵花插到j-1个花瓶的最大美观值,把i-1朵花插到j-1个花瓶的最大美观值+把第i朵花插到第j个花瓶的美观值}
得到这个递推关系写出的代码要比上面的效率高:
为了方便,most_b的0行和0列没用,都从1开始

cpp 代码
  1. #include<iostream></iostream>   
  2. using namespace std;   
  3.   
  4. int main(){   
  5.   int i,j;   
  6.     int most_b[101][101];   
  7.     int v[101][101];   
  8.     const int MIN = -50001;   
  9.     int F,V;   
  10.     int max = MIN;   
  11.   
  12.     cin >> F >> V;   
  13.     for(i = 1; i <= F; i++)   
  14.         for(j = 1; j <= V; j++){   
  15.            cin >> v[i][j];   
  16.            most_b[i][j] = MIN;   
  17.         }   
  18.   
  19. //通过most_b[0][i]辅助作用可以减少不少代码,   
  20. //如果不用most_b[0][i],你需要初始化most_b第一行的值为v第一行的值   
  21. //第一列维most_b[i][1] = max{v[i..i][1]}   
  22. //通过下面初始化,把上面两个初始化操作合并到了递推求解那个循环中了   
  23.     for(i = 0; i <= V; i++)   
  24.         most_b[0][i] = 0;   
  25.        
  26. //从底向上递推求解:   
  27.      for(i = 1; i <= F; i++)   
  28.          for(j = i; j <= i + V-F; j++)   
  29.              if(most_b[i][j-1] > most_b[i-1][j-1] + v[i][j])   
  30.                   most_b[i][j] = most_b[i][j-1];   
  31.              else  
  32.                   most_b[i][j] = most_b[i-1][j-1] + v[i][j];   
  33.         
  34.      cout << most_b[F][V] << endl;   
  35.      return 0;   
  36. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值