第六届河南省赛 zzulioj 1488: River Crossing (一维DP)nyoj 716

1488: River Crossing

Time Limit: 1 Sec   Memory Limit: 128 MB
Submit: 72   Solved: 35

Submit Status Web Board

Description

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

Input

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)

Output

For each test case, output a line with the minimum time it takes for Afandi to get all of the sheep across the river.

Sample Input

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

Sample Output

18
50
//这题是简单的DP题,就是题意较难理解(看了好长时间都没看懂T_T)
//题意:牧羊人有n头羊,他自己划船过河花费m分钟
一个牧羊人(阿凡底)有n头羊,现在他要将这n头羊都运过河,问最少花费多少时间。。。
但因为每加一头羊,划过河的时间就得相应的增加。例如:测试数据二:
牧羊人独自过河花费10分钟,
牧羊人和一头羊过河花费m+a[1]=10+3=13;
牧羊人和两头羊过河花费m+a[1]+a[2]=10+3+4=17;
......
牧羊人和5头羊过河花费m+a[1]+...+a[5]=10+3+4+6+100+1=124;
法1:如果牧羊人一次把羊全运过去要花费124分钟,显然很耗时间。
法2:(1)但如果牧羊人第一次运过去三头羊花费23分钟
     (2)牧羊人独自回来花费10分钟
      (3)牧羊人再运两头羊过去花费17分钟
     所以法2总共花费50分钟,显然费时很短。。。
题意理解了,那么这道题就是个简单的DP题了。。。
具体看代码:
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int a[1010];
int dp[1010];
int main()
{
	int t,n,m,x;
	int i,j,k;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d%d",&n,&m);
		a[0]=0;
		for(i=1;i<=n;i++)
		{
			scanf("%d",&x);
			a[i]=a[i-1]+x;
		}
		for(i=1;i<=n;i++)
		{
			dp[i]=a[i]+m;
			for(j=1;j<i;j++)
			{
				dp[i]=min(dp[i],dp[i-j]+dp[j]+m);
			}
		}
		printf("%d\n",dp[n]);
	}
	return 0;
}

 

HINT

Source

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值