1488: River Crossing

Time Limit: 1 Sec  Memory Limit:
128 MB

Submit: 83  Solved: 42



SubmitStatusWeb
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



思路:注意一句话,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.).

这句话有点迷。说明并没有对羊进行编号。假如第一只,第二只运过去,回来运第三只羊,花费还是按照第一只的算,以此类推。

另dp[i]为当运送第i只羊的最小花费,那么相当于它是由两种状态转移来的,一种是前i只羊一起运送,一种就是前j只羊一起运送,然后i-j到i只羊为第二批再运送一次,两者加和得到的较小值。

即dp[i]=min(dp[i],dp[i-j]+dp[j]+m)。



代码:

#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<cstdlib>
using namespace std;
int main() {
int t;
scanf("%d",&t);
while(t--) {
int n,m;
scanf("%d %d",&n,&m);
int dp[n+1],sum[n+1];
fill(dp,dp+n+1,0);
fill(sum,sum+n+1,0);
for(int i=1;i<=n;i++) {
scanf("%d",&sum[i]);sum[i]+=sum[i-1];
}
for(int i=1;i<=n;i++) {
dp[i]=sum[i]+m;
for(int j=1;j<=i;j++) {
dp[i]=min(dp[i],dp[j]+dp[i-j]+m);
}
}
printf("%d\n",dp[n]);
}
return 0;
}

第六届河南省赛 River Crossing 简单DP的更多相关文章

  1. 第七届河南省赛10403: D.山区修路(dp)

    10403: D.山区修路 Time Limit: 2 Sec  Memory Limit: 128 MB Submit: 69  Solved: 23 [Submit][Status][Web Bo ...

  2. 第七届河南省赛F.Turing equation(模拟)

    10399: F.Turing equation Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 151  Solved: 84 [Submit][St ...

  3. 第七届河南省赛10402: C.机器人(扩展欧几里德)

    10402: C.机器人 Time Limit: 2 Sec  Memory Limit: 128 MB Submit: 53  Solved: 19 [Submit][Status][Web Boa ...

  4. 第七届河南省赛G.Code the Tree(拓扑排序+模拟)

    G.Code the Tree Time Limit: 2 Sec  Memory Limit: 128 MB Submit: 35  Solved: 18 [Submit][Status][Web ...

  5. 第七届河南省赛B.海岛争霸(并差集)

    B.海岛争霸 Time Limit: 2 Sec  Memory Limit: 128 MB Submit: 130  Solved: 48 [Submit][Status][Web Board] D ...

  6. 第七届河南省赛A.物资调度(dfs)

    10401: A.物资调度 Time Limit: 2 Sec  Memory Limit: 128 MB Submit: 95  Solved: 54 [Submit][Status][Web Bo ...

  7. 第七届河南省赛H.Rectangles(lis)

    10396: H.Rectangles Time Limit: 2 Sec  Memory Limit: 128 MB Submit: 229  Solved: 33 [Submit][Status] ...

  8. NYOJ-712 探寻宝藏(第六届河南省程序设计大赛)

    探 寻 宝 藏 时间限制:1000 ms  |  内存限制:65535 KB 难度:5   描述 传说HMH大沙漠中有一个M*N迷宫,里面藏有许多宝物.某天,Dr.Kong找到了迷宫的地图,他发现迷宫 ...

  9. 湖南省第六届省赛题 Biggest Number (dfs+bfs,好题)

    Biggest Number 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描述 You have a maze with obstacles and non-zero di ...

随机推荐

  1. ios 类的内部结构

    1. Class 和 Object 的定义 /// An opaque type that represents an Objective-C class. typedef struct objc_c ...

  2. Problem E: 动物爱好者

    Problem E: 动物爱好者 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 882  Solved: 699[Submit][Status][Web ...

  3. sublime 集成git插件,及git常用命令

    以前用了很久的totoiseSVN,显然不够高大上,开发界的版本装x利器还得是git.然而git的命令行对没怎么用过linux的人可能觉得还不如用gitGUI,可是当你知道sublime可以集成git ...

  4. JavaScript系列----正则表达式

    1.正则表达式 1.1.正则表达式的类型 正则表达式在JavaScript中,提供了一种内置的构造函数--RegExp. 正则表达式有三种匹配模式: g: 表示全局模式,即模式应用于所有的字符串,而非 ...

  5. 机器学习数学|微积分梯度jensen不等式

    机器学习中的数学 觉得有用的话,欢迎一起讨论相互学习~Follow Me 原创文章,如需转载请保留出处 本博客为七月在线邹博老师机器学习数学课程学习笔记 索引 微积分,梯度和Jensen不等式 Tay ...

  6. 查看 docker 容器使用的资源

    在容器的使用过程中,如果能及时的掌握容器使用的系统资源,无论对开发还是运维工作都是非常有益的.幸运的是 docker 自己就提供了这样的命令:docker stats. 默认输出 docker sta ...

  7. Vue.js优雅的实现列表清单的操作

        一.Vue.js简要说明 Vue.js (读音 /vjuː/,类似于 view) 是一套构建用户界面的渐进式框架.与前端框架Angular一样, Vue.js在设计上采用MVVM模式,当Vie ...

  8. unlink()

    unlink() 函数删除文件. 若成功,则返回 true,失败则返回 false. 语法 unlink(filename,context) 参数 描述 filename 必需.规定要删除的文件. c ...

  9. C#泛型集合之List

    1.命名空间:System.Collections.Generic(程序集:mscorlib)2.描述: 1).表示可通过索引访问的对象的强类型列表:提供用于对列表进行搜索.排序和操作的方法. 2). ...

  10. 深度学习之tensorflow (一)

    一.TensorFlow简介 1.TensorFlow定义: tensor  :张量,N维数组 Flow   :  流,基于数据流图的计算 TensorFlow : 张量从图像的一端流动到另一端的计算 ...