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. maven中jetty插件配置

    maven中jetty插件的配置,可用于项目在内置jetty服务器中的部署. <plugin> <groupId>org.mortbay.jetty</groupId&g ...

  2. EF框架搭建小总结--CodeFirst模型优先

    前言:之前在下总结编写了一篇 EF框架搭建小总结--ModelFirst模型优先 博文,看到一段时间内该博文的访问量蹭.蹭蹭.蹭蹭蹭...往上涨(实际也不是很多,嘿嘿),但是还是按捺不住内心的喜悦(蛮 ...

  3. 二、Tomcat配置以及IDEA运行第一个Jsp项目——JavaWeb点滴

    一.Tomcat配置环境变量 tomcat从官网下载最新的即可,本人下载的是安装版本.在安装过程中需要设置用户名和密码以及选择相应的JDK的安装目录.这些都比较简单直接下一步即可,安装完成之后就是配置 ...

  4. JAVA提高十一:LinkedList深入分析

    上一节,我们学习了ArrayList 类,本节我们来学习一下LinkedList,LinkedList相对ArrayList而言其使用频率并不是很高,因为其访问元素的性能相对于ArrayList而言比 ...

  5. 磨刀不误砍柴工!vs2010快捷键大全

    最常用的快捷键: VS2012变化的快捷键: 注释::VS2010是(Ctrl+E,C),VS2012是(Ctrl+K, Ctrl+C),实际操作,按住Ctrl键不放,先按K键,再按C键.相当于Ctr ...

  6. IIS下自定义错误页面配置的两种方式(亲测可行)--IIS服务器

    网站自定义错误页面的设置,大家应该都知道它的重要性……不多说,下面带大家一步步在IIS下设置网站自定义错误页面…… 1.首先进入你的网站主页,找到[错误页](注意是IIS下的错误页不是.NET错误页) ...

  7. Linux学习(十六)VIM

    一.简介 VIM是vi的增强版.VIM是Linux平台上的主要编辑器.基本上所有的文档的新增,修改,保存都需要用到它.所以,掌握VIM是很有必要的. vim的安装非常简单,一条命令就可以了: yum ...

  8. NFS存储服务部署

    第1章 NFS介绍 1.1 NFS服务内容的概述 □ RPC服务知识概念介绍说明,以及RPC服务存在价值(必须理解掌握) □ NFS服务工作原理讲解(必须理解掌握) □ NFS共享文件系统使用原理讲解 ...

  9. JsDoc脚本注释文档生成

    使用jsDoc可使用特定注释,将注释的内容生成文档,可用于生成脚本库的API文档 jsdoc 文档:   http://usejsdoc.org/

  10. 最大流——Dinic算法

    前面花了很长时间弄明白了压入-重标记的各种方法,结果号称是O(V3)的算法测demo的时候居然TLE了一个点,看了题解发现所有人都是用Dinic算法写的,但它的复杂度O(V2E)明显高于前者,具体是怎 ...