第六届河南省赛 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 ...
随机推荐
- xml读取一行数据
#include<map>#include<iostream>#include<fstream>#include<string>using namesp ...
- Oracle结合Mybatis实现取表中前10条数据
之前一直使用mysql和informix数据库,查表中前10条数据十分简单: 最原始版本: select top * from student 当然,我们还可以写的复杂一点,比如外加一些查询条件? 比 ...
- 用ajax的同步请求解决登陆注册需要根据服务器返回数据判断是否能提交的问题
最近在写www.doubilaile.com的登陆注册.需要用ajax请求服务器判断用户名是否存在,用户名和密码是否匹配,进而提交数据.碰到的问题是异步请求都能成功返回数据,但是该数据不能作为紧接着的 ...
- 动态规划求一个序列的最长回文子序列(Longest Palindromic Substring )
1.问题描述 给定一个字符串(序列),求该序列的最长的回文子序列. 2.分析 需要理解的几个概念: ---回文 ---子序列 ---子串 http://www.cnblogs.com/LCCRNblo ...
- poj 1254 Hansel and Grethel
Hansel and Grethel Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 2199 Accepted: 100 ...
- How Many Answers Are Wrong
How Many Answers Are Wrong Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/ ...
- 2016 ICPC总结
2016 ICPC总结 九月份开学,开始知识点的补充,刚开始的几周都在刷acmsteps,十月开始进行专题性的学习,首先进行的数据结构,给自己定的计划,十一月前看完数据结构,刚开始的时候看的都是以前的 ...
- 初入红尘——在安联IT实习的一点感受(未完......)
文章很短,只有800字. 从踏进安联的大门开始,我便入了红尘. 安联的迎客之道 “花径不曾缘客扫,蓬门今始为君开.”我的第一个贵人就是前台的美君姐.由于路况不熟,所以我没把握好时间,到的时候比约定的面 ...
- awk内置函数
gsub(r,s,t) 在字符串t中,用字符串s替换和正则表达式r匹配的所有字符串.返回替换的个数.如果没有给出t,缺省为$0 index(s,t) 返回s 中字符串t 的位置,不出现时为0 leng ...
- TestNG并行测试
并行(多线程)技术在软件术语里被定义为软件.操作系统或者程序可以并行地执行另外一段程序中多个部分或者子组件的能力.TestNG允许我们以并行(多线程)的方式来执行测试.这就意味着基于TestNG测试组 ...