River Crossing



1000 ms  |  内存限制: 65535


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).


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)


样例输入



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



样例输出



18 50



第六届河南省程序设计大赛

ACM_赵铭浩



题意:一个老汉带着n个羊过河-.-有n个值a[1],a[2]...a[n]

老汉过河有个时间--m,带一个羊过河的时间为m+a[1],。。。。带n个羊过河的时间为m+a[1]+a[2]+...+a[n]...

求让所有羊过河的最小时间-。-

属于动态规划问题-.-

先求出m个羊过河的最小时间,n个羊过河的情况可以分为n个一起过,或m个和n-m个过。。。

m可以像n一样分解-.-(但我们已求出了m个过河的最小时间-.-就不用分解了)所以我们只要让m从1-n-1...

代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int n,m,shu[1050],he[1050],jian[1050];
int main()
{
int t;scanf("%d",&t);
while (t--)
{
scanf("%d%d",&n,&m);
he[0]=m;
for (int i=1;i<=n;i++)
{
scanf("%d",&shu[i]);
he[i]=he[i-1]+shu[i];
}
for (int i=1;i<=n;i++)
{
jian[i]=he[i];
for (int j=1;j<i;j++)
jian[i]=min(jian[i],jian[j]+jian[i-j]+m);
}
printf("%d\n",jian[n]);
}
return 0;
}