LightOJ 1106 dp

来源:互联网 发布:网络新书排行榜2017 编辑:程序博客网 时间:2024/05/06 17:46

Gone Fishing

 

John is going on a fishing trip. He has h hours available, and there are n lakes in the area all reachable along a single, one-way road. John starts at lake 1, but he can finish at any lake he wants. He can only travel from one lake to the next one, but he does not have to stop at any lake unless he wishes to. For each i (1 to n-1), the number of 5-minute intervals it takes to travel from lake i to lake i+1 is denoted ti. For example, t3=4 means that it takes 20 minutes to travel from lake 3 to 4.

To help plan his fishing trip, John has gathered some information about the lakes. For each lake i, the number of fish expected to be caught in the initial 5 minutes, denoted fi, is known. Each 5 minutes of fishing decreases the number of fish expected to be caught in the next 5-minute interval by a constant rate of di. If the number of fish expected to be caught in an interval is less than or equal to di, there will be no more fish left in the lake in the next interval. To simplify the planning, John assumes that no one else will be fishing at the lakes to affect the number of fish he expects to catch. Write a program to help John plan his fishing trip to maximize the number of fish expected to be caught. The number of minutes spent at each lake must be a multiple of 5.

Input

Input starts with an integer T (≤ 100), denoting the number of test cases.

Each case starts with a line containing two integers n (2 ≤ n ≤ 25) and h (1 ≤ h ≤ 16). Next, there is a line of nintegers specifying fi (0 ≤ fi ≤ 1000), then a line of n integers di (0 ≤ di ≤1000), and finally, a line of n-1 integers denoting ti (0 < ti < 192).

Output

For each test case, print the case number first. Then print the number of minutes spent at each lake, separated by commas, for the plan achieving the maximum number of fish expected to be caught. This is followed by a line containing the number of fish expected. If multiple plans exist, choose the one that spends as long as possible at lake 1. If there is still a tie, choose the one that spends as long as possible at lake 2, and so on.

Sample Input

3

2 1

10 1

2 5

2

4 4

10 15 20 17

0 3 4 3

1 2 3

4 4

10 15 50 30

0 3 4 3

1 2 3

Sample Output

Case 1:

45, 5

Number of fish expected: 31

Case 2:

240, 0, 0, 0

Number of fish expected: 480

Case 3:

115, 10, 50, 35

Number of fish expected: 724


题意:n个鱼塘,h小时时间,5分钟为一个单位时间,每个鱼塘初始钓鱼量,单位分钟减少的钓鱼量,从第i个鱼塘到i+1个鱼塘的时间,问h小时时间最多能钓多少鱼,如果答案不唯一,那么按照钓鱼时间输出第一个鱼塘多的,第二个。。。


题解:定义dp[i][j]为第i个鱼塘钓j个单位时间的最大量,那么转移方程就为

dp[i][j]=max(dp[i][j],dp[i-1][j-k]+cost[i][k])

cost[i][k]表示在第i个鱼塘钓k个单位时间的量。

只要枚举k即可


#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<vector>using namespace std;typedef long long ll;ll f[50],t[50],d[50],dp[50][500],cost[50][500],pre[50][500];vector<ll>sp;int main(){ll tt,cas=1;scanf("%lld",&tt);while(tt--){sp.clear();ll i,j,k,n,h;memset(dp,-1,sizeof(dp));memset(cost,0,sizeof(cost));memset(pre,-1,sizeof(pre));scanf("%lld%lld",&n,&h);for(i=1;i<=n;i++)scanf("%lld",&f[i]);for(i=1;i<=n;i++)scanf("%lld",&d[i]);for(i=1;i<n;i++)scanf("%lld",&t[i]);h*=12;for(i=1;i<=n;i++){ll now=f[i];for(j=1;j<=h;j++){if(now>0)cost[i][j]=now+cost[i][j-1];else cost[i][j]=cost[i][j-1];now-=d[i];}}ll ans=0;for(i=0;i<=h;i++)dp[1][i]=cost[1][i],ans=max(ans,dp[1][i]);for(i=2;i<=n;i++){for(j=0;j<=h;j++){for(k=0;k<=h;k++){if(j-t[i-1]-k>=0&&dp[i-1][j-k-t[i-1]]>=0){if(dp[i][j]<dp[i-1][j-k-t[i-1]]+cost[i][k]){pre[i][j]=k;dp[i][j]=dp[i-1][j-k-t[i-1]]+cost[i][k];ans=max(dp[i][j],ans);}}}}}printf("Case %lld:\n",cas++);ll pe=1;for(i=1;i<=n;i++){if(dp[i][h]>dp[pe][h]&&dp[i][h]>=0){pe=i;}}j=h;for(i=pe;i>1;i--){sp.push_back(pre[i][j]);j=j-t[i-1]-pre[i][j];}sp.push_back(j);reverse(sp.begin(),sp.end());for(i=sp.size();i<n;i++)sp.push_back(0);for(i=0;i<sp.size();i++){if(i==sp.size()-1){printf("%d\n",sp[i]*5);}else printf("%d, ",sp[i]*5);}printf("Number of fish expected: %lld\n",dp[pe][h]);}return 0;}


0 0
原创粉丝点击