# 第六届河南省程序设计大赛——H River Crossing（简单动态规划）

## 题目描述:

Afandi is herding N sheep across the expanses of grassland  when he finds himself blocked by a river. A single raft is available for transportation.

Afandi knows that he must ride on the raft for all crossings, but adding sheep to the raft makes it traverse the river more slowly.

When Afandi is on the raft alone, it can cross the river in M minutes When the i sheep are added, it takes Mi minutes longer to cross the river than with i-1 sheep (i.e., total M+M1   minutes with one sheep, M+M1+M2 with two, etc.).

Determine the minimum time it takes for Afandi to get all of the sheep across the river (including time returning to get more sheep).

## 输入描述:

<span style="color:#000000">On the first line of the input is a single positive integer k, telling the number of test cases to follow. 1 ≤ k ≤ 5  Each case contains:

* Line 1: one space-separated integers: N and M      (1 ≤ N ≤ 1000 , 1≤ M ≤ 500).

* Lines 2..N+1:  Line i+1 contains a single integer: Mi  (1 ≤ Mi ≤ 1000)
</span>

## 输出描述:

<span style="color:#000000">For each test case, output a line with the minimum time it takes for Afandi to get all of the sheep across the river.</span>

## 样例输入:

2
2 10
3
5
5 10
3
4
6
100
1


## 样例输出:

18
50

n代表有n只羊，m代表空船耗时

ac代码：

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
int main(){
int t,dp[1005],a[1005];
cin>>t;
while(t--){
int n,m;
cin>>n>>m;
dp[0]=m;   //空船耗时m
for(int i=1;i<=n;i++){
cin>>a[i];
dp[i]=dp[i-1]+a[i];   //带上i只羊初始耗时
}
for(int i=1;i<=n;i++){
for(int j=1;j<i;j++){
dp[i]=min(dp[i],dp[i-j]+dp[j]+m);  //将i只羊分为两批运，一批i-j只，一批j只 因为分两批所以中间要空船返回一下
}
}
cout<<dp[n]<<endl;
}
return 0;
}

• 点赞
• 评论
• 分享
x

海报分享

扫一扫，分享海报

• 收藏
• 打赏

打赏

来自师范的学渣

你的鼓励将是我创作的最大动力

C币 余额
2C币 4C币 6C币 10C币 20C币 50C币
• 举报
• 一键三连

点赞Mark关注该博主, 随时了解TA的最新博文

04-19 1725
05-24 442
05-18 212
05-28 302