第六届河南省赛 River Crossing 简单DP
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 10
3
5
5 10
3
4
6
100
1
Sample Output
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的更多相关文章
- 第七届河南省赛10403: D.山区修路(dp)
10403: D.山区修路 Time Limit: 2 Sec Memory Limit: 128 MB Submit: 69 Solved: 23 [Submit][Status][Web Bo ...
- 第七届河南省赛F.Turing equation(模拟)
10399: F.Turing equation Time Limit: 1 Sec Memory Limit: 128 MB Submit: 151 Solved: 84 [Submit][St ...
- 第七届河南省赛10402: C.机器人(扩展欧几里德)
10402: C.机器人 Time Limit: 2 Sec Memory Limit: 128 MB Submit: 53 Solved: 19 [Submit][Status][Web Boa ...
- 第七届河南省赛G.Code the Tree(拓扑排序+模拟)
G.Code the Tree Time Limit: 2 Sec Memory Limit: 128 MB Submit: 35 Solved: 18 [Submit][Status][Web ...
- 第七届河南省赛B.海岛争霸(并差集)
B.海岛争霸 Time Limit: 2 Sec Memory Limit: 128 MB Submit: 130 Solved: 48 [Submit][Status][Web Board] D ...
- 第七届河南省赛A.物资调度(dfs)
10401: A.物资调度 Time Limit: 2 Sec Memory Limit: 128 MB Submit: 95 Solved: 54 [Submit][Status][Web Bo ...
- 第七届河南省赛H.Rectangles(lis)
10396: H.Rectangles Time Limit: 2 Sec Memory Limit: 128 MB Submit: 229 Solved: 33 [Submit][Status] ...
- NYOJ-712 探寻宝藏(第六届河南省程序设计大赛)
探 寻 宝 藏 时间限制:1000 ms | 内存限制:65535 KB 难度:5 描述 传说HMH大沙漠中有一个M*N迷宫,里面藏有许多宝物.某天,Dr.Kong找到了迷宫的地图,他发现迷宫 ...
- 湖南省第六届省赛题 Biggest Number (dfs+bfs,好题)
Biggest Number 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 You have a maze with obstacles and non-zero di ...
随机推荐
- LeetCode 501. Find Mode in Binary Search Tree (找到二叉搜索树的众数)
Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently occurred ...
- LeetCode 226. Invert Binary Tree (反转二叉树)
Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 Trivia:This problem was ...
- IntelliJ IDEA创建多模块依赖项目
刚从Eclipse转IDEA, 所以记录一下IDEA的使用 创建多模块依赖项目 1. 新建父工程 这样就创建好了一个普通项目,一般我们会把src删掉,在此项目下新建新的模块 2. 新建子模块 创建供前 ...
- Muduo阅读笔记---入门(一)
第一步:下载源码和文档 下载muduo项目的源码.<muduo-manual.pdf>文档,以及<Linux多线程服务端编程:使用muduo C++网络库.pdf>,这些是前期 ...
- WebService WSDL结构分析
转载地址:http://blog.csdn.net/sunchaohuang/article/details/3076375 WSDL (Web Services Description L ...
- route命令实例练习
第1章 命令配置 虚拟服务器 网卡配置信息 虚拟网卡名称 虚拟网卡模式 服务器01 eth1 10.0.0.10/24 nat模式 服务器02 eth2 10.0.0.11/24 nat模式 eth3 ...
- JS模块化-requireJS
1. 为什么要使用require.js 刚开始的时候,网页需要用到很多不同的插件,都是依次加载,需要注意其中的加载顺序即依赖关系. <script src="1.js"> ...
- 修改文件的所有者失(chown: changing ownership of `uploads': Operation not permitted)
在项目开发的时候,经常需要将文件上传到指定的目录下. 例如这次用thinkphp5的时候,需要在public目录下建立uploads目录用于存放上传的资源. 首先在命令窗口下输入: mkdir upl ...
- 【Win 10 应用开发】UI Composition 札记(三):与 XAML 集成
除了 DirectX 游戏开发,我们一般很少单独使用 UI Composition ,因此,与 XAML 互动并集成是必然结果.这样能够把两者的优势混合使用,让UI布局能够更灵活. 说到与 XAML ...
- Webpack 入门教程
Webpack 是一个前端资源加载/打包工具.它将根据模块的依赖关系进行静态分析,然后将这些模块按照指定的规则生成对应的静态资源. 本章节基于 Webpack3.0 测试通过. 从图中我们可以看出,W ...